-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp_get.ino
92 lines (72 loc) · 2.67 KB
/
http_get.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* AskSensors HTTP Request
* Description: Connect ESP8266 to AskSensors over HTTP
* Author: https://asksensors.com, 2018-2019
* github: https://github.com/asksensors/AskSensors-ESP8266-API
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
// user config: TODO
const char* wifi_ssid = "..................."; // SSID
const char* wifi_password = "..................."; // WIFI
const char* apiKeyIn = "..................."; // API KEY IN
const unsigned int writeInterval = 25000; // write interval (in ms)
// ASKSENSORS config.
String host = "http://api.asksensors.com"; // ASKSENSORS API host name
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : Connect ESP8266 to AskSensors over HTTP");
Serial.println("Wait for WiFi... ");
Serial.print("********** connecting to WIFI : ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("-> WiFi connected");
Serial.println("-> IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
// wait for WiFi connection
if (WiFi.status() == WL_CONNECTED){
HTTPClient http;
Serial.print("[HTTP] begin...\n");
// Create a URL for the request
String url = "";
url += host;
url += "/write/";
url += apiKeyIn;
url += "?module1=";
url += random(10,100);
Serial.print("********** requesting URL: ");
Serial.println(url);
http.begin(url); //HTTP
Serial.println("> Request sent to ASKSENSORS");
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
Serial.println("********** End ");
Serial.println("*****************************************************");
}
delay(writeInterval); // wait for writeInterval
}