Hello how are you?
I’m trying to solve the problem but I still can’t find the solution, I pass my complete configuration:
ITEMS
Switch LUZ3 "Luz 3"<light>{mqtt=">[mosquitto:CASA/LUZ3:command:ON:ON3],>[mosquitto:CASA/LUZ3:command:OFF:OFF3]"}
Switch LUZ4 "Luz 4"<light>{mqtt=">[mosquitto:CASA/LUZ4:command:ON:ON4],>[mosquitto:CASA/LUZ4:command:OFF:OFF4]"}
Number Tempcasa "Temperatura interior [%.0f °C]" <temperature> {mqtt="<[mosquitto:CASA/TEMPERATURA:state:default]"}
Number Humcasa "Humedad relativa [%.0f %%]" <humidity> {mqtt="<[mosquitto:CASA/HUMEDAD:state:default]"}
SITEMAPS
sitemap Casa label="Main Menu" {
Frame
{
Default item=Datos_NTP label="Fecha"
Default item=Datos_NTP1 label="Hora"
}
Frame
{
Text label="Casa" icon="group"
{
Text item=Tempcasa
Text item=Humcasa
Switch item=SENSORdeLUZ
Switch item=LUZ3
Switch item=LUZ4
Switch item=LUZ5
Switch item=LUZ6
Switch item=LUZ7
}
}
Frame
{
Text label="Taller" icon="group"
{
Switch item=LUZCHURRASQUERA
Switch item= SENSORDEMOVIMIENTOTALLER
}
}
Frame
{
Text label="Clima" icon="sun"
{
Text item=Temperature label= "Temperatura" icon= "temperature"
}
}
Frame
{
Text label="Datos astronĂłmicos" icon="sun"
{
Text item=AMANECER
Text item=ANOCHECER
Text item= ESTACIONES
Text item= SIGNOS
}
}
CODE WEMOS
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define relay3 4 //D2 // LUZ4
#define relay4 0 //D3 // LUZ5
#define mqtt_user "openhabian"
#define mqtt_password "leo"
#define wifi_ssid "Speedy-C8A471" // "Nombre de tu WiFi"
#define wifi_password "3015289896" // "Password de tu WiFi"
#define mqtt_server "192.168.1.43" // "IP de tu Raspberry"
#define mqtt_port 1883
String deviceID = "esp-relay";
#define intopic3 "CASA/LUZ3"
#define intopic4 "CASA/LUZ4"
WiFiClient espClient;
PubSubClient client;
void setup() {
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setClient(espClient);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(deviceID.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
client.subscribe(intopic3);
client.subscribe(intopic4);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Mensaje recibido [");
Serial.print(topic);
Serial.print("] ");
String(receivedChar) = "";
for (int i = 0; i < length; i++) {
receivedChar += (char)payload[i];
}
Serial.println(receivedChar);
if (receivedChar == "OFF3")
{
digitalWrite(relay3, LOW);
}
if (receivedChar == "ON3"){
digitalWrite(relay3, HIGH);
}
/////////////////////////////
if (receivedChar == "OFF4")
{
digitalWrite(relay4, LOW);
}
if (receivedChar == "ON4")
{
digitalWrite(relay4, HIGH);
}
Serial.println();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();}
CODE NODEMUECU
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTPIN D5
#define wifi_ssid "Speedy-C8A471"
#define wifi_password "3015289896"
#define mqtt_server "192.168.1.43"
#define mqtt_port 1883
#define mqtt_user "openhabian"
#define mqtt_password "leo"
String deviceID = "esp-sensor";
#define humidity_topic "CASA/HUMEDAD"
#define temperature_topic "CASA/TEMPERATURA"
#define DHTTYPE DHT22 // DHT 22 (AM2302)
WiFiClient espClient;
PubSubClient client;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
setup_wifi();
client.setClient(espClient);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
dht.begin();
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(deviceID.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
}
Serial.println();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 50000) {
float hum = dht.readHumidity();
float temp = dht.readTemperature(false);
Serial.println(String(temp).c_str());
client.publish(temperature_topic, String(temp).c_str(), true);
Serial.println(String(hum).c_str());
client.publish(humidity_topic, String(hum).c_str(), true);
}}