Hi,
I am building a door sensor using a 3-pin switch, an ESP2866-01 (standalone) for communication to a server and a battery as power supply. (I'm using theArduino IDE for ESP 8266 to program the ESP2866 directly without the need to control it using a regular Arduino.)
Requirements of the setup are:
- door open and close events should be send to the server. (Door open is powering on of ESP2866, door close can be detecting by change of switch state.
- power needs to be turned off when (or shortly after) the door is closed and the events are send to server, so the battery will last long.
I have build the attached circuit and use the following code
#include <ESP8266WiFi.h>
const char* ssid = "#####";
const char* password = "#####";
unsigned long wakeTime;
void software_Reboot(){
//following not working yet (error code: "unknown opcode or format name 'jmp'"
//asm volatile (" jmp 0");
}
void setup() {
//1) set pin 0 to high, to keep power even if lever switch opens again
pinMode (0,OUTPUT);
digitalWrite(0,HIGH);
//2) to watch current status of manual switch
pinMode(2,INPUT);
//3) set serial
Serial.begin(9600);
delay(10);
WiFi.begin(ssid, password);
//for speed we set esp8266 ip, gateway and subnet manually
WiFi.config({##,##,##,##},{##,##,##,##},{##,##,##,##});
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
WiFiClient client;
const String host = "##,##,##,##";
const byte hostip[]={##,##,##,##};
const String clientId="######";
int loggedOpen=false;
int loggedClose=false;
int failed=0;
void loop() {
//if loggedClose then waiting for shutdown to finish
if (loggedClose) return;
//check if we logged open door and door is not yet closed
int cStatus=digitalRead(2);
if (loggedOpen){
if (cStatus==LOW) { //low means door is open
delay(2000);
return;
}
}
// Use WiFiClient class to create TCP connections
const int httpPort = 80;
if(!client.connected()){
if (!client.connect(hostip, httpPort)) {
Serial.println("Connection failed");
delay (500);
failed++;
if (failed>10){
Serial.println("Too many connection attempts, resetting...");
software_Reboot();
}
return;
}else{
Serial.println("Connection succeeded");
}
}
// We now create a URI for the request
//String url = "/index.html?Wifi1";
String url = "/index.php?";
url=url+"elapsed="+(millis()/1000);
url=url+"&";
if (!loggedOpen){
url=url+clientId+"=1";
loggedOpen=true;
Serial.println("Handle Log OPEN");
}else{ //status of door should be closed
url=url+clientId+"=0";
loggedClose=true;
Serial.println("Handle Log CLOSE");
}
Serial.print("Requesting URL: ");
Serial.println(url);
client.println("GET "+url+" HTTP/1.1");
client.println("Host: "+host);
client.println("User-Agent: ESP8266");
client.println("Connection: close");
client.println("");//needed as signal that block has ended
delay(10);//give some time to process
//we must read data to clear buffer
unsigned long time=0;
Serial.println("Start Response");
while (client.available()) {
char c = client.read();
Serial.write(c);
}
Serial.println("End Response");
//if door is closed we can shut down by setting pin 0 to LOW
if (loggedClose){
Serial.println("Shutdown");
digitalWrite(0,LOW);
}
}
As you can see I set pin 0 to high to keep power on (circumventing the switch in case door is already closed) however this does not seem to work. Am I doing something wrong?
(To be complete, I also have two other problems: the error code generated in the reset method 'unknown opcode jmp' but this could be specific for the ESP2866 IDE environment; Also pin 2 is not registering the closing of the door / receiving 5V )
Hope anyone can help!
thx, Nard