Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Anderson Cardoso
Published © GPL3+

Relay Board

A relay board with ESP8266 from LC.

IntermediateFull instructions provided2,816
Relay Board

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
ESP8266 WiFi 5V 1 Channel Relay Delay Module IoT Smart Home
×1
Arduino MKR WiFi 1010
Arduino MKR WiFi 1010
×1

Hand tools and fabrication machines

APP USR-TCP232-Test-V1.3
USB to TTL Serial Module

Story

Read more

Custom parts and enclosures

PCD

Schematics

Esquema

Code

Rele_wifi.ino

Arduino
#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

Anderson Cardoso
3 projects • 3 followers
Engenheiro em Automação e Controle ! Amante em rádio e eletrônica.
Contact

Comments

Please log in or sign up to comment.