/
Notify_Flag_v2.ino
145 lines (123 loc) · 3.82 KB
/
Notify_Flag_v2.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <ArduinoJson.h> // Install this library from Sketch->Include Library->Libray Manager
#include <Servo.h>
#include <ESP8266WiFi.h>
const char* THIG_NAME = "SparksESP"; // Put your thing name here
const char* key = "Servo1"; // Put your key name here
const char* WIFISSID = "SSID"; // Put your WiFi SSID here
const char* PASSWORD = "PASSWORD"; // Put your WiFi password here
int servoPin = 2; // This is the pin to which your servo is attached to
const char* host = "dweet.io";
String ans;
String response;
uint8_t bodyPosinit;
uint8_t bodyPosend;
String dweetKey ="\"";
int length = dweetKey.length();
Servo mailFlag;
void setup() {
mailFlag.attach(servoPin);
Serial.begin(115200);
Serial.println("Flag Reset");
mailFlag.write(0);
Serial.print("Connecting to : ");
Serial.println(WIFISSID);
WiFi.begin(WIFISSID,PASSWORD);
while(WiFi.status()!= WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
Serial.println(WiFi.localIP());
ans="False";
dweetKey += key;
dweetKey += "\":";
}
void loop() {
receiveDweet();
printValue();
Serial.println(ans);
if(ans=="True")
{
Serial.println("Flag On!");
mailFlag.write(90);
sendDweet();
}
}
void sendDweet()
{
Serial.print("Setting ");
Serial.print(key);
Serial.print(" of ");
Serial.print(THIG_NAME);
Serial.println(" to FALSE.");
WiFiClient client;
const int httpPort = 80;
Serial.print("Connecting to : ");
Serial.println(host);
if (!client.connect(host, httpPort)) {
Serial.println("Connection Failed");
return;
}
Serial.println("Connected Successfully!");
client.print(String("GET /dweet/for/"));
client.print(THIG_NAME);
client.print(String("?"));
client.print(key);
client.print(String("=False HTTP/1.1\r\n Host: dweet.io \r\n Connection: close\r\n\r\n"));
delay(10);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
Serial.println("\nClosing Connection");
Serial.println("Going to Sleep for 10 min");
ESP.deepSleep(600*1000000);
}
Serial.println("\nClosing Connection");
}
void receiveDweet()
{
Serial.print("Receiving Data for ");
Serial.println(THIG_NAME);
WiFiClient client;
const int httpPort = 80;
Serial.print("Connecting to : ");
Serial.println(host);
if (!client.connect(host, httpPort)) {
Serial.println("Connection Failed");
return;
}
Serial.println("Connected Successfully!");
client.print(String("GET /get/latest/dweet/for/"));
client.print(THIG_NAME);
client.print(String(" HTTP/1.1\r\n Host: dweet.io \r\n Connection: close\r\n\r\n"));
delay(10);
while(client.available()){
response = client.readStringUntil('\r');
Serial.print(response);
}
Serial.println("\nClosing Connection");
}
void printValue()
{
bodyPosinit =4+ response.indexOf("\r\n\r\n");
response = response.substring(bodyPosinit);
Serial.println(response);
bodyPosinit =10+ response.indexOf("\"content\"");
bodyPosend = response.indexOf("}]}");
response = response.substring(bodyPosinit,bodyPosend);
Serial.println(response);
if (response.indexOf(dweetKey) == -1) {
ans = "Key not found";
} else {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(response);
if (!root.success()){
Serial.println("parseObject() failed");
ans = "parse error.";
} else {
const char* val = root[key];
ans = String(val);
}
}
}