// Inclusão de bibliotecas necessárias
#include <WiFi.h> // Biblioteca Wifi-h
#include <HTTPClient.h> // Biblioteca HTTPClient.h
// Declaração de constantes
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* serverName = "http://api.thingspeak.com/update?api_key=IOEV11RXJVR71FLI";
const int relayPIN = 25;
const int sensorPIN = 32;
// Inicio de execução do programa, após a declaração das constantes!
void setup() {
Serial.begin(115200);
pinMode(sensorPIN, INPUT);
pinMode(relayPIN, OUTPUT);
digitalWrite(relayPIN, LOW);
WiFi.begin(ssid, password);
Serial.print("Conectando-se ao Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Conectado ao Wi-Fi com sucesso!");
}
int controlIrrigation(int moisture) {
if (moisture < 2000) { // Caso a umidade esteja abaixo de 2000, deve-se ligar o LED!
digitalWrite(relayPIN, HIGH);
return 1;
} else {
digitalWrite(relayPIN, LOW); // Se a umidade NÃO estiver abaixo de 2000, deve-se desligar o LED!
return 0;
}
}
void loop() {
int moisture = analogRead(sensorPIN); // Leitura do valor simulado do potênciometro
Serial.print("Valor do sensor de umidade (simulado): ");
Serial.println(moisture);
int irrigationStatus = controlIrrigation(moisture);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String serverPath;
serverPath.concat(serverName);
serverPath.concat("&field1=");
serverPath.concat(String(irrigationStatus)); // Parte que envia o status do irrigador
serverPath.concat("&field2=");
serverPath.concat(String(moisture)); // Parte que envia o valor simulado do potenciômetro
http.begin(serverPath.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Erro ao enviar solicitação GET: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Wi-Fi desconectado!");
}
delay(15000); // Pausa ou atraso de 15000 milissegundos
}