// Version 0.1.0 - PLEASE NOTE: WORK IN PROGRESS, INCOMPLETE CODE!
// Fill in information from Blynk Device Info here
#define BLYNK_TEMPLATE_ID "TMPL4b-n2UW9a" // "TMP**********"
#define BLYNK_TEMPLATE_NAME "ESP32 WOKWI test 1" // "Device name"
#define BLYNK_AUTH_TOKEN "gXgjoK4jZF2rWwrQNhnBF372QMLQYXL2" // "YourAuthToken"
#define BLYNK_FIRMWARE_VERSION "0.1.0" // Your own internal software version
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// #include <BlynkESP32_BT_WF>
#include <LiquidCrystal.h> // include standard LCD library
// #include "BlynkEdgent.h" // Extensive and advanced features Blynk library
#define BLYNK_PRINT Serial //<<Comment it out to disable prints and save space
// #define BLYNK_DEBUG
#define APP_DEBUG
char ssid[] = "Wokwi-GUEST"; // Your WiFi SSID; use "Wokwi-GUEST" on Wokwi
char pass[] = ""; // Your WiFi password, leave "" for open network without password\
LiquidCrystal lcd(23,22,21,19,18,5); // assign pins for LCD as RS, E, D4, D5, D6, D7
#define LED1 15
#define LED2 2
#define LED3 0
#define LED4 4
#define LED5 16
/* The device can send data TO the App using Blynk.virtualWrite(pin, value)
and receive data FROM the App using BLYNK_WRITE(vPIN). */
// WidgetLED LED4(V3);
//#define button1_pin 26
//#define button2_pin 25
//#define relay1_pin 13
//#define relay2_pin 12
//int relay1_state = 0;
//int relay2_state = 0;
//Change the virtual pins according the rooms
//#define button1_vpin V0
//#define button2_vpin V2
/*
BLYNK_CONNECTED() { // This function called when device connects to Blynk.Cloud
Blynk.syncVirtual(button1_vpin); // Request the latest state from server
Blynk.syncVirtual(button2_vpin);
}
BLYNK_WRITE(button1_vpin) { // This function called on Virtual Pin state change
relay1_state = param.asInt(); // on Blynk App or Web Dashboard button press
digitalWrite(relay1_pin, relay1_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {
relay2_state = param.asInt();
digitalWrite(relay2_pin, relay2_state);
} */
// Sample volts and amps each second, divide accumulated Watts by 3600 to get Wh
const int potPin1 = 34; // Left pot (pot1) is on pin 34, simulates voltage
const int potPin2 = 35; // Right pot (pot2) is on pin 35, simulates current
int pot1Value, pot2Value = 0; // Declare integers for potentiometers raw ADC values
// Declare floats for the variables
float pot1Voltage, pot2Current, Power, Energy, IncrementalPower = 0.00;
BlynkTimer timer; // timer for ADC sample read delay
void setup() {
Serial.begin(9600); // or 115200? (Debug console)
lcd.begin(16,2); // specify 16-column 2-row LCD
lcd.setCursor(1, 0);
delay(100); //<< VERY IMPORTANT, won't function without it
pinMode(LED1, OUTPUT); // INPUT pinMode is default, no need to set
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, 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);
timer.setInterval(1000L, AnalogPinRead); // Run sensor scan 4 times a second
}
// CODE EXAMPLE FOR ANALOG READ
void AnalogPinRead() {
pot1Value = analogRead(potPin1); // Reads the value of pot 1
pot1Voltage = 3.3 * pot1Value/4095; // Multiply it by ref. volt., divide by 12 bits
pot2Value = analogRead(potPin2); // Reads the value of pot 2
pot2Current = 3.3 * pot2Value/4095;
Power = pot1Voltage * pot2Current;
IncrementalPower += Power;
Energy = IncrementalPower / 3600;
Blynk.virtualWrite(V5, String(pot1Voltage,3) + "V, " + String(pot2Current,3) + "A, "
+ String(Power,3) + "W");
Serial.println(IncrementalPower, 3);
Serial.println(String(Energy) + "Wh");
// below is another example for printing multiple variables to one line
/* char buff[18]; // make enough space
snprintf(buff,(sizeof(buff)-1),"%6.3fV,%6.3fA",pot1Voltage,pot2Current);
Serial.println(buff); */
lcd.clear();
lcd.setCursor(0,0); // set cursor to first column, first row
//lcd.println((String)pot1Value + " " + (String)pot2Value);
//lcd.setCursor(0,1);
lcd.println(String(pot1Voltage,3) + "V " + String(pot2Current,3) + "A");
lcd.setCursor(0,1); // set cursor to first column, second row
lcd.println(String(Power,3) + "W");
}
void loop() {
//delay(1000);
delay(1000); // in case of no other delay, speeds up Wokwi simulation
Blynk.run();
timer.run();
}
// Every time you use the LED switch in the app, this function
// will listen and update the state on device
BLYNK_WRITE(V0) // V0 = datastream used to transfer and store LED1 switch state.
{
int value = param.asInt(); // Local variable `value` stores the incoming LED switch state (1 or 0)
if (value == 1) // Based on this value, the physical LED on the board will be on or off:
{ digitalWrite(LED1, HIGH);
Serial.print("LED1 value = ");
Serial.println(value); }
else
{ digitalWrite(LED1, LOW);
Serial.print("LED1 value = ");
Serial.println(value); }
}
BLYNK_WRITE(V1)
{
int value = param.asInt(); // Local var. `value` stores incoming LED switch state (1 or 0)
if (value == 1) // Based on this value, the LED on the board will be on or off
{ digitalWrite(LED2, HIGH);
Serial.print("LED2 value = ");
Serial.println(value); }
else
{ digitalWrite(LED2, LOW);
Serial.print("LED2 value = ");
Serial.println(value); }
}
BLYNK_WRITE(V2)
{
int value = param.asInt(); // Local var. `value` stores incoming LED switch state (1 or 0)
if (value == 1) // Based on this value, the LED on the board will be on or off
{ digitalWrite(LED3, HIGH);
Serial.print("LED3 value = ");
Serial.println(value); }
else
{ digitalWrite(LED3, LOW);
Serial.print("LED3 value = ");
Serial.println(value); }
}
// Select your pin with physical button
// const int btnPin = 4;
// V3 LED Widget represents the physical button state
/* boolean btnState = false;
void buttonLedWidget()
{
// Read button
boolean isPressed = (digitalRead(btnPin) == LOW);
// If state has changed...
if (isPressed != btnState) {
if (isPressed) {
LED4.on();
} else {
LED4.off();
}
btnState = isPressed;
}
} */
BLYNK_WRITE(V3)
{
int value = param.asInt();
/* if (value == 7)
{ digitalWrite(LED4, LOW);
LED4.off();
Serial.print("LED4 value = ");
Serial.println(value); }
if (value == 8)
{ digitalWrite(LED4, HIGH);
LED4.on();
Serial.print("LED4 value = ");
Serial.println(value); } */
if (value == 9)
{ digitalWrite(LED5, LOW);
Serial.print("LED5 value = ");
Serial.println(value); }
if (value == 10)
{ digitalWrite(LED5, HIGH);
Serial.print("LED5 value = ");
Serial.println(value); }
}
/***********************************************************
// Every time you use the LED switch in the app, this function
// will listen and update the state on device
BLYNK_WRITE(V0) // V0 = datastream used to transfer and store LED1 switch state.
{
int value = param.asInt(); // Local variable `value` stores the incoming LED switch state (1 or 0)
if (value == 1) // Based on this value, the physical LED on the board will be on or off:
{ digitalWrite(LED1, HIGH);
Serial.print("LED1 value = ");
Serial.println(value); }
else
{ digitalWrite(LED1, LOW);
Serial.print("LED1 value = ");
Serial.println(value); }
}
*/
int led2
BLYNK_WRITE(V2){
int pinValueV2 = param.asInt();
if(pinValueV2 == 1){
led2.on();
}
if(pinValueV2 == 0){
led2.off();
}
}
**********************************************************
/*
WidgetLED led4(V4);
int value = param.asInt();
if (value == 7)
{ digitalWrite(LED4, LOW);
led4.off();
Serial.print("led4 value = ");
Serial.println(value); }
if (value == 8)
{ digitalWrite(LED4, HIGH);
led4.on();
Serial.print("led4 value = ");
Serial.println(value); }
WidgetLED led1(V1);
void button1 {
int value = param.asInt();
if (value == 1)
{ led1.on(); }
if (value == 0)
{ led1.off(); }
}
/*************************************************************
WidgetLED led1(V2);// Virtual Pin V2
BlynkTimer timer;
void sensor1(){
int value = digitalRead(IR_sensor);
if(value == HIGH){
led1.on();
digitalWrite(LED,HIGH);
}
else{
led1.off();
digitalWrite(LED,LOW);
}
}
// V1 LED Widget is blinking
void blinkLedWidget() {
if (led1.getValue()) {
led1.off();
Serial.println("LED on V1: off");
} else {
led1.on();
Serial.println("LED on V1: on"); } }
*************************************************************/