2019-04-14 23:11:37 +02:00

53 lines
1.3 KiB
C++

/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
*********/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin D1 on the ESP8266 12-E - GPIO 5
#define ONE_WIRE_BUS 5
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature DS18B20(&oneWire);
char temperatureCString[6];
char temperatureFString[6];
// only runs once on boot
void setup() {
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);
DS18B20.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
}
void getTemperature() {
float tempC;
float tempF;
DS18B20.requestTemperatures();
tempC = DS18B20.getTempCByIndex(0);
dtostrf(tempC, 2, 2, temperatureCString);
tempF = DS18B20.getTempFByIndex(0);
dtostrf(tempF, 3, 2, temperatureFString);
delay(100);
}
// runs over and over again
void loop() {
getTemperature();
Serial.println(temperatureCString);
Serial.println(temperatureFString);
}