Temperature and level sensor integration

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
oasisone
New user
Posts: 6
Joined: 08 May 2021, 08:39

Temperature and level sensor integration

#1 Post by oasisone » 05 Jun 2022, 08:48

Hello, i've got a level and temperature sensor that transmits its data via serial interface i can read it via the code below but i wanted to implement it in the espeasy so i can make some rules and have statistics online and show the data on a display.
i was thinking of 2 possibilities:
1. manage to implement this code in the espeasy so i could use it directly but i am new to this so i would need some links with documentation
2. read it on a simple arduino board and then output the serial and use it in the espeasy for further processing here there is some more documentation online so i think i might manage


Any propositions on how to tackle this?

Code: Select all

// Extracted from http://hack4life.pbworks.com/Arduino%20Solar%20Water%20Heater%20Sensor

const int inPin = 22; 

 

void setup() {

  pinMode(inPin, INPUT);

  Serial.begin(9600);

}

 

#define TIMEOUT 50000

#define MARK_HEADER 7000

#define SPACE_HEADER 3000

#define MARK 500

#define SPACE_ONE 1500

#define SPACE_ZERO 600

 

int expectPulse(int val){

  unsigned long t=micros();

  while(digitalRead(inPin)==val){

    if( (micros()-t)>TIMEOUT ) return 0;

  }

  return micros()-t;

}

 

// temp in celsious and level goes from 0 to 3

bool readTempNLevelSensor(char inPin, char &temp, char &level){

   byte data[5]={0,0,0,0,0};

   unsigned long val1;

   unsigned long st=micros();

   val1 = expectPulse(HIGH);

   if(val1>MARK_HEADER){

     val1 = expectPulse(LOW);

     if(val1>SPACE_HEADER){

       int c=39;

       for(;c>-1;c--){

         val1 = expectPulse(HIGH);

         if(val1<MARK){

          val1 = expectPulse(LOW);

          if(val1==0){

            //Serial.println("Mark error 0");

            break; 

          }

          if(val1<SPACE_ZERO){

            //0

            //data[c>>3]|=(1<<(c & B111));

          }else if(val1<SPACE_ONE){

            //1

            data[c>>3]|=(1<<(c & B111));

          }else{

           //Serial.print(val1);Serial.println(" Space error");

           break; 

          }

         }else{

          //Serial.println("Mark error");

          break; 

         }  

       }

       // Each reading should not take more than 70ms (use time to detect errors)

       if(micros()-st<70000){

         temp=data[3];

         level=data[2];

         //Serial.print(data[3]);Serial.print(" ");Serial.println((data[2]));

         //Serial.print(data[4],HEX);Serial.print(" ");Serial.print(data[3],HEX);Serial.print(" ");Serial.print(data[2],HEX);Serial.print(" ");Serial.print(data[1],HEX);Serial.print(" ");Serial.println(data[0],HEX);

         return true;

       }

    }

   }  

   return false;

}

 

void loop() {

  char temp,level;

  if(readTempNLevelSensor(inPin, temp, level)){

    Serial.print(temp,DEC);Serial.print("c ");Serial.println(level,DEC);

  }

}

User avatar
Ath
Normal user
Posts: 3419
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Temperature and level sensor integration

#2 Post by Ath » 05 Jun 2022, 08:57

You could try to receive the data using the Serial server, using the latest improvements in this pr, so it can be processed using rules, without a web-client connected.
You will need a Github account to be able to download the bin files from the Github Actions build (although the build partially failed, most build results are available).
/Ton (PayPal.me)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 29 guests