Here are two sketeches that I got working.
WeMos D1 mini with DHT22 shield:
// This sketch is designed to work with an WeMos D1 min and the WeMos DHT SHield
#include <Homie.h>
#include <DHT.h>
#define DHTPIN D4 // what pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
const int TEMPERATURE_INTERVAL = 10;
unsigned long last_temperature_sent = 0;
const int HUMIDITY_INTERVAL = 10;
unsigned long last_humidity_sent = 0;
HomieNode temperatureNode("temperature", "temperature");
HomieNode humidityNode("humidity", "humidity");
DHT dht(DHTPIN, DHTTYPE);
void setupHandler() {
// Do what you want to prepare your sensor
}
void getSendTemperature() {
if (millis() - last_temperature_sent >= TEMPERATURE_INTERVAL * 1000UL || last_temperature_sent == 0) {
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
if (Homie.setNodeProperty(temperatureNode, "temperature", String(temperature), true)) {
last_temperature_sent = millis();
} else {
Serial.println("Sending failed");
}
}
}
void getSendHumid() {
if (millis() - last_humidity_sent >= HUMIDITY_INTERVAL * 1000UL || last_humidity_sent == 0) {
float humidity = dht.readHumidity();
if (isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
if (Homie.setNodeProperty(humidityNode, "humidity", String(humidity), true)) {
last_humidity_sent = millis();
} else {
Serial.println("Sending failed");
}
}
}
void loopHandler() {
getSendTemperature();
getSendHumid();
}
void setup() {
Homie.setFirmware("awesome-temperature", "1.0.0");
Homie.registerNode(temperatureNode);
Homie.registerNode(humidityNode);
Homie.setSetupFunction(setupHandler);
Homie.setLoopFunction(loopHandler);
Homie.setup();
}
void loop() {
Homie.loop();
}
NodeMCU v1.0 with LED and push button:
#include <Homie.h>
const int PIN_LED = D2;
const int PIN_BUTTON = D1;
int buttonOLD=LOW;
HomieNode light("light", "light");
bool lightOnHandler(String message) {
if (message == "true") {
digitalWrite(PIN_LED, HIGH);
Homie.setNodeProperty(light, "on", "true"); // Update the state of the light
Serial.println("Light is on");
} else if (message == "false") {
digitalWrite(PIN_LED, LOW);
Homie.setNodeProperty(light, "on", "false");
Serial.println("Light is off");
} else {
return false;
}
return true;
}
HomieNode buttonNode("button", "button");
void loopHandler() {
if ( digitalRead(PIN_BUTTON) == HIGH && buttonOLD == LOW)
{
Homie.setNodeProperty(buttonNode, "event", "push", false);
Homie.setNodeProperty(buttonNode, "pressed", "true", true);
buttonOLD=HIGH;
}
if ( digitalRead(PIN_BUTTON) == LOW && buttonOLD == HIGH)
{
Homie.setNodeProperty(buttonNode, "pressed", "false", true);
buttonOLD=LOW;
}
}
void setup() {
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, LOW);
Homie.setFirmware("awesome-light" ,"1.0.0");
light.subscribe("on", lightOnHandler);
Homie.registerNode(light);
Homie.registerNode(buttonNode);
Homie.setLoopFunction(loopHandler);
Homie.setup();
}
void loop() {
Homie.loop();
}
Infact, the last sketch works for push button and switch.
When the button is pushed/pressed it sends two messages to mwtt (“event=pushed” and “pressed=true”)
When the button is released it sends just “pressed=false”.
Just delete the code you don’t need.
I will try to integrate these examples in GitHub and make a pull request.
Feel free to improve there sketches!!