/* ESP32 WiFi Scanning example */
#include "DHTesp.h"
#include <HTTPClient.h>
#include <ESPSupabase.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <ArduinoJson.h>
const int RELAY_PIN = 0;
const int DHT_PIN = 22;
DHTesp dhtSensor;
Supabase sb;
String supabase_url = "https://axnauvcnfkteiprxmtop.supabase.co";
String anon_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImF4bmF1dmNuZmt0ZWlwcnhtdG9wIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MjI0NzEyOTQsImV4cCI6MjAzODA0NzI5NH0.3mzTTwi-Z9JgJWZOTGHy-lEFmbMdSXQ3EM-nolwjzxg";
String table = "tempH";
bool upsert = false;
void setup() {
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.begin(115200);
Serial.println("Inicializando WiFi...");
WiFi.mode(WIFI_STA);
Serial.println("Setup feito!");
Serial.println("Procurando...");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Procura feita!");
if (n == 0) {
Serial.println("Redes não foram encontradas. Tentando novamente");
while (n == 0)
Serial.println(". ");
delay(0.1);
n = WiFi.scanNetworks();
} else {
Serial.println();
Serial.print(n);
Serial.println(" redes disponíveis!");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
Serial.print("Connectando a ");
Serial.print(WiFi.SSID(i));
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Conectado!");
}
}
Serial.println("");
}
void loop() {
// Wait a bit before scanning again
delay(5000);
// read temperature
float temp = dhtSensor.getTemperature();
// read humidity
float humi = dhtSensor.getHumidity();
// read temperature in Fahrenheit
if ( isnan(temp) || isnan(humi)) {
Serial.println("Failed to read from DHT22 sensor!");
} else {
Serial.println("Temp: " + String(temp, 2) + "°C");
Serial.println("Umidade: " + String(humi, 1) + "%");
Serial.println("---");
if (temp > 24 || humi < 45){
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Corrente acionada");
delay(1000);
} else {
// Normally Open configuration, send HIGH signal stop current flow
// (if you're usong Normally Closed configuration send LOW signal)
digitalWrite(RELAY_PIN, LOW);
Serial.println("Corrente inativa");
}
delay(2000); // wait 2 seconds between readings (DHT22 has ~0.5Hz sample rate)
}
// + DB Sb, API, APP
// Put your JSON that you want to insert rows
// You can also use library like ArduinoJson generate this
String JSON = "{\"valor_tump\": " + String(temp) + ", \"humi\": " + String(humi) + "}";
// Beginning Supabase Connection
sb.begin(supabase_url, anon_key);
// Uncomment this line below, if you activate RLS in your Supabase Table
// sb.login_email("email", "password");
// You can also use
// db.login_phone("phone", "password");
int code = sb.insert(table, JSON, upsert);
Serial.println(code);
sb.urlQuery_reset();
}