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:
#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