-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrogue.ino
65 lines (56 loc) · 1.63 KB
/
drogue.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
#include <WiFi.h>
#include <HTTPClient.h>
char WIFI_SSID[] = "XXX";
char WIFI_PSK[] = "XXX";
// User name is <device>@<application>
char DROGUE_USER[] = "device1@example-app";
char DROGUE_PASS[] = "hey-rodney";
char DROGUE_URL[] = "https://http.sandbox.drogue.cloud/v1/pico?ct=10";
WiFiClient client;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
int status;
Serial.println("Connecting to WiFi AP...");
while (status != WL_CONNECTED) {
status = WiFi.begin(WIFI_SSID, WIFI_PSK);
delay(10000);
}
Serial.println("Connected to WiFi!");
}
void loop() {
HTTPClient https;
https.setInsecure();
Serial.println("Reporting temperature...");
char payload[64];
float temp = analogReadTemp();
snprintf(payload, 64, "{\"temp\":%f}", temp);
if (https.begin(DROGUE_URL)) {
https.setAuthorization(DROGUE_USER, DROGUE_PASS);
https.addHeader("Content-type", "application/json");
int httpCode = https.POST(payload);
if (httpCode > 0) {
Serial.println("HTTP request sent");
if (httpCode == HTTP_CODE_OK) {
const String& payload = https.getString();
if (payload == "{\"led\":\"on\"}") {
Serial.println("TURN ON");
digitalWrite(32, 1);
} else {
Serial.println("TURN OFF");
digitalWrite(32, 0);
}
} else {
Serial.printf("Received HTTP code %d: %s\n", httpCode, https.errorToString(httpCode).c_str());
}
} else {
Serial.printf("HTTP POST failed: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.println("Error starting HTTP request");
}
delay(5000);
}