Dear friends ,
I am immensely grateful for the teachings and for sharing your knowledge with me.
PeteKnight , Thanks for your magnificent post on using the timer, a flawless gem that everyone should read before doing any projects here.
John93,
You don’t write much, but with the little you write you clarify everything, I used your indication and adapted the example you suggested, and the problem is solved.
Now the GPIOs don’t fire at every restart or when ESP is turned on, the problem was the delays, as you explained, the time it takes Blynk to connect to the ESP, it’s about 10 seconds, but if you use Delays, these will delay all the code, triggering some action even before the module connects, this is my conclusion, feel free to correct me if I’m wrong.
now that I have that sorted out, I’ll deal with the beautification only in the code and name changes like relay for example instead of ledpin, etc. and I’m going to assemble the hardware, as soon as I do that, I still hope, I’ll upload the project to my youtube channel, and I’ll publish the video link here.
Thanks.
Greetings!
Here the code:
/*************************************************************
This example shows how to synchronize Button widget
and physical button state.
App project setup:
Button widget attached to V2 (Switch mode)
*************************************************************/
// 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 "TMPLHc_jQ6HO"
#define BLYNK_DEVICE_NAME "4 reles esp32"
#define BLYNK_AUTH_TOKEN "BBmmzNOqYKecgE3Kcv6HrE1rOI7a06bM"
// 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[] = "2";
char pass[] = "2";
// Set your LED and physical button pins here
const int ledPin1 = 12;
const int ledPin2 = 13;
const int ledPin3 = 23;
const int ledPin4 = 27;
const int btnPin1 = 26;
const int btnPin2 = 25;
const int btnPin3 = 33;
const int btnPin4 = 32;
BlynkTimer timer;
void checkPhysicalButton();
int ledState1 = LOW;
int btnState1 = HIGH;
int ledState2 = LOW;
int btnState2 = HIGH;
int ledState3 = LOW;
int btnState3 = HIGH;
int ledState4 = LOW;
int btnState4 = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
ledState1 = param.asInt();
digitalWrite(ledPin1, ledState1);
}
BLYNK_WRITE(V2) {
ledState2 = param.asInt();
digitalWrite(ledPin2, ledState2);
}
BLYNK_WRITE(V3) {
ledState3 = param.asInt();
digitalWrite(ledPin3, ledState3);
}
BLYNK_WRITE(V4) {
ledState4 = param.asInt();
digitalWrite(ledPin4, ledState4);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin1) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState1 != LOW) {
// Toggle LED state
ledState1 = !ledState1;
digitalWrite(ledPin1, ledState1);
// Update Button Widget
Blynk.virtualWrite(V1, ledState1);
}
btnState1 = LOW;
} else {
btnState1 = HIGH;
}
if (digitalRead(btnPin2) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState2 != LOW) {
// Toggle LED state
ledState2 = !ledState2;
digitalWrite(ledPin2, ledState2);
// Update Button Widget
Blynk.virtualWrite(V2, ledState2);
}
btnState2 = LOW;
} else {
btnState2 = HIGH;
}
if (digitalRead(btnPin3) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState3 != LOW) {
// Toggle LED state
ledState3 = !ledState3;
digitalWrite(ledPin3, ledState3);
// Update Button Widget
Blynk.virtualWrite(V3, ledState3);
}
btnState3 = LOW;
} else {
btnState3 = HIGH;
}
if (digitalRead(btnPin4) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState4 != LOW) {
// Toggle LED state
ledState4 = !ledState4;
digitalWrite(ledPin4, ledState4);
// Update Button Widget
Blynk.virtualWrite(V4, ledState4);
}
btnState4 = LOW;
} else {
btnState4 = HIGH;
}
}
void setup()
{
// Debug console
Serial.begin(115200);
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);
pinMode(ledPin1, OUTPUT);
pinMode(btnPin1, INPUT_PULLUP);
digitalWrite(ledPin1, ledState1);
pinMode(ledPin2, OUTPUT);
pinMode(btnPin2, INPUT_PULLUP);
digitalWrite(ledPin2, ledState2);
pinMode(ledPin3, OUTPUT);
pinMode(btnPin3, INPUT_PULLUP);
digitalWrite(ledPin3, ledState3);
pinMode(ledPin4, OUTPUT);
pinMode(btnPin4, INPUT_PULLUP);
digitalWrite(ledPin4, ledState4);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
timer.run();
checkPhysicalButton();
}