Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By kas
#61040 @warwick & al1fch

I tried the MQTT publish way
results are similar to Http REST: 700ms average
Values are a bit more scattered (545ms to 1083ms)

Image
Both methods are fully acceptable, giving a 3 month+ theoretical battery life (LiFePO4 3.3V 1500mAh) :twisted:
Don't forget to kill the "power" onboard led
Last edited by kas on Wed Feb 01, 2017 8:05 am, edited 1 time in total.
User avatar
By kas
#61876 Hi Juan

Again, the all trick is to work in masked time and avoid the 750ms delay per sensor specified in the official example

pseudo code is as follow:
Code: Select all - Data aquisition
     start convertion for 18B20 sensor    --> setOneWireConversion()
     other data (sensors, battery level)
 - Router connection (static IP)
 - Thingspeak connection
 - Get temperature values    --> getOneWireData()
 - Post ThingSpeak data string
 - Go deep sleep for x minutes

setOneWireConversion() is called early in the code, getOneWireData() is called much later, after time consuming tasks (router & Thingspeak connections)

Code for setOneWireConversion()
Code: Select allboolean setOneWireConversion() {                  // ---- OneWire code (DS18B20 only, non parasitic) -----
  if(!ds.reset())   return false;                 // test sensors availability
  ds.skip();                                      // Global addressing
  ds.write(0x44, 1);
  return true;
}


Code for getOneWireData()
Code: Select allint getOneWireData(float sens[]) {                         // get data (max 3 sensors)
  byte data[12], addr[8];
  byte n = 0, i = 0;
  sens[0] = sens[1] = sens[2] = 99;
 
  while(ds.read()==0 && i++<80)  delay(10);                  // wait for end of conversion (timeout 800ms)
  ds.reset_search();
  while(ds.search(addr)) {
    ds.reset();
    ds.select(addr);   
    ds.write(0xBE);                                          // Read Scratchpad
    for (int i=0; i<9; i++)     data[i] = ds.read();
    int16_t raw = (data[1] << 8) | data[0];
    sens[n] = (float)raw / 16.0;
    n++;
  }
  return n;                                                  // returns number of sensors on bus
}


Again, this code is for 18B20, non parasitic
Parasitic mode is also possible, but tricky ;)
User avatar
By juan3211
#61879 Hi @kas

Thanks a lot, sorry about my English, but what I mean is how do you get that times measured ?

I have done a trick with RTCmemory and now I have a online test. Please see this post:
http://www.esp8266.com/viewtopic.php?t=13559

(may be is better to answer here all the stuff).

I don't know how do you do it ? Is it the same way as mine ? Could you share your sketch to understand how have you done the time measurement ?

I hope you could understand me after watching my little test (that test is to compare between HTTP and MQTT methods, I know that with fixed IP (for example) I will get less time, but for the test, it is the same)

Hope your answer (and sketch ;)

Regards,