Skip to content

Instantly share code, notes, and snippets.

@chrwei
Created May 4, 2017 00:46
Show Gist options
  • Save chrwei/660ed599a39e1e87509bc316f3e4903e to your computer and use it in GitHub Desktop.
Save chrwei/660ed599a39e1e87509bc316f3e4903e to your computer and use it in GitHub Desktop.
esp8266 + 2 x ds12b20
#include <OneWire.h>
#include <DallasTemperature.h>
//pin definitions
const int pin1wire = D3;
//ds18b20 stuff
const int ds18Resolution = 12;
const int ds18count = 2;
DeviceAddress ds18addr[] = {
{ 0x28, 0xC1, 0x02, 0x64, 0x04, 0x00, 0x00, 0x35 },
{ 0x28, 0xE7, 0x0B, 0x63, 0x04, 0x00, 0x00, 0x44 }
};
uint8_t ds18index[ds18count]; //index of this matches ds18addr and the value is the index the library sees
unsigned int ds18delay;
unsigned long ds18lastreq;
OneWire oneWire(pin1wire);
DallasTemperature ds18(&oneWire);
void setup() {
Serial.begin(115200);
delay(10);
ds18.begin();
Serial.println();
Serial.println();
for(byte j=0; j<ds18count; j++) {
ds18index[j] = 255; //prime for failure
}
//loop the devices detected and match them with sensors we care about
for (byte i=0; i<ds18.getDeviceCount(); i++) {
DeviceAddress taddr;
if(ds18.getAddress(taddr, i)) {
ds18.setResolution(taddr, ds18Resolution); //also set desired resolution
boolean failed = false;
for(byte j=0; j<ds18count; j++) {
if(oneWire.crc8(taddr, 7)==ds18addr[j][7]) { //found it
Serial.print(i); Serial.print(" matches");Serial.println(j);
ds18index[j] = i; //store the lib index in the array
break; //stop the j loop
}
}
}
}
ds18.setWaitForConversion(false); //this enables asyncronous calls
ds18.requestTemperatures(); //fire off the first request
ds18lastreq = millis();
ds18delay = 750 / (1 << (12 - ds18Resolution)); //delay based on resolution
}
void loop() {
if(millis() - ds18lastreq >= ds18delay) {
for(byte i=0; i<ds18count; i++) {
Serial.print("Sensor: "); Serial.print(i);
Serial.print(" TempC: "); Serial.print(ds18.getTempCByIndex(ds18index[i]));
Serial.print(" TempF: "); Serial.print(ds18.getTempFByIndex(ds18index[i]));
Serial.println();
}
ds18.requestTemperatures();
ds18lastreq = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment