/*************************************************************
This is a simple demo of sending and receiving some data.
Be sure to check out other examples!
*************************************************************/
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPLYTrKqXWB"
#define BLYNK_DEVICE_NAME "Quickstart Device"
#define BLYNK_AUTH_TOKEN "aSFgRjxkqMhykk42cC3bVsWfeUxesw6c"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
// ex
#define btnA 2
int count = 0;
// LED
#define led 32
// DHT
#include "DHTesp.h"
#define DHT 15
float Temperature;
float Humidity;
DHTesp dht;
// OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const uint8_t T[] = {0x03, 0xe0, 0x00, 0x03, 0xe0, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x03, 0x70, 0x00, 0x07, 0x70, 0x00, 0x0e, 0x38, 0x00, 0x0c, 0x18, 0x00, 0x0c, 0x18, 0x00, 0x0e, 0x38, 0x00, 0x07, 0xf0, 0x00, 0x03, 0xe0, 0x00};
const uint8_t H[] = {0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x01, 0xe0, 0x00, 0x03, 0xf0, 0x00, 0x07, 0x38, 0x00, 0x0e, 0x1c, 0x00, 0x1c, 0x0e, 0x00, 0x18, 0x06, 0x00, 0x38, 0x07, 0x00, 0x30, 0x03, 0x00, 0x30, 0x03, 0x00, 0x30, 0x03, 0x00, 0x38, 0x07, 0x00, 0x18, 0x06, 0x00, 0x1c, 0x0e, 0x00, 0x0f, 0xfc, 0x00, 0x07, 0xf8, 0x00, 0x00, 0xc0, 0x00};
const uint8_t F[] = {0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x01, 0xe0, 0x00, 0x03, 0xf0, 0x00, 0x07, 0x38, 0x00, 0x06, 0x18, 0x00, 0x0e, 0xdc, 0x00, 0x0c, 0xcc, 0x00, 0x1c, 0xce, 0x00, 0x38, 0xc7, 0x00, 0x30, 0xc3, 0x00, 0x70, 0x03, 0x80, 0x60, 0xc1, 0x80, 0xe0, 0xc1, 0xc0, 0xc0, 0x00, 0xc0, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00};
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
Serial.println("value : "+String(value));
if(value == 1)
{
digitalWrite(led, LOW); // relay > active Low
}
else
{
digitalWrite(led, HIGH);
}
// Update state
Blynk.virtualWrite(V1, value);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
/*
// Count
if(digitalRead(btnA) == 1)
{
count ++;
Serial.println("btnA_Count : "+ String(count));
}
Blynk.virtualWrite(V5, count);
*/
// DHT
Temperature = dht.getTemperature();
Humidity = dht.getHumidity();
Blynk.virtualWrite(V6, Temperature);
Blynk.virtualWrite(V7, Humidity);
//-------------------------------------------
Blynk.virtualWrite(V4, led);
Blynk.virtualWrite(V2, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(btnA, INPUT);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
// DHT_SETUP
dht.setup(DHT, DHTesp::DHT22);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
display.display();
}
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(30, 10);
display.print("Hello USER");
display.display();
}
void loop()
{
TempAndHumidity data = dht.getTempAndHumidity();
if(data.temperature > 35)
{
digitalWrite(led, HIGH);
Blynk.virtualWrite(V4, HIGH);
Blynk.virtualWrite(V8, HIGH);
display.setCursor(80, 20);
display.print("Fan : "+String + "ON");
}
if(data.temperature <= 35)
{
digitalWrite(led, LOW);
Blynk.virtualWrite(V4, LOW);
Blynk.virtualWrite(V8, LOW);
display.setCursor(80, 20);
display.print("Fan : "+String + "OFF");
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(30, 2);
display.println("Thermometer");
display.drawBitmap(2, 14, T, 18, 18, 1);
display.setCursor(20, 20);
display.print(String(data.temperature, 2) + " C");
display.drawBitmap(2, 39, H, 18, 18, 1);
display.setCursor(20, 45);
display.print(String(data.humidity, 1) + " %");
display.drawBitmap(65, 25, F, 18, 18, 1);
display.display();
delay(200);
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}