-
Notifications
You must be signed in to change notification settings - Fork 24
/
ThingSpeak
69 lines (59 loc) · 1.58 KB
/
ThingSpeak
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
const char* host = "api.thingspeak.com";
String url = "/update?api_key=your_api_key";
const int httpPort = 80;
int interval = 60000;
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "your_ssid";
const char* password = "your_password";
String working() {
r = 50; // do some magic here!
return(String("field1=")+String(r));
}
void delivering(String payload) {
WiFiClient client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpPort)) {
Serial.print("connection failed: ");
Serial.println(payload);
return;
}
String getheader = "GET "+ String(url) +"&"+ String(payload) +" HTTP/1.1";
client.println(getheader);
client.println("User-Agent: ESP8266 Kyuho Kim");
client.println("Host: " + String(host));
client.println("Connection: close");
client.println();
Serial.println(getheader);
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
Serial.println("Done cycle.");
}
void connect_ap() {
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\n Got WiFi, IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
connect_ap();
Serial.println("ESPArduinoThingSpeak.cpp - 2017/3/15");
}
unsigned long mark = 0;
void loop() {
if (millis() > mark ) {
mark = millis() + interval;
String payload = working();
delivering(payload);
}
}