ESP8266 as Temperature sensor Extra parameters :)

8 years 3 months ago - 8 years 3 months ago #2675 by Adam69
Dear all,

During my 'play' with the temp sensor on a ESP8266, found the well working sketch. After all this playing, made a fully working sensor, in a little box. The readings where good, but sometime too mutch. Made some modifications to the code and added 3 parameters:

ADD-ON by Paulo da Graca - Dec. 2015

#define HYSTERIE 0.5 // Only if Temperature does change more than this amount, than send report
Added this, so that the ESP8266 does not have to send all the updates. I have seen that the sensor is really accurate
and differences of 0.05 where send, this is not realy needed, Put minimal change to report change.

#define REPORT_MAX_NO_REPORT 9 // IF Temperatue is the same, no report will be send, enter max NO sending report
If the temperatue is the same all the time, it is not needed to send this all the time. However if you will never
receive a change is it not clear if sensor is OFF or temp did not change. This will force an update every x times.

#define CALIBRATION -0.75 // calibartion of Temperature, give adjust parameter if temp is off, otherwise just 0
With my sensor found that the temp was a bit off, comparing with 2 others. To ajust the temp-reading add this.
In my case, the temp needed to be ajusted by 0,75 degrees celsius.



/*
  Created by Igor Jarc
 See http://iot-playground.com for details
 Please use community fourum on website do not contact author directly
 
 Code based on https://github.com/DennisSc/easyIoT-ESPduino/blob/master/sketches/ds18b20.ino
 
 External libraries:
 - https://github.com/adamvr/arduino-base64
 - https://github.com/milesburton/Arduino-Temperature-Control-Library
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.

 ADD-ON by Paulo da Graca - Dec. 2015
   #define HYSTERIE             0.5  // Only if Temperature is increase more or just less than this amount, than send report 
       Added this, so that the ESP8266 does have to send all the updates. I have seen that the sensor is really accurate
       and differences of 0.05 where send, this is not realy needed, Put minimal change to report change.
   #define REPORT_MAX_NO_REPORT   9  // IF Temperatue is the same, no report will be send, enter max NO sending report
       If the temperatue is the same all the time, it is not needed to send this all the time. However if you will never
       receive a change is it not clear if sensor if OFF or temp did not change. This will force an update every x times.
   #define CALIBRATION        -0.75  // calibartion of Temperature, give adjust parameter if temp is off, otherwise just 0
       With my sensor found that the temp was a bit off, comparing with 2 others. To ajust the temp-reading add this.
       In my case, the temp needed to be ajusted by 0,75 degrees celsius.
*/

#include <ESP8266WiFi.h>
#include <Base64.h>
#include <OneWire.h>
#include <DallasTemperature.h>


//AP definitions
#define AP_SSID          "AP_SSID"
#define AP_PASSWORD      "AP_PASSWORD"

// EasyIoT server definitions
#define EIOT_USERNAME    "admin"
#define EIOT_PASSWORD    "test"
#define EIOT_IP_ADDRESS  "192.168.1.100"
#define EIOT_PORT        80

// NODE definitions
#define EIOT_NODE        "N1S0"

// Settings
#define REPORT_INTERVAL       60  // Interval between reports in sec, to send Temperature to IoTserver
#define HYSTERIE             0.5  // Only if Temperature is increase more or just loss than this amount, than send report 
#define REPORT_MAX_NO_REPORT   9  // IF Temperatue is the same, no report will be send, enter MAX no sending report
#define CALIBRATION           -2  // calibartion of Temperature, give adjust parameter if temp is off, otherwise just 0


#define ONE_WIRE_BUS 2  // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;
int No_Report;

void setup() {
  Serial.begin(115200);
  
  wifiConnect();
    
  char uname[USER_PWD_LEN];
  String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);  
  str.toCharArray(uname, USER_PWD_LEN); 
  memset(unameenc,0,sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
  oldTemp = -1;
  No_Report = 0;
}

void loop() {
  float temp;
  
  do {
    DS18B20.requestTemperatures(); 
    temp = DS18B20.getTempCByIndex(0);
    Serial.print("Temperature: ");
    Serial.println(temp);
  } while (temp == 85.0 || temp == (-127.0));
  
  temp = temp + CALIBRATION;

No_Report++;
if (No_Report <= REPORT_MAX_NO_REPORT)
  {
  if (temp != oldTemp)
    {
    if (temp > (oldTemp + HYSTERIE))
      {
      sendTeperature(temp);
      oldTemp = temp;
      No_Report = 0;
      }
    else
      {
      if (temp < (oldTemp - HYSTERIE))
        {
        sendTeperature(temp);
        oldTemp = temp;
        No_Report = 0;
        }
      }
    }
  }
else
  {
  sendTeperature(temp);
  oldTemp = temp;
  No_Report = 0;
  }

  int cnt = REPORT_INTERVAL;
  
  while(cnt--)
    {
     delay(1000);
    }
}


void wifiConnect()
{
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");  
}


void sendTeperature(float temp)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }
 
  String url = "";
  // generate EasIoT server node URL
  url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE) + "/ControlLevel/"+String(temp);

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Authorization: Basic " + unameenc + " \r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}

I really hope this does help anyone, like it did help me :)
Kind regards,

Adam69

Please Log in or Create an account to join the conversation.

8 years 3 months ago #2677 by FabrizioC
I'm trying something similar:
float delta = m_temp - oldTemp;
  if (abs(delta) > 0.01 || (millis() - startTempMillis) >  MAX_REPORT_DELAY)
  {
    sendTeperature(sensorID, m_temp);
    startTempMillis = millis();
    oldTemp = m_temp;
  }

My delta is something like your HYSTERIE and my MAX_REPORT_DELAY (5 minutes, form me) prevent data to be unsent maximum for that time.

Please Log in or Create an account to join the conversation.

8 years 3 months ago - 8 years 3 months ago #2678 by Adam69
FabrizioC,

That looks damn good.
I did think that the fixed interval * max no reports... in my cace, reading every minute, times 10 would mean, a reading at least every 10 minutes.
In your case, you just look at the time regardless of the interval, that also pretty perfect :)

Also I did not find any sample code for the IF statement, but just today found that ELSE IF, will also work, this would mean, that I can cleanup the code a bit.

Anyway, I will leave it as it is now for me ( never break a running system ! ).
And thank you for sharing this code with us!
Regards,

Adam69

Please Log in or Create an account to join the conversation.

Time to create page: 0.230 seconds

Forum latest

  • No posts to display.