-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_get.ino
92 lines (70 loc) · 2.25 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
/*
Connect ESP32 to AskSensors
* Description: This sketch connects to a website (https://asksensors.com) using an ESP32 Wifi module.
* Author: https://asksensors.com, 2018
* github: https://github.com/asksensors
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
WiFiMulti WiFiMulti;
HTTPClient ask;
// TODO: user config
const char* ssid = "............."; //Wifi SSID
const char* password = "............."; //Wifi Password
const char* apiKeyIn = "..................."; // API KEY IN
const unsigned int writeInterval = 25000; // write interval (in ms)
// ASKSENSORS API host config
const char* host = "api.asksensors.com"; // API host name
const int httpPort = 80; // port
void setup(){
// open serial
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : Connect ESP32 to AskSensors.");
Serial.println("Wait for WiFi... ");
// connecting to the WiFi network
WiFiMulti.addAP(ssid, password);
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
// connected
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}else {
// Create a URL for updating module1 and module 2
String url = "http://api.asksensors.com/write/";
url += apiKeyIn;
url += "?module1=";
url += random(10, 100);
url += "&module2=";
url += random(10, 100);
Serial.print("********** requesting URL: ");
Serial.println(url);
// send data
ask.begin(url); //Specify the URL
//Check for the returning code
int httpCode = ask.GET();
if (httpCode > 0) {
String payload = ask.getString();
Serial.println(httpCode);
Serial.println(payload);
} else {
Serial.println("Error on HTTP request");
}
ask.end(); //End
Serial.println("********** End ");
Serial.println("*****************************************************");
}
client.stop(); // stop client
delay(writeInterval); // delay
}