-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromethues.ino
116 lines (85 loc) · 2.31 KB
/
promethues.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
/**
Promethues_Pager
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#define WIFISSID "YOUR WIFI SSID"
#define PASSWORD "WIFI PASSWORD"
String URLADDR = "http://192.168.1.10:9090/api/v1/alerts"; //promthues address
int currentStateSize;
int newStateSize;
ESP8266WiFiMulti WiFiMulti;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(WIFISSID, PASSWORD);
}
void fireAlert(){
for (uint8_t t = 30; t > 0; t--) {
Serial.println("fire ....");
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
}
}
bool isStateChanged(){
if (currentStateSize == 0){
currentStateSize = newStateSize;
}
if (newStateSize != currentStateSize){
currentStateSize = newStateSize;
return true;
} else{
return false;
}
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, URLADDR)) {
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// found at server
if (httpCode == HTTP_CODE_OK ) {
//String payload = http.getString();
newStateSize = http.getSize();
if (isStateChanged()){
fireAlert();
}else {
Serial.println("state not changed..");
}
Serial.println(currentStateSize);
Serial.println(newStateSize);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
delay(10000);
}