Control de cargas con ubidots

hola tengo un problema, no se si me podrán ayudar lo que sucede es que estoy suscribiéndome a 5 topics en ubidots con mqtt como se puede ver, pero cuando estoy en el tablero de ubidots si apago el botón control1 se apaga, pero cuando prendo el boton control2 se vuelve a prender el boton control 1 y así en los demás, como puedo hacer que cuando apague el boton control1 se quede apagado hasta que lo vuelva a accionar, sin que se active denuevo con los otros botones .

#include "UbidotsESPMQTT.h"
#include <ESP8266WiFi.h>

#define WIFINAME "CESAR" //Your SSID
#define WIFIPASS "1796#2019#" // Your Wifi Pass
#define TOKEN "xxxxxxxxxxxxxxxxxxxxx" // Ubidots TOKEN


Ubidots client(TOKEN);
int control1 = 0;
int control2 = 2;
int control3 = 14;
int control4 = 12;
int control5 = D7;

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++) {
    Serial.print((char)payload[i]);
  }

  Serial.println();
  if ((char)payload[0] == '1') {
    digitalWrite(control1, HIGH);
  }
  else {
    digitalWrite(control1, LOW);
  }
  if ((char)payload[0] == '1') {
    digitalWrite(control2, HIGH);
  }
  else {
    digitalWrite(control2, LOW);
  }
  if ((char)payload[0] == '1') {
    digitalWrite(control3, HIGH);
  }
  else {
    digitalWrite(control3, LOW);
  }
  if ((char)payload[0] == '1') {
    digitalWrite(control4, HIGH);
  }
  else {
    digitalWrite(control4, LOW);
  }
  if ((char)payload[0] == '1') {
    digitalWrite(control5, HIGH);
  }
  else {
    digitalWrite(control5, LOW);
  }
}


void setup() {
  Serial.begin(115200);
  client.setDebug(true);  // Pass a true or false bool value to activate debug messages
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe("esp8266-1", "control1");  // Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe("esp8266-1", "control2");  // Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe("esp8266-1", "control3");  // Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe("esp8266-1", "control4");  // Insert the dataSource and Variable's Labels
  client.ubidotsSubscribe("esp8266-1", "control5");  // Insert the dataSource and Variable's Labels
  pinMode(control1, OUTPUT);
  pinMode(control2, OUTPUT);
  pinMode(control3, OUTPUT);
  pinMode(control4, OUTPUT);
  pinMode(control5, OUTPUT);

}

void loop() {
  if (!client.connected()) {
    client.reconnect();
    client.ubidotsSubscribe("esp8266-1", "control1");  // Insert the dataSource and Variable's Labels
    client.ubidotsSubscribe("esp8266-1", "control2");  // Insert the dataSource and Variable's Labels
    client.ubidotsSubscribe("esp8266-1", "control3");  // Insert the dataSource and Variable's Labels
    client.ubidotsSubscribe("esp8266-1", "control4");  // Insert the dataSource and Variable's Labels
    client.ubidotsSubscribe("esp8266-1", "control5");  // Insert the dataSource and Variable's Labels
  }
  client.loop();


}

espero me puedan ayudar :frowning: de ante mano muchas gracias por su atención

Te advierto que no he usado esta librería pero ¿no deberías hacer algo así

if (topic == "control1") {
  if ((char)payload[0] == '1') { 
    digitalWrite(control1, HIGH); 
  } else { 
    digitalWrite(control1, LOW);
  }
}

if (topic == "control2") {
// lo que sigue...

y lo mismo para el resto?

Si no pasa el primer if() puede que en lugar de usar directamente la igualdad debas usar strcmp(). (Googlea sobre su uso)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.