-->
Page 1 of 1

Wemos D1 mini +Relay5V blackout problem

PostPosted: Mon Oct 23, 2017 6:59 am
by Milan1989
Hi,

I use a Wemos D1 mini and Relay as a garage opener.
My problem is in case of blackout the Wemos reboot and swich off and on the releys twice.

It seems the wemos checks the ports every reboot.

Do you have any idea how I can solve this problem, because in case of balckout the garage door will open?

This code is working properly, but doesnt handle the blackout:

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();   
}

Re: Wemos D1 mini +Relay5V blackout problem

PostPosted: Tue Oct 24, 2017 7:16 am
by QuickFix
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: