forked from SpotifaiI/parkify-sense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp.ino
70 lines (54 loc) · 1.67 KB
/
esp.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
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "iPhone Raiz";
const char* password = "Coxinha123";
const char* serverName = "http://192.168.147.137:8001";
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Conectado ao Wi-Fi!");
Serial.println("ESP32 pronto para enviar dados!");
Serial.println(WiFi.localIP());
}
void checkWiFiConnection() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Reconectando ao Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Reconectado!");
}
}
void loop() {
checkWiFiConnection();
if (Serial.available()) {
String dado = Serial.readStringUntil('\n');
Serial.println("Recebido do Arduino: " + dado);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = dado;
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String resposta = http.getString();
Serial.println("Código de resposta HTTP: " + String(httpResponseCode));
Serial.println("Resposta do servidor: " + resposta);
} else {
Serial.println("Erro ao enviar POST: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("Wi-Fi Desconectado");
}
}
delay(100);
}