Hi everybody, as the title says i have a problem with a if in my program.
The real cuestion is why don't works with two topics.
In color black is the problem
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Connect to the WiFi
const char* ssid = ""; // Poner aquí la SSID de la WiFi
const char* password = ""; // Poner aquí el passwd de la WiFi
const char* mqtt_server = "";
const int mqttPort = ;
const char* mqttUser = "";
const char* mqttPassword = "";
WiFiClient espClient;
PubSubClient client(espClient);
[b]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);
if (strcmp(topic, "Luces") == 0){
if (receivedChar == '0' )
// ESP8266 Huzzah outputs are "reversed"
{ digitalWrite(D2, LOW); }
if (receivedChar == '1')
{ digitalWrite(D2, HIGH); }
}
if (strcmp(topic, "Puerta") == 0){
if (receivedChar == '0' )
// ESP8266 Huzzah outputs are "reversed"
{ digitalWrite(D4, LOW); }
if (receivedChar == '1')
{ digitalWrite(D4, HIGH); }
}
if (strcmp(topic, "Aire") == 0){
if (receivedChar == '0' )
// ESP8266 Huzzah outputs are "reversed"
{ digitalWrite(D3, LOW); }
if (receivedChar == '1')
{ digitalWrite(D3, HIGH); }
}
Serial.println();
}
}[/b]
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266 Client",mqttUser,mqttPassword)) {
Serial.println("connected");
// ... and subscribe to topic
client.subscribe("Luces");
client.subscribe("Puerta");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
client.setServer(mqtt_server, mqttPort);
client.setCallback(callback);
setup_wifi();
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D4,OUTPUT);
}
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.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
}
Thanks for your time and apologize for my english