Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #define DEBUG
- #define L1 D4 //pino de saida para acionamento da Lampada L1
- //informações da rede WIFI
- const char* ssid = "RedeSegura11"; //SSID da rede WIFI
- const char* password = "987654321"; //senha da rede wifi
- //informações do broker MQTT - Verifique as informações geradas pelo CloudMQTT
- const char* mqttServer = "m16.cloudmqtt.com"; //server
- const char* mqttUser = "wwwnjzwn"; //user
- const char* mqttPassword = "ZxePSQvbzxPp"; //password
- const int mqttPort = 11441; //port
- const char* mqttTopicSub ="casa/L1"; //tópico que sera assinado
- WiFiClient espClient;
- PubSubClient client(espClient);
- void setup() {
- Serial.begin(115200);
- pinMode(L1, OUTPUT);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- #ifdef DEBUG
- Serial.println("Conectando ao WiFi..");
- #endif
- }
- #ifdef DEBUG
- Serial.println("Conectado na rede WiFi");
- #endif
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- while (!client.connected()) {
- #ifdef DEBUG
- Serial.println("Conectando ao Broker MQTT...");
- #endif
- if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
- #ifdef DEBUG
- Serial.println("Conectado");
- #endif
- } else {
- #ifdef DEBUG
- Serial.print("falha estado ");
- Serial.print(client.state());
- #endif
- delay(2000);
- }
- }
- //subscreve no tópico
- client.subscribe(mqttTopicSub);
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- //armazena msg recebida em uma sring
- payload[length] = '\0';
- String strMSG = String((char*)payload);
- #ifdef DEBUG
- Serial.print("Mensagem chegou do tópico: ");
- Serial.println(topic);
- Serial.print("Mensagem:");
- Serial.print(strMSG);
- Serial.println();
- Serial.println("-----------------------");
- #endif
- //aciona saída conforme msg recebida
- if (strMSG == "1"){ //se msg "1"
- digitalWrite(L1, LOW); //coloca saída em LOW para ligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo
- }else if (strMSG == "0"){ //se msg "0"
- digitalWrite(L1, HIGH); //coloca saída em HIGH para desligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo
- }
- }
- //função pra reconectar ao servidor MQTT
- void reconect() {
- //Enquanto estiver desconectado
- while (!client.connected()) {
- #ifdef DEBUG
- Serial.print("Tentando conectar ao servidor MQTT");
- #endif
- bool conectado = strlen(mqttUser) > 0 ?
- client.connect("ESP8266Client", mqttUser, mqttPassword) :
- client.connect("ESP8266Client");
- if(conectado) {
- #ifdef DEBUG
- Serial.println("Conectado!");
- #endif
- //subscreve no tópico
- client.subscribe(mqttTopicSub, 1); //nivel de qualidade: QoS 1
- } else {
- #ifdef DEBUG
- Serial.println("Falha durante a conexão.Code: ");
- Serial.println( String(client.state()).c_str());
- Serial.println("Tentando novamente em 10 s");
- #endif
- //Aguarda 10 segundos
- delay(10000);
- }
- }
- }
- void loop() {
- if (!client.connected()) {
- reconect();
- }
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement