#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String payload;
WiFiClient IOT;
HTTPClient http;
const char* serverURL = "https://eff1-187-182-196-183.ngrok-free.app/Energia";
String ClientId = "10";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Conectando ao WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConectado ao WiFi!");
pinMode(32, INPUT);
pinMode(33, INPUT);
pinMode(35, INPUT);
}
void loop() {
//Dados para serem incluidos no json:
int produzidoRaw = analogRead(35);
int gastoRaw = analogRead(32);
int distribuidoRaw = analogRead(33);
float produzidoWh = map(produzidoRaw, 0, 4095, 0, 100); // Mapear de 0 a 100 Wh
float gastoWh = map(gastoRaw, 0, 4095, 0, 100); // Mapear de 0 a 100 Wh
float distribuidoWh = map(distribuidoRaw, 0, 4095, 0, 100); // Mapear de 0 a 100 Wh
float armazenadoWh = produzidoWh - gastoWh - distribuidoWh;
if (WiFi.status() == WL_CONNECTED) {
//Header do post
http.begin(IOT, serverURL);
http.addHeader("Content-Type", "application/json");
String httpRequestData = "{\"IdUser\": \"" + String(ClientId) +
"\",\"Produzido\": \"" + String(produzidoWh) +
"\",\"Gasto\": \"" + String(gastoWh) +
"\",\"Armazenado\": \"" + String(armazenadoWh) +
"\",\"Distribuido\": \"" + String(distribuidoWh) + "\"}";
Serial.println(httpRequestData);
//Conexão falha devido problema no certificado digital(seria necessário pagar para hospedar a api em um http na web)
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String response = http.getString();
Serial.println("Resposta do servidor:");
Serial.println(response);
} else {
Serial.print("Erro no envio da requisição, código: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Falha na conexão WiFi.");
}
delay(10000);//10 segundos para cada post e consulta dos dados
}