Veja abaixo um exemplo em que foi implementado uma leitura do sensor DHT22 e 5 botões (interruptores).
Código completo:
#include <Arduino.h> #include <WiFi.h> #include <ESPAsyncWebServer.h> #include <ESPDash.h> #include <DHTesp.h> #include <Ticker.h> const int sensorPin = 17; DHTesp dht; int led_blue = 2; Ticker periodicTicker; const char* ssid = "ID"; const char* password = "senha"; AsyncWebServer server(80); ESPDash dashboard(&server); Card temperature(&dashboard, TEMPERATURE_CARD, "Temperature", "°C"); Card humidity(&dashboard, HUMIDITY_CARD, "Humidity", "%"); Card botao_1(&dashboard, BUTTON_CARD, "botao_1"); Card botao_2(&dashboard, BUTTON_CARD, "botao_2"); Card botao_3(&dashboard, BUTTON_CARD, "botao_3"); Card botao_4(&dashboard, BUTTON_CARD, "botao_4"); Card botao_5(&dashboard, BUTTON_CARD, "botao_5"); void updateCards() { float temp = dht.getTemperature(); float hum = dht.getHumidity(); temperature.update(temp); humidity.update(hum); dashboard.sendUpdates(); } void setup() { Serial.begin(115200); pinMode (led_blue,OUTPUT); dht.setup(sensorPin, DHTesp::DHT22); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } Serial.println(WiFi.localIP()); server.begin(); periodicTicker.attach_ms(5000, updateCards); } void loop() { botao_1.attachCallback([&](bool value){ Serial.println("[botao_1] Button Callback Triggered: "+String((value)?"true":"false")); botao_1.update(value); if(value){ digitalWrite(led_blue,HIGH); }else{ digitalWrite(led_blue,LOW); } dashboard.sendUpdates(); }); botao_2.attachCallback([&](bool value){ Serial.println("[botao_2] Button Callback Triggered: "+String((value)?"true":"false")); botao_2.update(value); if(value){ digitalWrite(led_blue,HIGH); }else{ digitalWrite(led_blue,LOW); } dashboard.sendUpdates(); }); botao_3.attachCallback([&](bool value){ Serial.println("[botao_3] Button Callback Triggered: "+String((value)?"true":"false")); botao_3.update(value); if(value){ digitalWrite(led_blue,HIGH); }else{ digitalWrite(led_blue,LOW); } dashboard.sendUpdates(); }); botao_4.attachCallback([&](bool value){ Serial.println("[botao_4] Button Callback Triggered: "+String((value)?"true":"false")); botao_4.update(value); if(value){ digitalWrite(led_blue,HIGH); }else{ digitalWrite(led_blue,LOW); } dashboard.sendUpdates(); }); botao_5.attachCallback([&](bool value){ Serial.println("[botao_5] Button Callback Triggered: "+String((value)?"true":"false")); botao_5.update(value); if(value){ digitalWrite(led_blue,HIGH); }else{ digitalWrite(led_blue,LOW); } dashboard.sendUpdates(); }); }