- Wed Feb 01, 2017 8:04 am
#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