Firmware portao automatico


#1

boas pessoal… tenho aki um codigo k nao estou a conseguir fazer rolar… o codigo é para um esp8266 k estara ligado ao meu portao automatico… consuigo via MQTT ler o estado do sensor no telemovel…mas nao consuigo acionar o rele…
na caixa serial do IDE aparece o estado do sensor e o estado do meu botao( on ou off ) mas a placa nao reage ao botao do rele

talvez seija coisa facil…:joy::joy:

Bloco de Citação
#include <ESP8266SSDP.h>

#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library
#include <PubSubClient.h> //MQTT

////START CUSTOM PARAMS********//

////Define parameters for the http firmware update // which we aren’t using
const char* host = “Garage1ESP”;
const char* ssid = “Scasa”;
const char* password = “ericeira”;

//Define the pins
#define RELAY_PIN 5
#define DOOR_PIN 2

//Define your own MQTT
#define mqtt_server “m14.cloudmqtt.com” //This is what you set up in HA.
#define door_topic “sensor/garage/state1” //you can change this name, but make sure you “replace all”
#define button_topic “sensor/garage/action1” //you can change this name, but make sure you “replace all”
const char* mqtt_user = “mtha”; //This is what you set up in HA.
const char* mqtt_pass = “BYwRC”; //This is what you set up in HA.

//END CUSTOM PARAMS********//
//This can be used to output the date the code was compiled
const char compile_date[] = DATE " " TIME;

WiFiClient espCgarage1;

//Initialize MQTT
PubSubClient client(espCgarage1);

//Setup Variables
String switch1;
String strTopic;
String strPayload;
char* door_state = “UNDEFINED”;
char* last_state = “”;

void setup() {
//Set Relay(output) and Door(input) pins
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
pinMode(DOOR_PIN, INPUT);

Serial.begin(115200);

setup_wifi();

client.setServer(mqtt_server, 18330); //1883 is the port number you have forwared for mqtt messages. You will need to change this if you’ve used a different port
client.setCallback(callback); //callback is the function that gets called for a topic sub
}

void loop() {
//If MQTT client can’t connect to broker, then reconnect
if (!client.connected()) {
reconnect();
}
checkDoorState();
client.loop(); //the mqtt function that processes MQTT messages

}

void callback(char* topic, byte* payload, unsigned int length) {
//if the ‘garage/button’ topic has a payload “OPEN”, then ‘click’ the relay
payload[length] = ‘\0’;
strTopic = String((char*)topic);
if (strTopic == button_topic)
{
switch1 = String((char*)payload);
Serial.println(switch1);
if (switch1 == “OPEN”)
{
//‘click’ the relay
Serial.println(“ON”);
digitalWrite(RELAY_PIN, HIGH);
delay(600);
digitalWrite(RELAY_PIN, LOW);
}
}
}

void setup_wifi() {

delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void checkDoorState() {
//Checks if the door state has changed, and MQTT pub the change
last_state = door_state; //get previous state of door
if (digitalRead(DOOR_PIN) == 1) // get new state of door
door_state = “fechado”;
else if (digitalRead(DOOR_PIN) == 0)
door_state = “aberto”;
if (last_state != door_state) { // if the state has changed then publish the change
client.publish(door_topic, door_state);
Serial.println(door_state);
}
}

void reconnect() {
//Reconnect to Wifi and to MQTT. If Wifi is already connected, then autoconnect doesn’t do anything.
Serial.print(“Attempting MQTT connection…”);
if (client.connect(host, mqtt_user, mqtt_pass)) {
Serial.println(“connected”);
client.subscribe(“sensor/garage/#”);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}


#2

já safas-te isso? se nao avisa que eu ajudo