/***
This example shows how value can be pushed from Arduino to
the Blynk App.
WARNING :
For this example you'll need Adafruit DHT sensor libraries:
https://github.com/adafruit/Adafruit_Sensor
https://github.com/adafruit/DHT-sensor-library
App dashboard setup:
Value Display widget attached to V5
Value Display widget attached to V6
***/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL28lSHkMJz"
#define BLYNK_TEMPLATE_NAME "Trabalho de algoritimos"
#define BLYNK_AUTH_TOKEN "xAgNzsNkvC6WV9LOGOb64AMoR3nlGibz"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <ESP32Servo.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
#define DHTPIN 15 // What digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
Servo meuServo_18;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
BLYNK_WRITE(V7) {
int pinValue = param.asInt(); // Get value from virtual pin V7
if (pinValue == 1) {
meuServo_18.write(90); // Move the servo to 90 degrees
Serial.println("Botão virtual pressionado: Servo movido para 90 graus");
} else {
meuServo_18.write(0); // Move the servo back to 0 degrees or any resting position
Serial.println("Botão virtual liberado: Servo movido para 0 graus");
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
dht.begin();
// Initialize the servo
meuServo_18.attach(18);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
// Configure the pin for the button
pinMode(2, INPUT_PULLUP);
}
void loop()
{
Blynk.run();
timer.run();
// Read the button state
int buttonState = digitalRead(2);
// Check if the button is pressed (LOW state)
if (buttonState == LOW) {
meuServo_18.write(90); // Move the servo to 90 degrees
Serial.println("Botão pressionado: Servo movido para 90 graus");
} else {
meuServo_18.write(0); // Move the servo back to 0 degrees or any resting position
Serial.println("Botão não pressionado: Servo movido para 0 graus");
}
}