All,
thank you for your response.
from pressing the light button in the node red dashboard to switching on the light will take 4-6s.
the light is connected to the Wemos.
i am using my home wifi network, sow maybe i should create a local wifi network?
how often data is sent i am not sure, but you will find the arduino code on the bottom.
the node red version i use is v1.0.6
the Wemos is running by arduino
/*****
*****/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHTesp.h>
DHTesp dht;
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "**********";
const char* password = "**********";
// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "192.168.192.65";
// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);
// DHT Sensor
const int DHTPin = 12;
// S1
const int s1 = 5;
int switch1last = 0;
// S2
const int s2 = 4;
int switch2last = 0;
// S3
const int s3 = 0;
int switch3last = 0;
// Lamp1
const int lamp1 = 16;
// Lamp2
const int lamp2 = 14;
// Lamp3
const int lamp3 = 13;
// Analog output pin dimmer1
const int analogOutPinDimmer1 = 2;
int outputValueDimmer1 = 0; // value output to the PWM (analog out)
// relais Dimmer 1
const int rd1 = 15;
// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;
// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic room/lampx, you check if the message is either on or off. Turns the lampx GPIO according to the message
if(topic=="room/lamp1"){
Serial.print("Changing Room lamp1 to ");
if(messageTemp == "on"){
digitalWrite(lamp1, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(lamp1, LOW);
Serial.print("Off");
}
}
if(topic=="room/lamp2"){
Serial.print("Changing Room lamp2 to ");
if(messageTemp == "on"){
digitalWrite(lamp2, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(lamp2, LOW);
Serial.print("Off");
}
}
if(topic=="room/lamp3"){
Serial.print("Changing Room lamp3 to ");
if(messageTemp == "on"){
digitalWrite(lamp3, HIGH);
digitalWrite(rd1, HIGH);
Serial.print("On");
}
else if(messageTemp == "off"){
digitalWrite(lamp3, LOW);
digitalWrite(rd1, LOW);
Serial.print("Off");
}
}
if(topic=="room/dimmer1"){
Serial.print ("Dimmer1 ");
analogWrite(analogOutPinDimmer1, messageTemp.toInt());
Serial.print(messageTemp);
}
}
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
/*
YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
To change the ESP device ID, you will have to give a new name to the ESP8266.
Here's how it looks:
if (client.connect("ESP8266Client")) {
You can do it like this:
if (client.connect("ESP1_Office")) {
Then, for the other ESP:
if (client.connect("ESP2_Garage")) {
That should solve your MQTT multiple connections problem
*/
if (client.connect("ESP8266Client1")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("room/lamp1");
client.subscribe("room/lamp2");
client.subscribe("room/lamp3");
client.subscribe("room/dimmer1");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {
pinMode(lamp1, OUTPUT);
pinMode(lamp2, OUTPUT);
pinMode(lamp3, OUTPUT);
pinMode(rd1, OUTPUT);
pinMode(s1, INPUT_PULLUP);
pinMode(s2, INPUT_PULLUP);
pinMode(s3, INPUT_PULLUP);
Serial.begin(115200);
dht.setup(D6, DHTesp::DHT22);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client");
now = millis();
// Publishes new temperature and humidity every 10 seconds
if (now - lastMeasure > 10000) {
lastMeasure = now;
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.getHumidity();
// Read temperature as Celsius (the default)
float t = dht.getTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
// float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t) || isnan(f)) {
// if (isnan(h) || isnan(t)) {
// Serial.println("Failed to read from DHT sensor!");
// return;
// }
// Computes temperature values in Celsius
float hic = dht.computeHeatIndex(t, h, false);
static char temperatureTemp[7];
dtostrf(hic, 6, 2, temperatureTemp);
// Uncomment to compute temperature values in Fahrenheit
// float hif = dht.computeHeatIndex(f, h);
// static char temperatureTemp[7];
// dtostrf(hic, 6, 2, temperatureTemp);
static char humidityTemp[7];
dtostrf(h, 6, 2, humidityTemp);
// Publishes Temperature and Humidity values
//temperatureTemp = t;
//humidityTemp = h;
client.publish("room/temperature", temperatureTemp);
client.publish("room/humidity", humidityTemp);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
// Serial.print(f);
// Serial.print(" *F\t Heat index: ");
// Serial.print(hic);
// Serial.println(" *C ");
// Serial.print(hif);
// Serial.println(" *F");
}
//switch 1
int switch1 = digitalRead(s1);
int l1 = digitalRead(lamp1);
// compare the buttonState to its previous state
// Serial.println(switch1);
// Serial.println(switch1last);
// Serial.println(l1);
if (switch1 != switch1last) {
// if the state has changed, increment the counter
if (switch1 == 1 && l1 == 0) {
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
client.publish("room/switch1on", "on");
} else if (switch1 == 1 && l1 == 1) {
// if the current state is HIGH then the button went from off to on:
Serial.println("off");
client.publish("room/switch1off", "off");
}
}
if (switch1 != switch1last) {
// if the state has changed, increment the counter
if (switch1 == 0 && l1 == 0) {
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
client.publish("room/switch1on", "on");
} else if (switch1 == 0 && l1 == 1) {
// if the current state is HIGH then the button went from off to on:
Serial.println("off");
client.publish("room/switch1off", "off");
}
}
//switch 2
int switch2 = digitalRead(s2);
int l2 = digitalRead(lamp2);
// compare the buttonState to its previous state
// Serial.println(switch2);
// Serial.println(switch1last);
// Serial.println(l2);
if (switch2 != switch2last) {
// if the state has changed, increment the counter
if (switch2 == 1 && l2 == 0) {
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
client.publish("room/switch2on", "on");
} else if (switch2 == 1 && l2 == 1) {
// if the current state is HIGH then the button went from off to on:
Serial.println("off");
client.publish("room/switch2off", "off");
}
}
if (switch2 != switch2last) {
// if the state has changed, increment the counter
if (switch2 == 0 && l2 == 0) {
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
client.publish("room/switch2on", "on");
} else if (switch2 == 0 && l2 == 1) {
// if the current state is HIGH then the button went from off to on:
Serial.println("off");
client.publish("room/switch2off", "off");
}
}
//switch 3
int switch3 = digitalRead(s3);
int l3 = digitalRead(lamp3);
// compare the buttonState to its previous state
// Serial.println(switch2);
// Serial.println(switch1last);
// Serial.println(l2);
if (switch3 != switch3last) {
// if the state has changed, increment the counter
if (switch3 == 1 && l3 == 0) {
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
client.publish("room/switch3on", "on");
} else if (switch3 == 1 && l3 == 1) {
// if the current state is HIGH then the button went from off to on:
Serial.println("off");
client.publish("room/switch3off", "off");
}
}
if (switch3 != switch3last) {
// if the state has changed, increment the counter
if (switch3 == 0 && l3 == 0) {
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
client.publish("room/switch3on", "on");
} else if (switch3 == 0 && l3 == 1) {
// if the current state is HIGH then the button went from off to on:
Serial.println("off");
client.publish("room/switch3off", "off");
}
}
// save the current state as the last state, for next time through the loop
switch1last = switch1;
switch2last = switch2;
switch3last = switch3;
delay (1000);
}