Things used in this project

Hardware components:
Esp01
Everything ESP ESP8266 ESP-01
×1
Amazon Web Services ESP8266 WiFi 5V 1 Channel Relay Delay Module IoT Smart Home
×1
Hand tools and fabrication machines:
APP USR-TCP232-Test-V1.3
Amazon Web Services USB to TTL Serial Module

Custom parts and enclosures

Schematics

Esquema
Esp8266 11213 06 pih1k7eudc ojola8tfyu

Code

Rele_wifi.inoArduino
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define topico_subscribe  "pub/pin"
#define topico_publish    "sub/pin"
#define id_mqtt           "esp01"

const char* ssid = "LINKESTOQUE";
const char* password = "qualidade12345";
const char* mqtt_server = "192.168.1.104";

WiFiClient espClient;
PubSubClient client(espClient);

char EstadoSaida = '0';

void initWiFi() 
{
    delay(10);
    Serial.println("------Conexao WI-FI------");
    Serial.print("Conectando-se na rede: ");
    Serial.println(ssid);
    Serial.println("Aguarde");
    
    reconectWiFi();
}

void reconnectMQTT() 
{
    while (!client.connected()) 
    {
        Serial.print("* Tentando se conectar ao MQTT Sever: ");
        Serial.println(mqtt_server);
        
        if (client.connect(id_mqtt)) 
        {
            Serial.println("Conectado com sucesso ao MQTT Server!");
            client.subscribe(topico_subscribe); 
        } 
        else 
        {
            Serial.println("Falha ao reconectar no Servidor.");
            Serial.println("Havera nova tentatica de conexao em 2s");
            delay(2000);
        }
    }
}

void reconectWiFi() 
{
    if (WiFi.status() == WL_CONNECTED)
        return;
        
    WiFi.begin(ssid, password); 
    
    while (WiFi.status() != WL_CONNECTED) 
    {
        delay(100);
        Serial.print(".");
    }
  
    Serial.println();
    Serial.print("Conectado com sucesso na rede ");
    Serial.print(ssid);
    Serial.println("IP obtido: ");
    Serial.println(WiFi.localIP());
}

void mqtt_callback(char* topic, byte* payload, unsigned int length) 
{
    String msg;
    char On[4]   = {0xA0, 0x01, 0x01, 0xA2};
    char Off[4]  = {0xA0, 0x01, 0x00, 0xA1};
    
    for(int i = 0; i < length; i++) 
      {
        char c = (char)payload[i];
        msg += c;
      }
  
    
    if (msg.equals("L"))
    {
        for(int a = 0; a < 4; a ++)
           {
              Serial.write (On[a]);
              EstadoSaida = '1';
           }
    }
 
    
    if (msg.equals("D"))
    {
        for(int a = 0; a < 4; a ++)
          {
            Serial.write (Off[a]); 
            EstadoSaida = '0';
          }
    }   
}

void EnviaEstadoOutputMQTT(void)
{
    if (EstadoSaida == '0')
    client.publish(topico_publish, "Rele desligado");
 
    if (EstadoSaida == '1')
    client.publish(topico_publish, "Rele ligado");
 
    Serial.println("- Estado da saida PIN enviado ao broker!");
    delay(1000);
}

void VerificaConexoesWiFIEMQTT(void)
{
    if (!client.connected()) 
        reconnectMQTT(); 
        reconectWiFi(); 
}

void setup() {

  client.setServer(mqtt_server, 1883);
  client.setCallback(mqtt_callback);
  
  WiFi.begin(ssid, password);   
  
  Serial.begin(9600);

  initWiFi();  
 
}                                                                                                                                                                                                                                                                                                                                   

void loop() 
{
  VerificaConexoesWiFIEMQTT();
  EnviaEstadoOutputMQTT();

  client.loop();

}

Credits

B0c8d56df67f947f521cd15de7bc3618
anderson05
0 projects • 1 follower
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Add projectSign up / Login