ESP8266/Temp: Difference between revisions
Jump to navigation
Jump to search
m (sudo coded) |
m (#990000 yankee doodle hack to F from float) |
||
Line 2: | Line 2: | ||
<pre> | <pre> | ||
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) | // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) | ||
// Pass the GPIO pin number sensor(s) is connected to | // Pass the GPIO pin number sensor(s) is connected to | ||
OneWire oneWire(13); | OneWire oneWire(13); | ||
// Pass our oneWire reference to Dallas Temperature. | // Pass our oneWire reference to Dallas Temperature. | ||
DallasTemperature sensors(&oneWire); | DallasTemperature sensors(&oneWire); | ||
// Put this in setup() | // Put this in setup() | ||
sensors.begin(); | sensors.begin(); | ||
// Put this where you want to read temp | // Put this where you want to read temp | ||
sensors.requestTemperatures(); | sensors.requestTemperatures(); | ||
sensors.getTempCByIndex(0); | sensors.getTempCByIndex(0); | ||
</pre> | |||
Hack for Fahrenheit and float conversion | |||
<pre> | |||
sensors.requestTemperatures(); | |||
float tempFloat = sensors.getTempCByIndex(0) * 1.8 + 32; // convert to F | |||
int tempReading = int(tempFloat); | |||
int tempReadingTenth = int((tempFloat - tempReading) * 10); // Hack around %f | |||
// For use with sprintf() | |||
// sprintf(buff, 100, "Temperature: %d.%d", tempReadig, tempReadingTenth); | |||
</pre> | </pre> | ||
Latest revision as of 20:30, 23 November 2015
Temperature Sensing with Dallas DS18B20
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) // Pass the GPIO pin number sensor(s) is connected to OneWire oneWire(13); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); // Put this in setup() sensors.begin(); // Put this where you want to read temp sensors.requestTemperatures(); sensors.getTempCByIndex(0);
Hack for Fahrenheit and float conversion
sensors.requestTemperatures(); float tempFloat = sensors.getTempCByIndex(0) * 1.8 + 32; // convert to F int tempReading = int(tempFloat); int tempReadingTenth = int((tempFloat - tempReading) * 10); // Hack around %f // For use with sprintf() // sprintf(buff, 100, "Temperature: %d.%d", tempReadig, tempReadingTenth);
Dallas Library[edit]
http://www.milesburton.com/?title=Dallas_Temperature_Control_Library
Library Download:
References[edit]
- Sensor Data Sheet
- Arduino Reference Project
- ESP8266 Reference Project