Ghost switch triggers

The powerline router has a different name to the router for that exact reason. Also the ethernet is run from the router to the powerline access point so if the router is down so is the “extension”

The power supply is the one thing I’ve not tampered with here so powering the esp from the board sounds like an idea, I’ll scratch around as I may even have a suitable converter lying around, the gate board runs on 24v. If not I’ll order one and try this asap and then just wait to see if it works I guess

Don’t forget to post back and marked as solved when you get it fixed.

update/correction. urgh :crazy_face:

I’ve just opened up the control box and I have already trried to isolate the esp from the gate board with relays. I’ve been messing around with this problem since time began I’ve already forgotten all the things I’ve tried!

I’ll draw up a diagram with the connections but in a nutshell I use the board to trigger itself via the relay and the microcontroller opens or closes the relay obviously.
I used to have the pins directly wired into the gate board terminals and triggered them with a 3.3v pulse but with all the false triggers I thought this may not be so wise so isolated the two with the relays.

Not sure if changing the power supply to the board is going to make any difference now?

Maybe not, as the reply with ground to the ESP and just switch the gate board ?

I’ve just seen/heard weird things with electronic that interact with each other, but don’t have a common/shared ground.

If you have a pir connected to an esp, that is a known source of false triggers. You need to shield the pir from the esp.
But I am not sure about your system. Is this the case? If so, does the gate open when the pir senses motion independent of Ha being connected?

No, the gate is triggered with a switch only, no PIR.

The uploaded code is from the shared project below:

Agreed. With your router off the gate controller should not have been able to be contacted by HA.

The ESP doesn’t draw huge power spikes when transmitting (less than 0.2A) so a reasonable sized electrolytic capacitor (470uF to 1000uF) across the power lines close to the ESP module should prevent most problems there. An oscilloscope would be handy to check for noise / spikes on the power lines.

The ESP does output up to +17dBm (50mW) when transmitting which could cause interference with close by sensitive electronics (like cheap PIR modules). Move the ESP module a meter (yard) away from your gate controller electronics to be sure.

Once you have discounted the above sources of interference it’s really down to the code in the ESP.

I think I’ve found the culprit, I don’t understand it and I’ve been here before thinking I’ve solved it, for it to start again a few days down the line…
I took the relay/esp on a battery pack and moved out of the wifi range, when back in range on re-connecting the relay would sometimes trigger. Not everyime just sometimes but I have moved my main router so that I can now get a signal by the gate so I don’t have to rely on the extension anymore. The extension feeds my CCTV cameras and they never drop out so not sure why there seems to be this apparent blip for the gate connection but I’ve not had any ghost triggers since connecting directly to the main router. Also my other relays don’t trigger when my wifi or internet drops out?
So like I said, I still don’t understand what exactly is causing it but it seems to have stopped after changing some things…
I will set up a test mqtt switch connected to the extension and monitor it over the next couple days to compare against the gate switch so I can see if there are any ghost triggers on that but for now the gate remains closed. shew!

So when the wifi drops out the gate sometimes opens?
That’s still not ideal. I’d be looking at the ESP code to correct that. Either to stop it happening or make it constant (i.e. lose wifi and gate opens for access).

The code is slightly adjusted from the shared project by erickjoaquin posted earlier and used widely on other controllers throughout my house and I’m sure many other users houses too. Funny enough the gate hasn’t misbehaved once since changing the router log on details but today my hifi relay has switched itself off and on at lease 4 times today, which I’ve not noticed before, but the logs show no record of this
Maybe there is something wrong with the code…

The answer may lie in the interference. I need to find out from other member who use TP Link switches if they experience problems when opening their KASA app, like I said all my relays click away randomly when the app is open. Causes absolute chaos in my house so I stopped using it

I switch off the router radio at night (wife’s instructions) which automatically turns back on at 6h00 in the morning. This morning the gate opened when the radio came back on an I also heard relays clicking in my bedroom but no lights went on so I guess the on/off is so quick the switch doesn’t have time to react, the gate on the other hand only needs a pulse to activate and open.
Only the gate and a relay in the living room shows up on the log, nothing from the bedroom?

06:01 Diffuser turned off
06:01 Gate turned off
06:01 Car Gate turned off
06:00 Gate turned on
06:00 Car Gate turned on
06:00 Gate turned off
06:00 Pedestrian Gate turned off
06:00 Gate turned on
06:00 Pedestrian Gate turned on

This “self” triggering is just too odd and I can’t find any mention of anyone else experiencing this so I doubt its the code…

I’m starting to think I have a poltergeist?! :hot_face:

My money is on interference from the WiFi , at a push try changing the mode on the esp. mine are all on wireless N , but try some lowering to G or even of u can take the performance hit B !

Wait, does the ESP do a reboot if it looses WiFi / MQTT ?

Are the pins ur using used to set boot modes ?

I’m using pins D1 and D2. I don’t think it reboots when reconnecting.

Gate openened again this morning as router came on but didn’t hear any other relay clicks but I was sleeping at the time so can’t be sure.

When I do a reboot on Hassio I get clicking relays too, is that quite normal?
Also when I power up my NodeMCU’s the relays do a little clicking dance, is that normal?
I always thought that is the state being set but the code is supposed to set them to low so I’ve always been a bit puzzled by this…

#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>

void callback(char* topic, byte* payload, unsigned int length);

//EDIT THESE LINES TO MATCH YOUR SETUP
#define MQTT_SERVER "192.168.1.6"  //you MQTT IP Address
const char* ssid = "HomeWiFi";
const char* password = "password";
IPAddress ip(192, 168, 1, 102);

//EJ: Data PIN Assignment on WEMOS D1 R2 https://www.wemos.cc/product/d1.html
// if you are using Arduino UNO, you will need to change the "D1 ~ D4" with the corresponding UNO DATA pin number 

const int switchPin1 = D1;
const int switchPin2 = D2;
const int switchPin3 = D3;
const int switchPin4 = D4;

//EJ: These are the MQTT Topic that will be used to manage the state of Relays 1 ~ 4
//EJ: Refer to my YAML component entry
//EJ: feel free to replicate the line if you have more relay switch to control, but dont forget to increment the number suffix so as increase switch logics in loop()

char const* switchTopic1 = "/gate/pedestrian1/";
char const* switchTopic2 = "/gate/car2/";
char const* switchTopic3 = "/gate/dummyswitch3/";
char const* switchTopic4 = "/gate/dummyswitch4/";


WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

void setup()
{
  //initialize the switch as an output and set to LOW (off)
  pinMode(switchPin1, OUTPUT); // Relay Switch 1
  digitalWrite(switchPin1, LOW);

  pinMode(switchPin2, OUTPUT); // Relay Switch 2
  digitalWrite(switchPin2, LOW);

  pinMode(switchPin3, OUTPUT); // Relay Switch 3
  digitalWrite(switchPin3, LOW);

  pinMode(switchPin4, OUTPUT); // Relay Switch 4
  digitalWrite(switchPin4, LOW);

  ArduinoOTA.setHostname("Gate NodeMCU"); // A name given to your ESP8266 module when discovering it as a port in ARDUINO IDE
  ArduinoOTA.begin(); // OTA initialization

  //start the serial line for debugging
  Serial.begin(115200);
  delay(100);

  //start wifi subsystem
  WiFi.begin(ssid, password);
  //attempt to connect to the WIFI network and then connect to the MQTT server
  reconnect();

  //wait a bit before starting the main loop
      delay(12000);
}


void loop()
{

  //reconnect if connection is lost
  if (!client.connected() && WiFi.status() == 3) {reconnect();}

  //maintain MQTT connection
  client.loop();

  //MUST delay to allow ESP8266 WIFI functions to run
  delay(10); 
  ArduinoOTA.handle();
}

void callback(char* topic, byte* payload, unsigned int length) {

  //convert topic to string to make it easier to work with
  String topicStr = topic; 
  //EJ: Note:  the "topic" value gets overwritten everytime it receives confirmation (callback) message from MQTT

  //Print out some debugging info
  Serial.println("Callback update.");
  Serial.print("Topic: ");
  Serial.println(topicStr);

   if (topicStr == "/gate/pedestrian1/") 
    {

     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin1, HIGH);
       client.publish("/gate/pedestrianConfirm1/", "1");
       delay(600);
       digitalWrite(switchPin1, LOW);
       client.publish("/gate/pedestrianConfirm1/", "0");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin1, LOW);
       client.publish("/gate/pedestrianConfirm1/", "0");
       }
     }

////

     // EJ: copy and paste this whole else-if block, should you need to control more switches
     else if (topicStr == "/gate/car2/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin2, HIGH);
       client.publish("/gate/carConfirm2/", "1");
       delay(600);
       digitalWrite(switchPin2, LOW);
       client.publish("/gate/carConfirm2/", "0");
       
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin2, LOW);
       client.publish("/gate/carConfirm2/", "0");
       }
     }
     
////
     
     else if (topicStr == "/gate/dummyswitch3/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin3, HIGH);
       client.publish("/gate/dummyswitchConfirm3/", "1");
       delay(600);
       digitalWrite(switchPin3, LOW);
       client.publish("/gate/dummyswitchConfirm3/", "0");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin3, LOW);
       client.publish("/gate/dummyswitchConfirm3/", "0");
       }
     }
     
////   
     
     else if (topicStr == "/gate/dummyswitch4/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin4, HIGH);
       client.publish("/gate/dummyswitchConfirm4/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin4, LOW);
       client.publish("/gate/dummyswitchConfirm4/", "0");
       }
     }
}


void reconnect() {

  //attempt to connect to the wifi if connection is lost
  if(WiFi.status() != WL_CONNECTED){
    //debug printing
    Serial.print("Connecting to ");
    Serial.println(ssid);

    //loop while we wait for connection
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    //print out some more debug once connected
    Serial.println("");
    Serial.println("WiFi connected");  
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }

  //make sure we are connected to WIFI before attemping to reconnect to MQTT
  if(WiFi.status() == WL_CONNECTED){
  // Loop until we're reconnected to the MQTT server
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");

      // Generate client name based on MAC address and last 8 bits of microsecond counter
      String clientName;
      clientName += "esp8266-";
      uint8_t mac[6];
      WiFi.macAddress(mac);
      clientName += macToStr(mac);

      //if connected, subscribe to the topic(s) we want to be notified about
      //EJ: Delete "mqtt_username", and "mqtt_password" here if you are not using any 
      if (client.connect((char*) clientName.c_str(),"username", "password")) {  //EJ: Update accordingly with your MQTT account 
        Serial.print("\tMQTT Connected");
        client.subscribe(switchTopic1);
        client.subscribe(switchTopic2);
        client.subscribe(switchTopic3);
        client.subscribe(switchTopic4);
        //EJ: Do not forget to replicate the above line if you will have more than the above number of relay switches
      }

      //otherwise print failed for debugging
      else{Serial.println("\tFailed."); abort();}
    }
  }
}

//generate unique name from MAC addr
String macToStr(const uint8_t* mac){

  String result;

  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);

    if (i < 5){
      result += ':';
    }
  }

  return result;
}

I would say that rebooting the ESP was causing the pins to go high low, triggering the gate.

But then u said it was fine over night, then once the WiFi turned on the gate opened.

Is there any way you can monitor the esp with the serial output ?
Then manually turn the WiFi of for a minute or two, then turn it back on and see what happens ?

Really clutching at straws now.

https://www.youtube.com/watch?v=31IyfM1gygo

This might help, watch the whole video, it’s about retain flags in MQTT. super cereal.

You might not be running Tasmota but he still goes into details about MQTT and the retain flags.

2 Likes

Thanks for the heads up, I’ll check it out and probably learn a thing or two.
I haven’t had a chance to give this much time lately but will be on it again in a few weeks. Luckily the gate is kind of behaving, only opens once a day now when router comes back on but I have got other esp’s going off and on randomly from time to time still.
I plan to set up a mirror esp, subscribed to the same mqtt pub/sub while connected to a serial monitor. Maybe that will give me some more info.

Another experiment is to change the code as I use the same base code for all my esp’s.
Do you know of any other published mqtt/hass project arduino code with two or more relay triggers and which is easily edited to add more?

This is brilliant! :star_struck:Although I’m not running tasmoto I do have the retained state set to true so I think you are barking up the right tree here. I will play around with the retained states and maybe even try uploading tasmoto if I have to.

With the gate I really only want a pulse to be sent and not a remain on/off. Currently I have it set so that when I trigger the gate it changes the state to high for 600milliseconds and then back to low again so I hope this is relatively easy to set up on tasmoto…

Thanks cooljim84, this may finally solve my headache. Will of course post updates and solutions once I’ve had a chance to tinkker this through

As soon as i watched it i thought “Wait that sounds like this post i was helping with”