#define BLYNK_TEMPLATE_ID "TMPL650TKoGCD"
#define BLYNK_TEMPLATE_NAME "Wokwi Blynk"
#define BLYNK_AUTH_TOKEN "yt-dQA1Id9a4RJmWS4hiSyqSyfHiDejc"
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
#define PIN 5 // Pin connected to the WS2812 data line
#define NUMPIXELS 16 // Number of WS2812 LEDs in the strip
#define DHTPIN 12 // DHT 22
#define DHTTYPE DHT22
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_NeoPixel.h>
#include <DHT.h>
#include <DHT_U.h>
// Name of the router and password
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int led = 14;
const int trigPin = 19;
const int echoPin = 18;
long duration;
float distanceCm;
float distanceInch;
// BlynkTimer initialize
BlynkTimer timer;
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// Start wifi - 2 parameters (ssid & password)
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
// Connection status
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Connection Info
Serial.println("");
Serial.println("WiFi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// 3 parameters - Token, ssid, password
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
// 2 parameters - 1. time (L = ms) , 2. Method_Name
timer.setInterval(1000L, DistanceData);
pixels.begin();
pixels.setBrightness(100); // reduce brightness to 50%
// Initialize the DHT sensor
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void DistanceData() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
// Write Distance Data into Blynk
// 2 parameters - 1. pin #, 2. Value
Blynk.virtualWrite(V1, distanceCm);
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, color);
pixels.show();
delay(wait);
}
}
// From Blynk to Wowki we need to write the LED status
// to write we always use this method (Blynk method)
BLYNK_WRITE(V0) {
// get the status of the virtual switch V0
int status = param.asInt();
digitalWrite(led, status);
}
BLYNK_WRITE(V2) {
int pinValue = param.asInt(); // Get the value of the virtual pin V2
if (pinValue == 1) {
while(1){
// Turn on WS2812 LEDs (example: set to red color)
colorWipe((255, 0, 0), 100); // Red color, 50ms delay
delay(100);
colorWipe(pixels.Color(0, 0, 255), 100);
delay(100);
colorWipe(pixels.Color(0, 255, 0), 100);
delay(100);
colorWipe(pixels.Color(255, 255, 0),100);
}
} else {
// Turn off WS2812 LEDs
colorWipe(pixels.Color(0, 0, 0), 50); // Off (black) color, 50ms delay
}
}
// Function to send DHT22 data to Blynk
void sendSensor() {
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if the read failed and exit early (to try again).
if (isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// Send the data to Blynk
Blynk.virtualWrite(V4, t); // Virtual Pin V4 for temperature
Blynk.virtualWrite(V3, h); // Virtual Pin V3 for humidity
}
void loop() {
Blynk.run();
timer.run();
}