Hi, i’m using the ESP8266 NodeMCU Amica with Thinger.io to create a WiFi controlled alarm. This alarm uses 2 magnetic door sensors which are connected as an input to the digital pins D3 and D5. If both sensors are LOW (the doors are closed), the output of a one-channel relay is also LOW (NO), but if only one of the sensors is HIGH (D3 or D5, one door opened) the relay output must be in HIGH state and this activates a 24V buzzer. This program has to be executed if the D0 is LOW (The bottom LED in the ESP8266 is used as an indicator for the ON state of the alarm), so the alarm can be activated remotely using the thinger.io dashboard and you also could see the sensors state. If an intruder is detected, the integrated upper LED in the D4 GPIO changes to LOW (The LED is now ON). The alarm can also be deactivated turning the D0 to HIGH in the dashboard (The bottom LED is OFF) and this produces changes in the relay output (now LOW, so the buzzer is OFF) and sets the upper led state (now OFF).
For some reason, i can’t read the digital inputs of D3 and D5, i think it could be due an error in the code, if someone could explain me what i’m doing wrong it would be very usefull:
#include <ThingerESP8266.h>
#define USERNAME "XXXXXXXXXXXX"
#define DEVICE_ID "XXXXXXXXXXXX"
#define DEVICE_CREDENTIAL "XXXXXXXXXXXXXX"
#define SSID "XXXXXXXXXXXXXXXX"
#define SSID_PASSWORD "XXXXXXXXXXXXXX"
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
#define int D0 = 16;
#define int D1 = 5;
#define int D2 = 4;
#define int D3 = 0;
#define int D4 = 2;
#define int D5 = 14;
#define int D6 = 12;
#define int D7 = 13;
#define int D8 = 15;
#define int D9 = 3;
#define int D10 = 1;
void setup() {
pinMode(D0, OUTPUT); //BOTTOM LED
pinMode(D4, OUTPUT); //UPPER LED
pinMode(D1, OUTPUT); //RELAY OUTPUT 1
pinMode(D2, OUTPUT); //RELAY OUTPUT 2
pinMode(D3, INPUT); //MAGNETIC DOOR SENSOR 1
pinMode(D5, INPUT); //MAGNETIC DOOR SENSOR 2
digitalWrite(D0, HIGH); //TURN OFF BOTTOM LED AT THE START
digitalWrite(D4, HIGH); //TURN OFF UPPER LED AT THE START
thing.add_wifi(SSID, SSID_PASSWORD);
thing["led_0"] << digitalPin(D0);
thing["led_1"] << digitalPin(D4);
thing["out_0"] << digitalPin(D1);
thing["out_1"] << digitalPin(D2);
thing["in_0"] >> digitalPin(D3);
thing["in_1"] >> digitalPin(D5);
}
void loop() {
thing.handle();
if (digitalRead(D0) == HIGH) { //ALARM DEACTIVATED
digitalWrite(D1,LOW); //TURN OFF THE RELAY OUTPUT
digitalWrite(D4,HIGH); //TURN OFF THE UPPER LED OF THE NODEMCU
}
if (digitalRead(D0) == LOW) { //ALARM ACTIVATED
if ((digitalRead(D3) == HIGH)||(digitalRead(D5) == HIGH)){ //INTRUDER DETECTED BY MAGNETIC DOOR SENSORS
digitalWrite(D1,HIGH); //TURN ON THE RELAY OUTPUT (BUZZER ON)
digitalWrite(D4,LOW); //TURN ON THE UPPER LED OF THE NODEMCU
}
}
}