-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32_example.cpp
86 lines (75 loc) · 1.94 KB
/
esp32_example.cpp
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
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "internet-box-name";
const char* password = "internet-box-password";
const char* serverName = "http://your.server.ip/php_example.php";
float temperature;
float humidite;
float pression;
float latitude;
float longitude;
void setup() {
temperature = 20.0;
humidite = 30.0;
pression = 1010.0;
latitude = 43.6044622;
longitude = 1.4442469;
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (humidite < 60)
{
temperature += 0.1;
humidite += 0.1;
pression += 0.1;
latitude += 0.01;
longitude -= 0.01;
}
else
{
temperature = 20.0;
humidite = 30.0;
pression = 1010.0;
latitude = 43.6044622;
longitude = 1.4442469;
}
if(WiFi.status()== WL_CONNECTED){
digitalWrite(LED, HIGH);
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare HTTP post
String httpRequestData = "temperature=" + String(temperature) + "&humidite=" + String(humidite)
+ "&pression=" + String(pression) + "&latitude=" + String(latitude)
+ "&longitude=" + String(longitude) + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP post
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
delay(45000);
}