Go Down

Topic: I need fix my code to on/off relay (Read 406 times) previous topic - next topic

moneyp1000

The condition is press button 1 relay 1 on and wait unit till press button 1 relay 1 off. How to fix it. Thank you very much.

My code

Code: [Select]
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiManager.h> 
ESP8266WiFiMulti wifiMulti;
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h> //Blynk
SimpleTimer timer;
#define WIFI_LED 13

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
char server[] = "XXXXXXXXXXX";
char ssid[] = "ArcHeR-OMEN";
//char ssid[] = "ArcHeR";
char pass[] = "XXXXXXXXX";
int port = 8080;

bool isFirstConnect = true;
int slider;
char Button1 = 0;
char Button2 = 9;
char Button3 = 10;
char Button4 = 14;
char Relay1 = 12;
char Relay2 = 5;
char Relay3 = 4;
char Relay4 = 15;
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;


BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
//Blynk.notify("Device 2 Starting!!!!");
isFirstConnect = false;
Serial.println(String("แอพเริ่มทำงาน!!"));  // used for debugging
}
}






void setup()
{
  Serial.begin(115200);
  Serial.println("\n Starting");
  wifiMulti.addAP(ssid, pass);
  Serial.println("SSID1");
  Blynk.config(auth, server, port);
  Blynk.connect();   // for a local server requiring WiFi connection
  pinMode(WIFI_LED, OUTPUT);
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  pinMode(Button3, INPUT);
  pinMode(Button4, INPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  digitalWrite(Button1, LOW);
  digitalWrite(Button2, LOW);
  digitalWrite(Button3, LOW);
  digitalWrite(Button4, LOW); 
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
  digitalWrite(Relay4, LOW);

  Serial.println("Connecting Wifi...");
    if(wifiMulti.run() == WL_CONNECTED) {
        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        Serial.println(WiFi.localIP());
    }


  timer.setInterval(15000, reconnectBlynk); // check every 15 seconds if we are connected to the server
}

void reconnectBlynk() // reconnect to server if disconnected, timer checks every 15 seconds
{                       
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
      digitalWrite(WIFI_LED,LOW);
    } else {
      BLYNK_LOG("Not reconnected");
      digitalWrite(WIFI_LED,HIGH);
    }
  }
}



void loop()
{
 if (Blynk.connected()) {   // to ensure that Blynk.run() function is only called if we are still connected to the server
    Blynk.run();
}
timer.run();
 if(wifiMulti.run() != WL_CONNECTED) {

        Serial.println("WiFi not connected!");
        delay(1000);
    }
  buttonState1 = digitalRead(Button1);
  buttonState2 = digitalRead(Button2);
  buttonState3 = digitalRead(Button3);
  buttonState4 = digitalRead(Button4);
 
    if (buttonState1 == HIGH) {
    // turn LED on:
    digitalWrite(Relay1, HIGH);
  } else {
    // turn LED off:
    digitalWrite(Relay1, LOW);
  }
     if (buttonState2 == HIGH) {
    // turn LED on:
    digitalWrite(Relay2, HIGH);
  } else {
    // turn LED off:
    digitalWrite(Relay2, LOW);
  }
    if (buttonState3 == HIGH) {
    // turn LED on:
    digitalWrite(Relay3, HIGH);
  } else {
    // turn LED off:
    digitalWrite(Relay3, LOW);
  }
       if (buttonState4 == HIGH) {
    // turn LED on:
    digitalWrite(Relay4, HIGH);
  } else {
    // turn LED off:
    digitalWrite(Relay4, LOW);
  }
}


result

https://www.youtube.com/watch?v=Z2ijE1UYdnw



PaulS

Quote
The condition is press button 1 relay 1 on and wait unit till press button 1 relay 1 off.
Condition? Do you mean requirement?

Quote
How to fix it.
That depends on how it is broken.

Code: [Select]
if(wifiMulti.run() != WL_CONNECTED) {

        Serial.println("WiFi not connected!");
        delay(1000);
    }

Is something magic supposed to happen while you have your head stuck in the sand?

Code: [Select]
    // turn LED on:
    digitalWrite(Relay1, HIGH);

Why on earth would you name the pin that the LED is connected to Relay1?

The art of getting good answers lies in asking good questions.

moneyp1000

Quote
The condition is  relay 1 on and wait unit till press button 1 relay 1 off.
i mean

press button 1 , one  time => Relay/LED 1 on

Press button 1 again  => Relay/LED 1 off


Quote
Why on earth would you name the pin that the LED is connected to Relay1?
sonoff connect by series Relay 1 and led 1 with GPIO 12

Code: [Select]
  GPIO      function                        schematic
   ---------------------------------------------------
   GPIO  0   Button 1                        E-FW
   GPIO  9   Button 2                        IO  9
   GPIO 10   Button 3                        IO 10
   GPIO 14   Button 4                        IO 14

   GPIO 12   Relay 1                         IO 12
   GPIO  5   Relay 2                         IO  5
   GPIO  4   Relay 3                         IO  4
   GPIO 15   Relay 4                         IO 15

   GPIO  2   Pin 1 Prog Header               SDA
   GPIO 13   Blue LED, Active Low            PWM 1

   GPIO  7   Not connected, jack (see TH16)  GPIO 07
   GPIO  8   Not connected, jack (see TH16)  GPIO 08

   3v3 - jack sleeve
   gnd - jack ring2

Go Up