Homie DS18B20
Posted: Wed Aug 24, 2016 6:27 pm
trying to flash ESP8266-01 and connect to DS18B20.
Here is my sketch and doesnt seem to pull the temperature. Works fine with ESPEasy or barebones without Homie
Please help!
Thanks,
Bill
Here is my sketch and doesnt seem to pull the temperature. Works fine with ESPEasy or barebones without Homie
Please help!
Thanks,
Bill
Code: Select all
#include <Homie.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define FW_NAME "temperature"
#define FW_VERSION "1.0.0"
const int TEMPERATURE_INTERVAL = 300;
unsigned long lastTemperatureSent = 0;
#define ONE_WIRE_BUS 2 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
HomieNode temperatureNode("temperature", "temperature");
void setupHandler() {
Homie.setNodeProperty(temperatureNode, "unit", "f", true);
}
void loopHandler() {
if (millis() - lastTemperatureSent >= TEMPERATURE_INTERVAL * 1000UL || lastTemperatureSent == 0) {
float temperature = 22; // Fake temperature here, for the example
DS18B20.requestTemperatures();
temperature = DS18B20.getTempFByIndex(0);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °F");
if (Homie.setNodeProperty(temperatureNode, "degrees", String(temperature), true)) {
lastTemperatureSent = millis();
} else {
Serial.println("Temperature sending failed");
}
}
}
void setup() {
Serial.begin(9600);
Serial.println(FW_NAME FW_VERSION);
DS18B20.begin();
Homie.setFirmware(FW_NAME, FW_VERSION);
Homie.registerNode(temperatureNode);
Homie.setSetupFunction(setupHandler);
Homie.setLoopFunction(loopHandler);
Homie.setup();
}
void loop() {
Homie.loop();
}