Ricardo, boa tarde. Estou estudando o Alexa e quero controlar 2 ou mais modulos reles. Criei dois botões do Adafruit chamados de escritorio e escritorio_01, porem ao executar o programa para mqtt adafruit e depois upload no 8266, os botoes quando altero o status no site adafruit os reles nao trocam de estatus. O 8266 quando mudo de estatos os botes no adafruit altera o status (led acende e apaga, porem os reles que tem o controle pelas saidas de D2 e D4 nao alteram. Acredito que seja problema do programa, para 2 reles que nao sei fazer, deve estar errado. Para um rele conforme os videos funciona sem problema. Por favor poderia verificar seu o programa para 2 reles está certo ? o que sera que estou errando ? Obrigado
include
include "Adafruit_MQTT.h"
include "Adafruit_MQTT_Client.h"
// Mapeamento do módulo RELAY
define RELAY D2
define RELAY D4
/* WiFi Access Point */
define WLAN_SSID "Multilaser_2.4G_2FC158"
define WLAN_PASS "96243911"
/* Adafruit.io Setup */
define AIO_SERVER "io.adafruit.com"
define AIO_SERVERPORT 8883 // use 8883 for SSL
define AIO_USERNAME "npctecnologia"
define AIO_KEY "022b66b2416143f0b233298c75de81a2"
/** Global State (you don't need to change this!) **/
// Create an ESP8266 WiFiClient class to connect to the MQTT server. //WiFiClient client; // or... use WiFiFlientSecure for SSL WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
/** Feeds */
// Notice MQTT paths for AIO follow the form: /feeds/ Adafruit_MQTT_Subscribe escritorio = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/escritorio"); Adafruit_MQTT_Subscribe escritorio_01 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/escritorio_01");
/* Sketch Code **/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). void MQTT_connect();
void setup() {
pinMode(RELAY , OUTPUT); digitalWrite (RELAY ,HIGH);
Serial.begin(9600); delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point. Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println();
Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP());
// Setup MQTT subscription for escritorio e escritorio_01 mqtt.subscribe(&escritorio); mqtt.subscribe(&escritorio_01); }
uint32_t x=0;
void loop() { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop // try to spend your time here
Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))) { // Check if its the onoff button feed
if (subscription == &escritorio) { Serial.print(F("escritorio ")); Serial.println((char *)escritorio.lastread);
if (strcmp((char )escritorio.lastread, "ON") == 0) { digitalWrite(RELAY , LOW); } if (strcmp((char )escritorio.lastread, "OFF") == 0) { digitalWrite(RELAY , HIGH); } } if (subscription == &escritorio_01) { Serial.print(F("escritorio_01 ")); Serial.println((char *)escritorio_01.lastread);
if (strcmp((char )escritorio_01.lastread, "ON") == 0) { digitalWrite(RELAY , LOW); } if (strcmp((char )escritorio_01.lastread, "OFF") == 0) { digitalWrite(RELAY , HIGH); } }
}
// ping the server to keep the mqtt connection alive if(! mqtt.ping()) { mqtt.disconn ```