-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESP8266_wifi.ino
62 lines (50 loc) · 1.37 KB
/
ESP8266_wifi.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
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
const char* ssid = "Makerspace-iLab";
const char* password = "";
long counter;
long readIntFromSerial()
{
long message;
if(Serial.available() > 0) //Check serial buffer for available data.
{
message = Serial.parseInt(); //Look for integer in serial buffer, if timeout after 60seconds, Serial.parseInt will return 0.
return message;
}
else
{
return -1;
}
}
void setup() {
Serial.begin(115200);
while(!Serial)
{
; //Wait for serial port to connect. Needed for native USB
}
Serial.setTimeout(60000L); //Set timeout to 60 seconds.
// Connect to WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.println(".");
delay(300);
//Wait for Wi-Fi to connect.
}
}
void loop()
{
counter = readIntFromSerial();
if(counter >= 0)
{
if(WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
String jsonCounter = "{\"traffic\": \"" + String(counter) + "\"}"; //Package into json format.
HTTPClient http; //Declare object of class HTTPClient
http.begin("http://192.168.0.188:5000/"); //Specify request destination
http.addHeader("Content-Type", "application/json"); //Specify content-type header
http.POST(jsonCounter); //Send the request
http.end(); //Close connection
}
}
}