Hi everybody and Hi Pete…
I don’t really know how I should get start from…
The “Constellation” as shown below works actually fine, exept one thing: After a while, the ESP disconnects from the Server (due floodings?). But that will be an Topic for later.
The code below is the standard Template and it works with singe buttons.
I would like to Re-Code this Code, to switch a 3-Stage-FAN/Vent. The Vent has a Main Power Switch.
(for example: if you push switch 1, then the Vent turns on by Stage 1. The Main Power Switch jump out. Then, if you push switch 2 it turns switch 1 off. and the same will happen to each other switch until you push switch zero/off again.
My Idea was, to open the Vent, and connect each Line which goes from the Coil to the internal Switch, to connect with the SSR’s (MainPWR to SSR 1, (from the Coil goes per Stage one Line to the internal switch) Stage 1 = SSR2, Stage 2 = SSR3 and Stage 3 = SSR4)
But, if I try to change the code for Pushbuttons, it wont work right.
The Widget-Buttons works some time, and some times not, the same is with the push-Buttons. I assume that the lines which I change generate some kind double effect and the switch which I have pushed will turn back again, also the widget-button. Then it becomes sometimes a one way effect on widged-button site. Sometimes but from push-button site. I struggle with that. Can anybody help me please?
Many thanks in advance
• Arduino IDE 1.8.13 (cause: no longer Support for certain Boards after Update)
• AMD Athlon II X4 630 2x 2,8 GHz, 12 GB RAM
• Win 7 x64 Ultimate SP1
Boards:
• 1x Original UNO R3
• 4x Nano
• 6x Lolin Wemos D1 R2 Mini (ESP8266) <— 1 IN USE
• 6x NodeMCU 8266
• 1x Teensy 4.1
• Wemos Di R2 Mini (ESP8266 WiFi)
• SAMSUNG GALAXY A80 (SMA80F) Android 11
• Blynk local server
• Blynk Library version 1.1.0
//#define BLYNK_PRINT Serial <--- if activated, IDE wont compile
#include <BlynkSimpleEsp8266.h>
//#include <BlynkSimpleEsp32.h>
//to have more security due copy and paste, AUTH, SSID, PW, etc. is inside credentials
#include <credentials.h>
// define the GPIO connected with Relays and switches
#define RelayPin1 14 //D5
#define RelayPin2 12 //D6
#define RelayPin3 13 //D7
#define RelayPin4 15 //D8
#define SwitchPin1 5 //D1
#define SwitchPin2 4 //D2
#define SwitchPin3 0 //D3
#define SwitchPin4 2 //D4
//#define wifiLed 16 //D0 reserved
#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2
#define VPIN_BUTTON_3 V3
#define VPIN_BUTTON_4 V4
int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4
int wifiFlag = 0;
BlynkTimer timer;
void relayOnOff(int relay){
switch(relay){
case 1:
if(toggleState_1 == 1){
digitalWrite(RelayPin1, LOW); // turn on relay 1
toggleState_1 = 0;
Serial.println("Device1 ON");
}
else{
digitalWrite(RelayPin1, HIGH); // turn off relay 1
toggleState_1 = 1;
Serial.println("Device1 OFF");
}
delay(100);
break;
case 2:
if(toggleState_2 == 1){
digitalWrite(RelayPin2, LOW); // turn on relay 2
toggleState_2 = 0;
Serial.println("Device2 ON");
}
else{
digitalWrite(RelayPin2, HIGH); // turn off relay 2
toggleState_2 = 1;
Serial.println("Device2 OFF");
}
delay(100);
break;
case 3:
if(toggleState_3 == 1){
digitalWrite(RelayPin3, LOW); // turn on relay 3
toggleState_3 = 0;
Serial.println("Device3 ON");
}
else{
digitalWrite(RelayPin3, HIGH); // turn off relay 3
toggleState_3 = 1;
Serial.println("Device3 OFF");
}
delay(100);
break;
case 4:
if(toggleState_4 == 1){
digitalWrite(RelayPin4, LOW); // turn on relay 4
toggleState_4 = 0;
Serial.println("Device4 ON");
}
else{
digitalWrite(RelayPin4, HIGH); // turn off relay 4
toggleState_4 = 1;
Serial.println("Device4 OFF");
}
delay(100);
break;
default : break;
}
}
void with_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1);
Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1); // Update Button Widget
}
else if (digitalRead(SwitchPin2) == LOW){
delay(200);
relayOnOff(2);
Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2); // Update Button Widget
}
else if (digitalRead(SwitchPin3) == LOW){
delay(200);
relayOnOff(3);
Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3); // Update Button Widget
}
else if (digitalRead(SwitchPin4) == LOW){
delay(200);
relayOnOff(4);
Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4); // Update Button Widget
}
}
void without_internet(){
//Manual Switch Control
if (digitalRead(SwitchPin1) == LOW){
delay(200);
relayOnOff(1);
}
else if (digitalRead(SwitchPin2) == LOW){
delay(200);
relayOnOff(2);
}
else if (digitalRead(SwitchPin3) == LOW){
delay(200);
relayOnOff(3);
}
else if (digitalRead(SwitchPin4) == LOW){
delay(200);
relayOnOff(4);
}
}
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
}
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
toggleState_1 = param.asInt();
digitalWrite(RelayPin1, toggleState_1);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
toggleState_2 = param.asInt();
digitalWrite(RelayPin2, toggleState_2);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
toggleState_3 = param.asInt();
digitalWrite(RelayPin3, toggleState_3);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
toggleState_4 = param.asInt();
digitalWrite(RelayPin4, toggleState_4);
}
void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
wifiFlag = 1;
// digitalWrite(wifiLed, HIGH); //Turn off WiFi LED
}
if (isconnected == true) {
wifiFlag = 0;
// digitalWrite(wifiLed, LOW); //Turn on WiFi LED
}
}
void setup()
{
Serial.begin(9600);
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, IPAddress(192,168,2,12), 8080);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
//pinMode(wifiLed, OUTPUT);
pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);
//During Starting all Relays should TURN OFF
digitalWrite(RelayPin1, toggleState_1);
digitalWrite(RelayPin2, toggleState_2);
digitalWrite(RelayPin3, toggleState_3);
digitalWrite(RelayPin4, toggleState_4);
WiFi.begin(WIFI_SSID, WIFI_PASS);
timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
Blynk.config(AUTH);
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi Not Connected");
}
else
{
Serial.println("WiFi Connected");
Blynk.run();
}
timer.run(); // Initiates SimpleTimer
if (wifiFlag == 0)
with_internet();
else
without_internet();
}