-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNodeMCU.ino
97 lines (73 loc) · 2.15 KB
/
NodeMCU.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//#include <ESP8266WiFi.h>
// Thnger.io Specifications
#include <ThingerESP8266.h>
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5); //RX, TX
#include <ArduinoJson.h>
#define USERNAME "your_username"
#define DEVICE_ID "thinger_device_id"
#define DEVICE_CREDENTIAL "thinger_device_credentials"
#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_password"
float t = 0;
float h = 0;
float lpg = 0;
float smk = 0;
int fire;
const char* flame_detected;
// Setup device details
ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
// Setup WiFi
thing.add_wifi(SSID, SSID_PASSWORD);
Serial.begin(115200);
s.begin(115200);
while (!Serial) continue;
// digital pin control example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
thing["led"] << digitalPin(LED_BUILTIN);
// resource output example (i.e. reading a sensor value)
thing["millis"] >> outputValue(millis());
thing["jsonData"] >> [](pson& out) {
out["temperature"] = t;
out["humidity"] = h;
out["lpg"] = lpg;
out["smoke"] = smk;
out["fire"] = flame_detected;
};
}
unsigned long lastCheck = 0;
void loop() {
// put your main code here, to run repeatedly:
thing.handle();
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(s);
if (root == JsonObject::invalid())
{
return;
}
t = root["temperature"];
h = root["humidity"];
lpg = root["lpg"];
smk = root["smoke"];
fire = root["fire"];
if(fire == 1) {
flame_detected = "NO";
}else {
flame_detected = "YES";
}
unsigned long currentTS = millis();
Serial.println(lastCheck);
Serial.println(currentTS);
Serial.println(currentTS-lastCheck);
if(currentTS-lastCheck>=1*60*1000) {
lastCheck=currentTS;
if (fire == 0) {
// Call thinger.io email endpoint to send an email to address
thing.call_endpoint("SIAJ");
thing.call_endpoint("AYMEN");
Serial.println(fire);
Serial.println("Endpoint called");
}
}
}