/****************************************************
* 1) BIBLIOTECAS OBRIGATÓRIAS
****************************************************/
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
/****************************************************
* 2) CONFIGURAÇÃO DO OLED
****************************************************/
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
/****************************************************
* 3) PINOS DO TEU CIRCUITO (IGUAL À IMAGEM)
****************************************************/
#define PIN_OLED_SDA 21
#define PIN_OLED_SCL 22
#define PIN_DHT 15
#define DHTTYPE DHT22
#define PIN_DS18B20 4
#define PIN_SOLO 33
#define PIN_CHUVA 36
#define PIN_RELE 26
#define PIN_PIR 13
#define PIN_BUZZER 19
/****************************************************
* 4) OBJETOS DOS SENSORES
****************************************************/
DHT dht(PIN_DHT, DHTTYPE);
OneWire oneWire(PIN_DS18B20);
DallasTemperature ds18b20(&oneWire);
/****************************************************
* 5) VARIÁVEIS
****************************************************/
unsigned long lastRead = 0;
const unsigned long interval = 1500;
float tempAr = 0;
float umidadeAr = 0;
float tempSolo = 0;
float soloPercent = 0;
float chuvaPercent = 0;
/****************************************************
* 6) CONFIGURAÇÃO DO WIFI (AQUI COLOCAS A TUA REDE)
****************************************************/
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// URL DA TUA API
String apiURL = "http://localhost:3000";
/****************************************************
* 7) FUNÇÃO → ENVIAR DADOS PARA O DASHBOARD (POST /update)
****************************************************/
void enviarDados() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiURL + "/update");
http.addHeader("Content-Type", "application/json");
String json = "{";
json += "\"humidity1\":" + String(umidadeAr) + ",";
json += "\"temperature1\":" + String(tempAr) + ",";
json += "\"humidity2\":" + String(soloPercent) + ",";
json += "\"temperature2\":" + String(tempSolo);
json += "}";
http.POST(json);
http.end();
}
}
/****************************************************
* 8) FUNÇÃO → LER ESTADO DA ELETROVÁLVULA (GET /electrovalve)
****************************************************/
bool lerComandoRega() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiURL + "/electrovalve");
int code = http.GET();
if (code == 200) {
String payload = http.getString();
http.end();
return payload.indexOf("on") > 0;
}
http.end();
}
return false;
}
/****************************************************
* 9) SETUP COMPLETO
****************************************************/
void setup() {
Serial.begin(115200);
// WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
}
Serial.println("\nWiFi conectado!");
// OLED
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Sistema Iniciado");
display.display();
// Sensores
dht.begin();
ds18b20.begin();
pinMode(PIN_RELE, OUTPUT);
pinMode(PIN_PIR, INPUT);
pinMode(PIN_BUZZER, OUTPUT);
digitalWrite(PIN_RELE, HIGH); // desligado
digitalWrite(PIN_BUZZER, LOW);
}
/****************************************************
* 10) LOOP PRINCIPAL
****************************************************/
void loop() {
unsigned long now = millis();
if (now - lastRead >= interval) {
lastRead = now;
// Leitura DHT22
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t)) tempAr = t;
if (!isnan(h)) umidadeAr = h;
// Leitura DS18B20
ds18b20.requestTemperatures();
float temp = ds18b20.getTempCByIndex(0);
if (temp != DEVICE_DISCONNECTED_C) tempSolo = temp;
// Sensor de solo
soloPercent = (analogRead(PIN_SOLO) / 4095.0) * 100.0;
// Sensor de chuva
chuvaPercent = 100.0 - ((analogRead(PIN_CHUVA) / 4095.0) * 100.0);
// Lógica automática
bool soloSeco = soloPercent > 60;
bool chovendo = chuvaPercent > 60;
bool temperaturaAlta = tempAr > 30;
bool bombaAuto = soloSeco && !chovendo && temperaturaAlta;
// Lógica remota (dashboard)
bool bombaRemota = lerComandoRega();
// Prioridade: comando remoto > automático
bool ligarBomba = bombaRemota ? true : bombaAuto;
digitalWrite(PIN_RELE, ligarBomba ? LOW : HIGH);
// Buzzer com PIR
if (digitalRead(PIN_PIR)) {
digitalWrite(PIN_BUZZER, HIGH);
delay(50);
digitalWrite(PIN_BUZZER, LOW);
}
// Enviar dados para API
enviarDados();
// Atualizar OLED
display.clearDisplay();
display.setCursor(0, 0);
display.println("Agricultura Inteligente");
display.println("-----------------------");
display.print("Temp Ar: ");
display.print(tempAr);
display.println(" C");
display.print("Temp Solo: ");
display.print(tempSolo);
display.println(" C");
display.print("Solo: ");
display.println(soloSeco ? "SECO" : "MOLHADO");
display.print("Chuva: ");
display.println(chovendo ? "CHOVENDO" : "SECO");
display.print("Bomba: ");
display.println(ligarBomba ? "ATIVADA" : "OFF");
display.display();
}
}
OLED
Solo (potenciómetro)
DHT22
DS18B20
Buzzer
Réle
LDR
PIR