Suggestions

:joy::joy::joy::joy::joy:

Whats the problem?

Pete.

Oh… that GIF ‘relay’ hurts my eyes (and head) :laughing:

1 Like

Me Pete, I’m the problem. I’ve seen road kill with better coding skills than me … :confounded:

However, I refuse to let it beat me.
I’ve cheated, searched this forum and found some snippets of code. Fingers crossed I’ll have at least 1 relay operating via a button on my phone by morning. :exploding_head:

2 Likes

Hey… I taut we was fwends :stuck_out_tongue:

I thought you have all the relays running in sequence quite nicely, should be relatively simple to call some or all of that from Blynk…

Post yer code, and all :smiley:

1 Like

Hell Yeh! … :+1:

I did it! :star_struck:

Well I sort of did it. :grimacing:

After reading all the comments in this post I decided to use @dutchman’s code as a base and build off it. My version of it isn’t the best, and it’s a bit laggy but its works. :smiley:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include "credentials.h"

BlynkTimer timer;

int relay1 = 14;
int relay2 = 12;
int relay3 = 13;
int relay4 = 15;
int relay5 = 2;
int relay6 = 0;
int relay7 = 4;
int relay8 = 5;

int relayVButton1 = 0;
int relayVButton2 = 0;
int relayVButton3 = 0;
int relayVButton4 = 0;
int relayVButton5 = 0;
int relayVButton6 = 0;
int relayVButton7 = 0;
int relayVButton8 = 0;

boolean relayState1 = 1;
boolean relayState2 = 1;
boolean relayState3 = 1;
boolean relayState4 = 1;
boolean relayState5 = 1;
boolean relayState6 = 1;
boolean relayState7 = 1;
boolean relayState8 = 1;


WidgetLED led1(10); //virtual led
WidgetLED led2(11); //virtual led
WidgetLED led3(12); //virtual led
WidgetLED led4(13); //virtual led
WidgetLED led5(14); //virtual led
WidgetLED led6(15); //virtual led
WidgetLED led7(16); //virtual led
WidgetLED led8(17); //virtual led

void setup()
{
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass, server, 8080);

  timer.setInterval(300L, somefunction);

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);

  digitalWrite(relay1, 1);
  digitalWrite(relay2, 1);
  digitalWrite(relay3, 1);
  digitalWrite(relay4, 1);
  digitalWrite(relay5, 1);
  digitalWrite(relay6, 1);
  digitalWrite(relay7, 1);
  digitalWrite(relay8, 1);
}

void somefunction()
{

  if (relayVButton1 > 0)
  {
    relayState1 = !relayState1;
  }
  digitalWrite(relay1, relayState1);


  if (relayVButton2 > 0)
  {
    relayState2 = !relayState2;
  }
  digitalWrite(relay2, relayState2);


  if (relayVButton3 > 0)
  {
    relayState3 = !relayState3;
  }
  digitalWrite(relay3, relayState3);

  if (relayVButton4 > 0)
  {
    relayState4 = !relayState4;
  }
  digitalWrite(relay4, relayState4);


  if (relayVButton5 > 0)
  {
    relayState5 = !relayState5;
  }
  digitalWrite(relay5, relayState5);


  if (relayVButton6 > 0)
  {
    relayState6 = !relayState6;
  }
  digitalWrite(relay6, relayState6);

  if (relayVButton7 > 0)
  {
    relayState7 = !relayState7;
  }
  digitalWrite(relay7, relayState7);


  if (relayVButton8 > 0)
  {
    relayState8 = !relayState8;
  }
  digitalWrite(relay8, relayState8);


  if (digitalRead(relay1) == LOW)
  {
    led1.on();
  }
  else
    led1.off();

  if (digitalRead(relay2) == LOW)
  {
    led2.on();
  }
  else
    led2.off();

  if (digitalRead(relay3) == LOW)
  {
    led3.on();
  }
  else
    led3.off();

  if (digitalRead(relay4) == LOW)
  {
    led4.on();
  }
  else
    led4.off();

  if (digitalRead(relay5) == LOW)
  {
    led5.on();
  }
  else
    led5.off();

  if (digitalRead(relay6) == LOW)
  {
    led6.on();
  }
  else
    led6.off();

  if (digitalRead(relay7) == LOW)
  {
    led7.on();
  }
  else
    led7.off();

  if (digitalRead(relay8) == LOW)
  {
    led8.on();
  }
  else
    led8.off();

}

BLYNK_WRITE(V1)
{
  relayVButton1 = param.asInt();
}
BLYNK_WRITE(V2)
{
  relayVButton2 = param.asInt();
}
BLYNK_WRITE(V3)
{
  relayVButton3 = param.asInt();
}
BLYNK_WRITE(V4)
{
  relayVButton4 = param.asInt();
}
BLYNK_WRITE(V5)
{
  relayVButton5 = param.asInt();
}
BLYNK_WRITE(V6)
{
  relayVButton6 = param.asInt();
}
BLYNK_WRITE(V7)
{
  relayVButton7 = param.asInt();
}
BLYNK_WRITE(V8)
{
  relayVButton8 = param.asInt();
}

void loop() {
  Blynk.run();
  timer.run();
}
1 Like

man, try to use arrays + for cycles for tasks like this! the above solution is soo lame and not scalable. think about it, what if next time you need to use 500 relays / anythings? you would manually declare 500 variables? no…

study this topic:

and arrays in general:

1 Like

I did look at arrays yesterday but gave up as it just complicated getting the code working. I’m still very much a noob plus it’s only 8, not 500. I found the code easier to follow and the errors I made easier to find by having 8 of each.

However, you are correct so now I know the code works I will attempt to use arrays instead. :grimacing:

they seems complicated at the beginning, but after you understand the basic principle, it is quite logical. just do not forget that the first element index is always 0, not 1. this can cause most of the confusion for beginners.

but you can write much cleaner code with them :wink:
good luck with the learning!

1 Like

I think I’m going to need lots more luck … :joy::joy:

Got to site early this morning so I’d have a little time to try and solve this array thing before starting work. Unfortunately 5 guys want to leave early so they turned up early too on the off chance I would be here, so not had much Blynk time this morning. :unamused:

I think I’m close, but no cigar. :frowning:
So to cheer myself up I’ve ordering 8 small 12v water pumps that I will connect to these relays(once I figure out this array thing) and make a simple version of this:

EDIT.
One of the labourers has just reversed the Manitou into the trench(footings) we’re about to pour concrete into, crushed the reinforcement. Great start to the day. :disappointed_relieved:

So if anyone would like to take a look and tell me whats wrong while I’ll clean up this mess I’d really appreciate it because now its not working at all …

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include "credentials.h"

BlynkTimer timer;

int relay[] = {14, 12, 13, 15, 2, 0, 4, 5};

int relayVButton[] = {0};

boolean relayState[] = {1};

WidgetLED led[] = {10, 11, 12, 13, 14, 15, 16, 17};

int count = 0;

void setup() {
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass, server, 8080);

  timer.setInterval(300L, somefunction);

  for (count = 0; count < 7; count++) {
    pinMode(relay[count], OUTPUT);
  }
  for (count = 0; count < 7; count++) {
    digitalWrite(relay[count], 1);

  }
}

void somefunction() {

  if (relayVButton[count] > 0)
  {
    relayState[count] = !relayState[count];
  }
  digitalWrite(relay[count], relayState[count]);


  if (digitalRead(relay[count]) == LOW)
  {
    led[count].on();
  }
  else
    led[count].off();
}

BLYNK_WRITE(V1)
{
  relayVButton[count] = param.asInt();
}
BLYNK_WRITE(V2)
{
  relayVButton[count] = param.asInt();
}
BLYNK_WRITE(V3)
{
  relayVButton[count] = param.asInt();
}
BLYNK_WRITE(V4)
{
  relayVButton[count] = param.asInt();
}
BLYNK_WRITE(V5)
{
  relayVButton[count] = param.asInt();
}
BLYNK_WRITE(V6)
{
  relayVButton[count] = param.asInt();
}
BLYNK_WRITE(V7)
{
  relayVButton[count] = param.asInt();
}
BLYNK_WRITE(V8)
{
  relayVButton[count] = param.asInt();
}

void loop() {
  Blynk.run();
  timer.run();
}

if this can help you, my non-blocking code

/***************** Library ESP and Blynk *****************/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

/***************** Library for OTA *****************/
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA
#include <ESP8266mDNS.h>

/***************** Library for real-time clock *****************/
#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;
WidgetRTC rtc;

/*************** Server *********************/
char auth[] = "xxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxxx";
char server[] = "192, 168, 0, 1";
IPAddress arduino_ip ( 192,  168,   0,  56);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,  168,   0,   254);
IPAddress subnet_mask(255, 255, 255,   0);


/*************** 4 Relays *********************/
int relay1 = 5;
int relay2 = 4;
int relay3 = 16;
int relay4 = 2;
int ON =0;
int OFF = 1;
int RelayBTN1 = 0;
int RelayBTN2 = 0;
int RelayBTN3 = 0;
int RelayBTN4 = 0;

/*************** LEDS *********************/
WidgetLED led1(10); //virtual led
WidgetLED led2(11); //virtual led
WidgetLED led3(12); //virtual led
WidgetLED led4(13); //virtual
WidgetLED led5(14); //virtual
WidgetLED led6(15); //virtual
WidgetLED led7(16); //virtual
WidgetLED led8(17); //virtual

/*************** Setup *********************/
void setup()
{
  Serial.begin(115200);
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);
  Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 0, 1), 8088);
  //Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 0, 1), 8442);

  /************************ OTA ***************/
  ArduinoOTA.setHostname("Nodemcu02"); //OTA Set the name of the network port
  ArduinoOTA.setPassword((const char *)"0000"); //OTA Set access password for remote firmware
  ArduinoOTA.begin(); //Initialize OTA
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
}

/*************** buttons *********************/
BLYNK_WRITE(V1)
{
  RelayBTN1 = param.asInt(); 
  if (RelayBTN1 == true) {
    digitalWrite(relay1, ON);
    Serial.println("relay1 on");
    led1.on();
    led2.off();

  }
  else {
    digitalWrite(relay1,OFF);
    Serial.println("relay1 off");
    led1.off();
    led2.on();
  }
}

BLYNK_WRITE(V2)
{
  RelayBTN2 = param.asInt();
  if (RelayBTN2 == true) {
    digitalWrite(relay2,ON);
    Serial.println("relay2 on");
    led3.on();
    led4.off();

  }
  else {
    digitalWrite(relay2,OFF);
    Serial.println("relay2 off");
    led3.off();
    led4.on();
  }
}

BLYNK_WRITE(V3)
{
  RelayBTN3 = param.asInt(); 
  if (RelayBTN3 == true) {
    digitalWrite(relay3,ON);
    Serial.println("relay3 on");
    led5.on();
    led6.off();

  }
  else {
    digitalWrite(relay3,OFF);
    Serial.println("relay3 off");
    led5.off();
    led6.on();
  }
}

BLYNK_WRITE(V4)
{
  RelayBTN4 = param.asInt(); 
  if (RelayBTN4 == true) {
    digitalWrite(relay4,ON);
    Serial.println("relay4 on");
    led7.on();
    led8.off();
  }
  else {
    digitalWrite(relay4,OFF);
    Serial.println("relay4 off");
    led7.off();
    led8.on();
  }
}


void loop() {
  ArduinoOTA.handle();  // For OTA
  Blynk.run();
  timer.run();
  ESP.wdtFeed();
}

Video_2018-04-21_095702

1 Like

Thanks, I’ll take a look later …

1 Like

Buttons are in switch mode :wink:

1 Like

To try and make @wanek array thing easier I used part of @Blynk_Coeur code, only 1 relay, 1 button and 2 LED’s. The relay works as expected from the button in the Blynk app. But I can’t figure out whats stopping the LED’s in the app switching when the button is pressed. LED(10) is red when the button is off as I wanted it to be but LED(11) doesn’t turn green when the button is pressed? :frowning:

I’m convinced its related to this:

But can’t figure it out. Can someone please point me in right direction. :pray:

/***************** Library ESP and Blynk *****************/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include "credentials.h"

BlynkTimer timer;

int count = 0;

/*************** 1 Relay *********************/
int relay1 = 14;
int ON = 0;
int OFF = 1;
int RelayBTN1 = 0;

/*************** 2 LEDS *********************/
WidgetLED led[] = {10, 11};

/*************** Setup *********************/
void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass, server, 8080);

  pinMode(relay1, OUTPUT);
}

/*************** 1 Virtual Buttons *********************/
BLYNK_WRITE(V1) {
  for (count = 0; count < 1; count++) {
    RelayBTN1 = param.asInt();
    if (RelayBTN1 == true) {
      digitalWrite(relay1, ON);
      led[count].on();
      led[count].off();
    }

    else {
      digitalWrite(relay1, OFF);
      led[count].off();
      led[count].on();
    }
  }
}
void loop() {
  Blynk.run();
  timer.run();
}

@Shadeyman
I don’t understand why do you use for next statements ?

when count = 0, you have led[0] , and led[1] off and on when count = 1

never led[10] , led [11]

try this

led[count+10].on();
led[count+11].off();

do you need to blink leds ? if yes it is not the good way

@Blynk_Coeur
I’m trying to learn how to use an “Array”, like @wanek had previously suggested. As I find them a little complicated I thought using only 1 relay, 1 button and 2 LED’s in the Array would simplify things for me. As you can see, it didn’t work out as I’d planned, I’m still confused … :exploding_head:

EDIT.

How do I correct this ?

Like this?

led[count+10].on();
led[count+11].off();

yes

1 Like

This array/count thing has me beat. I’m close but no cigar …

1 Like


:wink:
Principle is the same even if java

2 Likes