-->
Page 1 of 1

ESP8266 door sensor not working right

PostPosted: Wed Apr 11, 2018 2:51 am
by danielocdh
I'm using ESP8266-1 the old blue one (I'm gonna get a couple new ones soon). I managed to finally understand how to upload and make sketches work from Arduino IDE with pieces of information from various sites. I'm using MQTT because I read that it was easier to integrate with openhab (which I might end up using).

The problem(s):
1. If I boot the ESP8266-1 with the door closed, the ESP8266-1 won't connect to my wifi router and both leds will stay on.
2. If I boot the ESP8266-1 with the door opened, the ESP8266-1 will work as expected but the sensor will work only once, first it will send a message "OPEN", then after I close the door it will send a message "CLOSED", after I close/open the door again nothing else will be sent, but I still can ping the module after that.

My ESP8266-1 leds:
In my case I have 2 leds, the ON led is red, the one that flashes when programming is blue.

My connections:
ground to GND
voltage to VCC
voltage to CH_PD
MC-38 sensor cable 1 to GPIO2
MC-38 sensor cable 2 to ground

My code:
Code: Select all#include <ESP8266WiFi.h>
#include <MQTTClient.h>

/* WIFI Settings */
// Name of wifi network
const char* ssid = "my_router";

// Password to wifi network
const char* password = "somepass";

/* MQTT Settings */
// Topic which listens for commands
char* outTopic = "SmartHouse/security/DoorSensor1";

//MQTT Server IP Address
const char* server = "192.168.0.1";

//Unique device ID
const char* mqttDeviceID = "DoorSensor1";

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all detection has stopped
long unsigned int pause = 100; 

//sensor variables
boolean lockLow = true;
boolean takeLowTime; 

//the digital pin connected to the door sensor's output
int sensorPin = 2;

//MQTT 0
WiFiClient net;
MQTTClient client;

//Time Variable
unsigned long lastMillis = 0;

//Setup pins, wifi, webserver and MQTT
void setup()
{
  //pin
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, LOW);
  //wifi
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  //mqtt
  client.begin(server, net);
  client.onMessage(messageReceived);
  //wifi/MQTT
  connect();
}

//Connect to wifi and MQTT
void connect()
{
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
  }

  while (!client.connect(mqttDeviceID))
  {
    delay(1000);
  }
  client.publish(outTopic, "CONNECTED");
}

void loop()
{
  // MQTT Loop
  client.loop();
  delay(10);

  // Make sure device is connected
  if(!client.connected())
  {
    connect();
  }
  //Sensor Detection
  if(digitalRead(sensorPin) == HIGH)
  {
    if(lockLow)
    { 
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;           
      client.publish(outTopic, "OPEN"); 
      delay(50);
    }         
    takeLowTime = true;
  }
  if(digitalRead(sensorPin) == LOW)
  {       
    if(takeLowTime)
    {
      lowIn = millis();          //save the time of the transition from high to LOW
      takeLowTime = false;       //make sure this is only done at the start of a LOW phase
    }
    //if the sensor is low for more than the given pause,
    //we assume that no more detection is going to happen
    if(!lockLow && millis() - lowIn > pause)
    { 
      //makes sure this block of code is only executed again after
      //a new detection sequence has been detected
      lockLow = true;                       
      client.publish(outTopic, "CLOSED");
      delay(50);
    }
  }

}

void messageReceived(String &topic, String &payload)
{
  //This sensor does not recieve anything from MQTT Server so this is blank
}



Any suggestions to make this work will be much appreciated.

Daniel

Re: ESP8266 door sensor not working right

PostPosted: Thu Apr 12, 2018 5:01 am
by btidey
GPIO02 is used to determine what mode the ESP8266 is in when it boots up. It must be high for the ESP8266 to run normally. Your sensor is attached to that so if it is closed (grounded) then the ESP8266 will not start correctly.

The ESP-01 has very limited IO and so makes this trickier than when using other modules. You could use the RX line as the GPIO03 instead.

Re: ESP8266 door sensor not working right

PostPosted: Sat Apr 14, 2018 9:52 pm
by danielocdh
btidey wrote:GPIO02 is used to determine what mode the ESP8266 is in when it boots up. It must be high for the ESP8266 to run normally. Your sensor is attached to that so if it is closed (grounded) then the ESP8266 will not start correctly.

The ESP-01 has very limited IO and so makes this trickier than when using other modules. You could use the RX line as the GPIO03 instead.


I've run the module with nothing connected to GPIO2, it works ok (not considering the sensor), also to flash it I use ground on GPIO0. What is the mode determined by GPIO2? How do I set GPIO2 to high?

I tried using the same code with sensorPin = 3 (for Rx) and the door sensor connected to RX instead of GPIO2, here is the result:
1 When starting the module with the door closed, it won't send any OPEN/CLOSE messages, but I can ping the module normally.
2 When starting the module with the door open it will work as expected but only once, first it will send a message "OPEN", then after I close the door it will send a message "CLOSED", after I close/open the door again nothing else will be sent, but I still can ping the module after that.