#define BLYNK_TEMPLATE_NAME "Voltage sensor"
#define BLYNK_TEMPLATE_ID "TMPL6VbgjbzCM"
#define BLYNK_AUTH_TOKEN "ifBiQP3zJjkBqhgTR7QyLpauvG3w-NAG"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// setup OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//current
int sensorIn = 27; // pin where the OUT pin from sensor is connected on Arduino
int mVperAmp = 185; // this the 5A version of the ACS712 -use 100 for 20A Module and 66 for 30A Module
int Watt = 0;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
//voltage
int voltage_offset = 20;
//piezo
int sensorOutput = 26; //piezo
int ledPin = 34; // pin connected to LED
int threshold = 100;
BlynkTimer timer;
char ssid[] = "Hulaanmo"; // Your WiFi SSID
char pass[] = "Hulaanmo"; // Your WiFi password
float getVPP()
{
float result;
int readValue; // value read from the sensor
int maxValue = 0; // store max value here
int minValue = 4096; // store min value here ESP32 ADC resolution
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 3.3)/4096.0; //ESP32 ADC resolution 4096
return result;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare led connected pin as output
pinMode(sensorOutput, INPUT_PULLDOWN); // enable internal pull-down resistor
// Initialize OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(2);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Initializing Blynk...");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Setup Blynk timer
timer.setInterval(1000L, sendSensorData);
}
void loop() {
Blynk.run();
timer.run();
}
void sendSensorData() {
int volt = analogRead(14);// read the input - GPIO_NUM_34
double voltage = map(volt,0, 4096, 0, 1650) + voltage_offset;
voltage /= 100; // divide by 100 to get the decimal values
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println("V");
delay(500);
int sensorValue = 0; // default value when no sensor is connected
sensorValue = analogRead(sensorOutput); // read analog voltage from sensor
delay(10); // debounce the sensor input
// Calculate the voltage based on the analog reading
float voltagePiezo = sensorValue * (5.0 / 1023.0);
Serial.print(" | Voltage: ");
Serial.print(voltagePiezo);
Serial.println(" V");
if (sensorValue >= threshold) { // check voltage level from sensor
digitalWrite(ledPin, HIGH);
Serial.println("LED is ON");
} else {
digitalWrite(ledPin, LOW);
Serial.println("LED is OFF");
}
delay(500); // wait 500ms before taking the next reading
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; //root
AmpsRMS = ((VRMS * 1000)/mVperAmp)-0.13;
Serial.print(AmpsRMS);
Serial.print(" Amps RMS --- ");
Watt = (AmpsRMS*240/1.2);
// note: 1.2 is my own empirically established calibration factor
// as the voltage measured at D34 depends on the length of the OUT-to-D34 wire
// 240 is
Serial.print(Watt);
Serial.println(" Watts");
delay (100);
display.clearDisplay(); // Clear the display
display.setTextSize(1);
display.setCursor(20, 2);
display.print(" PIEZO GENERATOR ");
display.setTextSize(1);
display.setCursor(0,16);
display.print(" VOLTAGE : ");
display.setCursor(80, 16);
display.print(voltage, 4); //decimals
display.setTextSize(1);
display.setCursor(0, 34);
display.print(" CURRENT : ");
display.setCursor(80, 34);
display.print(AmpsRMS, 4); //decimals
display.setTextSize(1);
display.setCursor(0, 52);
display.print(" POWER : ");
display.setCursor(80, 52);
display.print(voltagePiezo, 4); //decimals
display.display(); // Update the display
Serial.print("Input Voltage = ");
Serial.println(voltage, 4); //decimals
Serial.print("Current = ");
Serial.println(AmpsRMS, 4); //decimals
Serial.print("Power = ");
Serial.println(Watt, 4); //decimals
Serial.println();
// Send data to Blynk
Blynk.virtualWrite(V1, voltage);
Blynk.virtualWrite(V2, AmpsRMS);
Blynk.virtualWrite(V3, voltagePiezo);
}
please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly. Triple backticks look like this: ```
@marksds Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
Pete.
already edited but do u know any solution to this?
You can’t use GPIO27 as an analog input when you’re using WiFi, because GPIO27 is connected to ADC2, and the ESP32 doesn’t support ADC2 and WiFi at the same time.
You should read this, and choose an ADC1 pin instead…
There are quite a few other issues with your code though, which will lead to problems with Blynk in future.
Pete.
i tried to follow your method but it still not working
More info required.
Pete.
still not displaying the text everytime i include the
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
So, I went into detail to point-out why the GPIO pin you’re using won’t work for analog input when using Blynk, and your responses are…
and quoting an unrelated line of code from your original sketch.
If you don’t want to provide information about the changes you’ve made to your sketch, the results you’re now seeing, whether your device is online, what you’re seeing in your serial monitor, what the role of the Arduino is in your hardware setup, etc, etc, then that’s fine by me - just don’t expect a solution unless you provide more info.
Pete.
I just changed the pins like what you told to me. but the oled screen is not showing anything unless I remove the " Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); "
#define BLYNK_TEMPLATE_NAME "Voltage sensor"
#define BLYNK_TEMPLATE_ID "TMPL6VbgjbzCM"
#define BLYNK_AUTH_TOKEN "ifBiQP3zJjkBqhgTR7QyLpauvG3w-NAG"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// setup OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//current
int sensorIn = 32; // pin where the OUT pin from sensor is connected on Arduino
int mVperAmp = 185; // this the 5A version of the ACS712 -use 100 for 20A Module and 66 for 30A Module
int Watt = 0;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
//voltage
int voltage_offset = 33;
//piezo
int sensorOutput = 34; //piezo
int ledPin = 35; // pin connected to LED
int threshold = 100;
BlynkTimer timer;
char ssid[] = "Hulaanmo"; // Your WiFi SSID
char pass[] = "Hulaanmo"; // Your WiFi password
float getVPP()
{
float result;
int readValue; // value read from the sensor
int maxValue = 0; // store max value here
int minValue = 4096; // store min value here ESP32 ADC resolution
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 3.3)/4096.0; //ESP32 ADC resolution 4096
return result;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare led connected pin as output
pinMode(sensorOutput, INPUT_PULLDOWN); // enable internal pull-down resistor
// Initialize OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(2);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Initializing Blynk...");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Setup Blynk timer
timer.setInterval(1000L, sendSensorData);
}
void loop() {
Blynk.run();
timer.run();
}
void sendSensorData() {
int volt = analogRead(14);// read the input - GPIO_NUM_34
double voltage = map(volt,0, 4096, 0, 1650) + voltage_offset;
voltage /= 100; // divide by 100 to get the decimal values
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println("V");
delay(500);
int sensorValue = 0; // default value when no sensor is connected
sensorValue = analogRead(sensorOutput); // read analog voltage from sensor
delay(10); // debounce the sensor input
// Calculate the voltage based on the analog reading
float voltagePiezo = sensorValue * (5.0 / 1023.0);
Serial.print(" | Voltage: ");
Serial.print(voltagePiezo);
Serial.println(" V");
if (sensorValue >= threshold) { // check voltage level from sensor
digitalWrite(ledPin, HIGH);
Serial.println("LED is ON");
} else {
digitalWrite(ledPin, LOW);
Serial.println("LED is OFF");
}
delay(500); // wait 500ms before taking the next reading
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; //root
AmpsRMS = ((VRMS * 1000)/mVperAmp)-0.13;
Serial.print(AmpsRMS);
Serial.print(" Amps RMS --- ");
Watt = (AmpsRMS*240/1.2);
// note: 1.2 is my own empirically established calibration factor
// as the voltage measured at D34 depends on the length of the OUT-to-D34 wire
// 240 is
Serial.print(Watt);
Serial.println(" Watts");
delay (100);
display.clearDisplay(); // Clear the display
display.setTextSize(1);
display.setCursor(20, 2);
display.print(" PIEZO GENERATOR ");
display.setTextSize(1);
display.setCursor(0,16);
display.print(" VOLTAGE : ");
display.setCursor(80, 16);
display.print(voltage, 4); //decimals
display.setTextSize(1);
display.setCursor(0, 34);
display.print(" CURRENT : ");
display.setCursor(80, 34);
display.print(AmpsRMS, 4); //decimals
display.setTextSize(1);
display.setCursor(0, 52);
display.print(" POWER : ");
display.setCursor(80, 52);
display.print(voltagePiezo, 4); //decimals
display.display(); // Update the display
Serial.print("Input Voltage = ");
Serial.println(voltage, 4); //decimals
Serial.print("Current = ");
Serial.println(AmpsRMS, 4); //decimals
Serial.print("Power = ");
Serial.println(Watt, 4); //decimals
Serial.println();
// Send data to Blynk
Blynk.virtualWrite(V1, voltage);
Blynk.virtualWrite(V2, AmpsRMS);
Blynk.virtualWrite(V3, voltagePiezo);
}
Pete.
oled screen still not displaying, but if i remove the " Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); " oled is displaying.
oled pins are
SDA is at GPIO21
SCL is at GPIO 22
GND is at GND pin
VCC is at 3.3V
#define BLYNK_TEMPLATE_NAME "Voltage sensor"
#define BLYNK_TEMPLATE_ID "TMPL6VbgjbzCM"
#define BLYNK_AUTH_TOKEN "ifBiQP3zJjkBqhgTR7QyLpauvG3w-NAG"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// setup OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//current
int sensorIn = 32; // pin where the OUT pin from sensor is connected on Arduino
int mVperAmp = 185; // this the 5A version of the ACS712 -use 100 for 20A Module and 66 for 30A Module
int Watt = 0;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
//voltage
int voltage_offset = 33;
//piezo
int sensorOutput = 34; //piezo
int ledPin = 35; // pin connected to LED
int threshold = 100;
BlynkTimer timer;
char ssid[] = "Hulaanmo"; // Your WiFi SSID
char pass[] = "Hulaanmo"; // Your WiFi password
float getVPP()
{
float result;
int readValue; // value read from the sensor
int maxValue = 0; // store max value here
int minValue = 4096; // store min value here ESP32 ADC resolution
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 3.3)/4096.0; //ESP32 ADC resolution 4096
return result;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare led connected pin as output
pinMode(sensorOutput, INPUT_PULLDOWN); // enable internal pull-down resistor
// Initialize OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(2);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println("Initializing Blynk...");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Setup Blynk timer
timer.setInterval(1000L, sendSensorData);
}
void loop() {
Blynk.run();
timer.run();
}
void sendSensorData() {
int volt = analogRead(36);// read the input - GPIO_NUM_34
double voltage = map(volt,0, 4096, 0, 1650) + voltage_offset;
voltage /= 100; // divide by 100 to get the decimal values
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println("V");
delay(500);
int sensorValue = 0; // default value when no sensor is connected
sensorValue = analogRead(sensorOutput); // read analog voltage from sensor
delay(10); // debounce the sensor input
// Calculate the voltage based on the analog reading
float voltagePiezo = sensorValue * (5.0 / 1023.0);
Serial.print(" | Voltage: ");
Serial.print(voltagePiezo);
Serial.println(" V");
if (sensorValue >= threshold) { // check voltage level from sensor
digitalWrite(ledPin, HIGH);
Serial.println("LED is ON");
} else {
digitalWrite(ledPin, LOW);
Serial.println("LED is OFF");
}
delay(500); // wait 500ms before taking the next reading
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; //root
AmpsRMS = ((VRMS * 1000)/mVperAmp)-0.13;
Serial.print(AmpsRMS);
Serial.print(" Amps RMS --- ");
Watt = (AmpsRMS*240/1.2);
// note: 1.2 is my own empirically established calibration factor
// as the voltage measured at D34 depends on the length of the OUT-to-D34 wire
// 240 is
Serial.print(Watt);
Serial.println(" Watts");
delay (100);
display.clearDisplay(); // Clear the display
display.setTextSize(1);
display.setCursor(20, 2);
display.print(" PIEZO GENERATOR ");
display.setTextSize(1);
display.setCursor(0,16);
display.print(" VOLTAGE : ");
display.setCursor(80, 16);
display.print(voltage, 4); //decimals
display.setTextSize(1);
display.setCursor(0, 34);
display.print(" CURRENT : ");
display.setCursor(80, 34);
display.print(AmpsRMS, 4); //decimals
display.setTextSize(1);
display.setCursor(0, 52);
display.print(" POWER : ");
display.setCursor(80, 52);
display.print(voltagePiezo, 4); //decimals
display.display(); // Update the display
Serial.print("Input Voltage = ");
Serial.println(voltage, 4); //decimals
Serial.print("Current = ");
Serial.println(AmpsRMS, 4); //decimals
Serial.print("Power = ");
Serial.println(Watt, 4); //decimals
Serial.println();
Pete.
I already provide the change code?
u told me to change the GPIO 14 to GPIO PINS UNDER ADC_1 and I already provide it up there
it shows that it is connected to wifi. that’s what i see on my serial monitor, my main problem is the oled is not displaying everytime I add the blynk code
Okay, I can’t help in that case.
Pete.
how sad, but why others can do blynk on esp
Other people are prepared to provide information, so its possible to help them with their issues. You aren’t, so I can’t help.
Pete.
I already provided the information you need but still cant?
u told me sir to change the line and after I changed it I provide the code even the pins for what Im using on my oled screen. how come sir?