Chat here is you are connecting ESP-xx type modules to existing AVR based Arduino

Moderator: igrr

User avatar
By Milan1989
#71119 Hi,

I use Wemos D1 mini +Relay 5V as a gate opener.

My problem is in case of blackout the Wemos reboots itself and the relays open and close twice after the power came back.

It seems to be the Wemos checks all I/O ports.

Do you have any idea how I can handle the blackouts, because in this case my garage door will open.

The flowing code works fine:
Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>   
   
#define CONTACT1 4   
#define RELAY1 2 
#define RELAY2 0   
int prevState1;   
 
char ssid[] = "test"; //SSID of your Wi-Fi router
char pass[] = "test1"; //Password of your Wi-Fi router
IPAddress server(192, 168, 1, 136);   
   
const char *inTopic = "domoticz/out";   
const char *outTopic = "domoticz/in";   
const char *onmsg1 = "{\"idx\" : 3,\"nvalue\" : 1 }";   
const char *offmsg1 = "{\"idx\" : 3,\"nvalue\" : 0 }";   
const char *onmsg2 = "{\"idx\" : 4,\"nvalue\" : 1 }";   
const char *offmsg2 = "{\"idx\" : 4,\"nvalue\" : 0 }";   
   
  void callback(char* topic, byte* payload, unsigned int length) {   
    Serial.print("Message arrived [");   
    Serial.print(topic);   
    Serial.print("] ");   
    for (int i=0;i<length;i++) {   
      Serial.print((char)payload[i]);   
    }   
    Serial.println();
    if((char)payload[0] == '3') {
      digitalWrite(RELAY1, LOW);
      delay(500);
      digitalWrite(RELAY1, HIGH);
    }
    if((char)payload[0] == '4') {
      digitalWrite(RELAY2, LOW);
      delay(500);
      digitalWrite(RELAY2, HIGH);
    }
  }   
     
WiFiClient espClient;
PubSubClient client(espClient);
     
  void reconnect() {   
    // Loop until we're reconnected   
    while (!client.connected()) {   
      Serial.print("Attempting MQTT connection...");   
      // Attempt to connect   
      if (client.connect("espClient")) {   
        Serial.println("connected");   
        // Once connected, publish an announcement...   
        client.publish(outTopic,"Az espClient-nek sikerült csatlakozni az MQTT szerverhez");   
        // ... and resubscribe   
        client.subscribe(inTopic);   
      } else {   
        Serial.print("failed, rc=");   
        Serial.print(client.state());   
        Serial.println(" try again in 5 seconds");   
        // Wait 5 seconds before retrying   
        delay(5000);   
      }   
    }   
  }   
void setup() {   
pinMode(CONTACT1, INPUT);   
pinMode(RELAY1, OUTPUT);   
pinMode(RELAY2, OUTPUT);   
digitalWrite(CONTACT1, HIGH);   
digitalWrite(RELAY1, HIGH);   
digitalWrite(RELAY2, HIGH);
 
 Serial.begin(115200);
  delay(10);
 
  // Connect to Wi-Fi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to...");
  Serial.println(ssid);
 
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Wi-Fi connected successfully");
  client.setServer(server, 1883);   
  client.setCallback(callback);   
}   
   
void loop() {   
int curState1 = digitalRead(CONTACT1);   
Serial.println(curState1);   
  if (!client.connected()) {   
    reconnect();   
  }   
     
if (curState1 != prevState1){   
prevState1=curState1;   
   
if (prevState1 == LOW){   
client.publish(outTopic, offmsg1);   
client.publish(outTopic, offmsg2);   
}     
if (prevState1 == HIGH){   
client.publish(outTopic, onmsg1);   
client.publish(outTopic, onmsg2);   
}   
}   
  client.loop();   
}
User avatar
By QuickFix
#71170 Please don't double or cross-post. :idea:

You better not use GPIO0 (run-mode / flash-mode) and GPIO2 (serial TX) for the relays; it's asking for trouble.

GPIO0 needs to be high or floating at start-up to let the ESP run the inside code, GPIO2 will emit serial debug-code @74880 baud (thus will toggle high/low) at start-up. :idea: