-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP8266HTTP.ino
73 lines (55 loc) · 1.55 KB
/
ESP8266HTTP.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
/*
* ESP8266 HTTP Client
*/
#ifndef _include_libs_arduino_
#define _include_libs_arduino_
#include <Arduino.h>
#include <ESP8266WiFi.h>
#endif
#include "httprequest.h"
#include "httpresponse.h"
#include "secrets.h"
void setup() {
Serial.begin(115200);
delay(1000);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(S_WLAN_SSID);
WiFi.begin(S_WLAN_SSID, S_WLAN_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
/* instantiate HttpRequest */
HttpRequest httpRequest;
HttpResponse httpResponse;
/* values to send */
int value1, value2;
value1 = (value1 + 1) % 100;
value2 = random(0, 100);
/* configuration */
/* * http request type */
httpRequest.setMethod(HttpRequest::POST);
/* * host * */
httpRequest.setHost("api.thingspeak.com");
/* * location * */
httpRequest.setLocation("/update");
/* * headers * */
httpRequest.addHeader("X-ThingSpeakAPIKey", S_THINGSPEAK_API_KEY);
httpRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
/* request body */
httpRequest.setBody("field1=" + String(value1) + "&field2=" + String(value2));
/* make the request */
httpResponse = httpRequest.makeRequest();
/* print out the response */
httpResponse.printResponse();
/* thingspeak likes 15 second pauses between request per channel */
delay(15000);
}