Good morning all,
I have a problem in my code. I want to control 4 relays with four push button and / or blynk application.
I only have button 1 which reacts quickly with aplication.
Do you have any idea how to fix this problem
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#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
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
BlynkTimer timer;
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxxxx";
// Set your LED and physical button pins here
const int relais1 = 17;
const int btn1 = 25;
const int led1 = 33;
const int relais2 = 5;
const int btn2 = 26;
const int led2 = 32;
const int relais3 = 18;
const int btn3 = 27;
const int led3 = 35;
const int relais4 = 19;
const int btn4 = 14;
const int led4 = 34;
// one wire temperature
#define ONE_WIRE_BUS 23
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 3;
float tempC;
void checkPhysicalButton1();
void checkPhysicalButton2();
void checkPhysicalButton3();
void checkPhysicalButton4();
void temperature();
int relais1Sate = LOW;
int btn1State = HIGH;
int led1State = LOW;
int relais2Sate = LOW;
int btn2State = HIGH;
int led2State = LOW;
int relais3Sate = LOW;
int btn3State = HIGH;
int led3State = LOW;
int relais4Sate = LOW;
int btn4State = HIGH;
int led4State = LOW;
int lcd_position[] = {16, 30, 44}; // position temperature sur l'écran
BLYNK_WRITE(V1) {
relais1Sate = param.asInt();
digitalWrite(relais1, relais1Sate);
led1State = param.asInt();
digitalWrite(led1, led1State);
}
BLYNK_WRITE(V2) {
relais2Sate = param.asInt();
digitalWrite(relais2, relais2Sate);
led2State = param.asInt();
digitalWrite(led2, led2State);
}
BLYNK_WRITE(V3) {
relais3Sate = param.asInt();
digitalWrite(relais3, relais3Sate);
led3State = param.asInt();
digitalWrite(led3, led3State);
}
BLYNK_WRITE(V4) {
relais4Sate = param.asInt();
digitalWrite(relais4, relais4Sate);
led4State = param.asInt();
digitalWrite(led4, led4State);
}
void temperature()
{
display.clearDisplay();
display.display();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("---------------------");
display.setCursor(0, 6);
display.print("| Temperature |");
display.setCursor(0, 12);
display.println("---------------------");
display.setCursor(0, 16); // position 0
display.println(" Interieur |");
display.print("---------------------");
display.display();
display.setCursor(0, 30); // position 1
display.println(" Exterrieur |");
display.print("---------------------");
display.display();
display.setCursor(0, 44); // position 2
display.println(" Eau |");
display.print("---------------------");
display.display();
// Send command to all the sensors for temperature conversion
sensors.requestTemperatures();
// Display temperature from each sensor
for (int i = 0; i < deviceCount; i++)
{
tempC = sensors.getTempCByIndex(i);
Blynk.virtualWrite(i + 21, tempC);
display.setCursor(80, lcd_position[i]);
display.print(tempC);
display.display();
}
}
void checkPhysicalButton1()
{
if (digitalRead(btn1) == LOW) {
// btnState is used to avoid sequential toggles
if (btn1State != LOW) {
// Toggle LED state
relais1Sate = !relais1Sate;
digitalWrite(relais1, relais1Sate);
led1State = !led1State;
digitalWrite(led1, led1State);
// Update Button Widget
Blynk.virtualWrite(V1, relais1Sate);
Blynk.virtualWrite(V1, led1State);
}
btn1State = LOW;
} else {
btn1State = HIGH;
}
}
void checkPhysicalButton2()
{
if (digitalRead(btn2) == LOW) {
// btnState is used to avoid sequential toggles
if (btn2State != LOW) {
// Toggle LED state
relais2Sate = !relais2Sate;
digitalWrite(relais2, relais2Sate);
led2State = !led2State;
digitalWrite(led2, led2State);
// Update Button Widget
Blynk.virtualWrite(V2, relais1Sate);
Blynk.virtualWrite(V2, led1State);
}
btn2State = LOW;
} else {
btn2State = HIGH;
}
}
void checkPhysicalButton3()
{
if (digitalRead(btn3) == LOW) {
// btnState is used to avoid sequential toggles
if (btn3State != LOW) {
// Toggle LED state
relais3Sate = !relais3Sate;
digitalWrite(relais3, relais3Sate);
led3State = !led3State;
digitalWrite(led3, led3State);
// Update Button Widget
Blynk.virtualWrite(V3, relais1Sate);
Blynk.virtualWrite(V3, led1State);
}
btn3State = LOW;
} else {
btn3State = HIGH;
}
}
void checkPhysicalButton4()
{
if (digitalRead(btn4) == LOW) {
// btnState is used to avoid sequential toggles
if (btn4State != LOW) {
// Toggle LED state
relais4Sate = !relais4Sate;
digitalWrite(relais4, relais4Sate);
led4State = !led4State;
digitalWrite(led4, led4State);
// Update Button Widget
Blynk.virtualWrite(V4, relais1Sate);
Blynk.virtualWrite(V4, led1State);
}
btn4State = LOW;
} else {
btn4State = HIGH;
}
}
void setup()
{
sensors.begin(); // debut de la librairie capteur de température
// Debug console
Serial.begin(9600);
WiFi.begin(ssid, pass);
//timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds
Blynk.config(auth);
pinMode(relais1, OUTPUT);
pinMode(btn1, INPUT_PULLUP);
digitalWrite(relais1, relais1Sate);
pinMode(led1, OUTPUT);
pinMode(relais2, OUTPUT);
pinMode(btn2, INPUT_PULLUP);
digitalWrite(relais2, relais2Sate);
pinMode(led2, OUTPUT);
pinMode(relais3, OUTPUT);
pinMode(btn3, INPUT_PULLUP);
digitalWrite(relais3, relais3Sate);
pinMode(led3, OUTPUT);
pinMode(relais4, OUTPUT);
pinMode(btn4, INPUT_PULLUP);
digitalWrite(relais4, relais4Sate);
pinMode(led4, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
display.display();
timer.setInterval(100L, checkPhysicalButton1);
timer.setInterval(200L, checkPhysicalButton2);
timer.setInterval(300L, checkPhysicalButton3);
timer.setInterval(400L, checkPhysicalButton4);
timer.setInterval(10000L, temperature);
}
void loop()
{
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!
}