Components and supplies
ESP8266 WiFi 5V 1 Channel Relay Delay Module IoT Smart Home
Arduino MKR WiFi 1010
ESP8266 ESP-01
Tools and machines
USB to TTL Serial Module
APP USR-TCP232-Test-V1.3
Project description
Code
Rele_wifi.ino
arduino
1#include <ESP8266WiFi.h> 2#include <PubSubClient.h> 3 4#define topico_subscribe "pub/pin" 5#define topico_publish "sub/pin" 6#define id_mqtt "esp01" 7 8const char* ssid = "LINKESTOQUE"; 9const char* password = "qualidade12345"; 10const char* mqtt_server = "192.168.1.104"; 11 12WiFiClient espClient; 13PubSubClient client(espClient); 14 15char EstadoSaida = '0'; 16 17void initWiFi() 18{ 19 delay(10); 20 Serial.println("------Conexao WI-FI------"); 21 Serial.print("Conectando-se na rede: "); 22 Serial.println(ssid); 23 Serial.println("Aguarde"); 24 25 reconectWiFi(); 26} 27 28void reconnectMQTT() 29{ 30 while (!client.connected()) 31 { 32 Serial.print("* Tentando se conectar ao MQTT Sever: "); 33 Serial.println(mqtt_server); 34 35 if (client.connect(id_mqtt)) 36 { 37 Serial.println("Conectado com sucesso ao MQTT Server!"); 38 client.subscribe(topico_subscribe); 39 } 40 else 41 { 42 Serial.println("Falha ao reconectar no Servidor."); 43 Serial.println("Havera nova tentatica de conexao em 2s"); 44 delay(2000); 45 } 46 } 47} 48 49void reconectWiFi() 50{ 51 if (WiFi.status() == WL_CONNECTED) 52 return; 53 54 WiFi.begin(ssid, password); 55 56 while (WiFi.status() != WL_CONNECTED) 57 { 58 delay(100); 59 Serial.print("."); 60 } 61 62 Serial.println(); 63 Serial.print("Conectado com sucesso na rede "); 64 Serial.print(ssid); 65 Serial.println("IP obtido: "); 66 Serial.println(WiFi.localIP()); 67} 68 69void mqtt_callback(char* topic, byte* payload, unsigned int length) 70{ 71 String msg; 72 char On[4] = {0xA0, 0x01, 0x01, 0xA2}; 73 char Off[4] = {0xA0, 0x01, 0x00, 0xA1}; 74 75 for(int i = 0; i < length; i++) 76 { 77 char c = (char)payload[i]; 78 msg += c; 79 } 80 81 82 if (msg.equals("L")) 83 { 84 for(int a = 0; a < 4; a ++) 85 { 86 Serial.write (On[a]); 87 EstadoSaida = '1'; 88 } 89 } 90 91 92 if (msg.equals("D")) 93 { 94 for(int a = 0; a < 4; a ++) 95 { 96 Serial.write (Off[a]); 97 EstadoSaida = '0'; 98 } 99 } 100} 101 102void EnviaEstadoOutputMQTT(void) 103{ 104 if (EstadoSaida == '0') 105 client.publish(topico_publish, "Rele desligado"); 106 107 if (EstadoSaida == '1') 108 client.publish(topico_publish, "Rele ligado"); 109 110 Serial.println("- Estado da saida PIN enviado ao broker!"); 111 delay(1000); 112} 113 114void VerificaConexoesWiFIEMQTT(void) 115{ 116 if (!client.connected()) 117 reconnectMQTT(); 118 reconectWiFi(); 119} 120 121void setup() { 122 123 client.setServer(mqtt_server, 1883); 124 client.setCallback(mqtt_callback); 125 126 WiFi.begin(ssid, password); 127 128 Serial.begin(9600); 129 130 initWiFi(); 131 132} 133 134void loop() 135{ 136 VerificaConexoesWiFIEMQTT(); 137 EnviaEstadoOutputMQTT(); 138 139 client.loop(); 140 141} 142
Downloadable files
Esquema
Esquema
Esquema
Esquema
Documentation
PCD
PCD
PCD
PCD
Comments
Only logged in users can leave comments