http://randomnerdtutorials.com/door-sta ... e-esp8266/
I am sure the code could be modified but is beyond my skills. thanks in advance
Explore... Chat... Share...
/*
Created by Rui Santos
All the resources for this project:
http://randomnerdtutorials.com/
Based on some ESP8266 code examples
*/
#include <ESP8266WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* host = "maker.ifttt.com";
const char* apiKey = "YOUR_IFTTT_API_KEY";
int pin1 = 2;
int pin2 = 3;
volatile int state1 = false;
volatile int flag1 = false;
const char* door_state1 = "closed";
volatile int state2 = false;
volatile int flag2 = false;
const char* door_state2 = "closed";
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
const long interval = 3000;
void changeDoorStatus1() {
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 >= interval) {
previousMillis1 = currentMillis1;
state1 = !state1;
if(state1) {
door_state1 = "opened";
}
else{
door_state1 = "closed";
}
flag1 = true;
Serial.println(state1);
Serial.println(door_state1);
}
}
void changeDoorStatus2() {
unsigned long currentMillis2 = millis();
if(currentMillis2 - previousMillis2 >= interval) {
previousMillis2 = currentMillis2;
state2 = !state2;
if(state2) {
door_state2 = "opened";
}
else{
door_state2 = "closed";
}
flag2 = true;
Serial.println(state2);
Serial.println(door_state2);
}
}
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Preparing the Door Status Monitor project...");
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pin1), changeDoorStatus1, CHANGE);
attachInterrupt(digitalPinToInterrupt(pin2), changeDoorStatus2, CHANGE);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if(flag1){
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/door_status1/with/key/";
url += apiKey;
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state1 + "\r\n");
flag1 = false;
}
delay(10);
if(flag2){
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/door_status2/with/key/";
url += apiKey;
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state2 + "\r\n");
flag2 = false;
}
delay(10);
}
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]