Basically you can have two interrupts, and also two ways of checking if the value is changed, it is not the best way to do it (in terms of lines of code and memory used) but it will do the job, now you only have to make two IFTT sequences that listen to another URL...
Code: Select all/*
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);
}