-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
70 lines (49 loc) · 1.49 KB
/
main.cpp
File metadata and controls
70 lines (49 loc) · 1.49 KB
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
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define ONE_WIRE_BUS 4 // GPIO pin
const char* ssid = "";
const char* password = "";
int wiFiConnectTimeout = 10; //seconds
int sensorReadDelay = 60*15; //seconds
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup(){
Serial.begin(115200);
sensors.begin();
sensors.requestTemperatures();
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
int wiFiConnectAttempts = 0;
while(WiFi.status() != WL_CONNECTED && wiFiConnectAttempts++ < wiFiConnectTimeout) {
Serial.print(".");
delay(1000);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println(" FAILED!");
} else {
Serial.println(" OK!");
Serial.print("Sending API request...");
const String metricsServerUrl = "http://metrics-server/";
HTTPClient http;
http.setTimeout(3000);
http.setConnectTimeout(3000);
http.begin(metricsServerUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "instance=a&sensor[0]=" + String(sensors.getTempCByIndex(0));
int httpResponseCode = http.POST(httpRequestData);
http.end();
if (httpResponseCode >= 200) {
Serial.println(" OK!");
} else {
Serial.println(" FAIL!");
}
WiFi.disconnect(true, true);
}
Serial.println("Zzzzz");
esp_deep_sleep(sensorReadDelay * 1000000);
}
void loop(){
}