-
Notifications
You must be signed in to change notification settings - Fork 1
/
thingspeak-client.cpp
30 lines (22 loc) · 1.08 KB
/
thingspeak-client.cpp
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
#include <ESP8266HTTPClient.h> // To send HTTP requests.
#include "thingspeak-client.h"
void ThingSpeakClient::setApiKey(String apiKey) {
_apiKey = apiKey;
}
void ThingSpeakClient::writeField(String field1Value, String field2Value, String field3Value, String statusMessage) {
String url = String("http://api.thingspeak.com/update");
Serial.println("ThingSpeakClient: Sending POST request to " + url);
String requestBody = "api_key=" + _apiKey + "&field1=" + field1Value + "&field2=" + field2Value + "&field3=" + field3Value + "&status=" + statusMessage;
Serial.println("ThingSpeakClient: HTTP request body: " + requestBody);
WiFiClient wifiClient;
HTTPClient http;
http.begin(wifiClient, url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int statusCode = http.POST(requestBody);
Serial.printf("ThingSpeakClient: Received HTTP status code: %d\r\n", statusCode);
if (statusCode != HTTP_CODE_OK) {
String responseBody = http.getString();
Serial.println("ThingSpeakClient: Received HTTP response body: " + responseBody);
}
http.end();
}