[SOLVED] ESP8266 as a wifi shield of Arduino Uno to control a 6 channel Relay

Hello guys, Hope you all are doing all sort of amazing things with blynk.

As you may have understood it by the title of the topic, I am trying to control my 6 channel relay with arduino uno using esp8266 as its wifi shield. After a thorough study, I came up with some sort of code. Can you guys please review it and see if I am on the write track? I am new to this so thought you guys can point me towards the right direction. Will the libraries I have used do the job ?

Any feedback is highly appreciated and welcomed.

Thanks a bunch.

Here is my code:

        #include <ESP8266_Lib.h>
        #include <BlynkSimpleShieldEsp8266.h> // include Blynk ESP8266

        char auth[] = "YourAuthToken";
        char ssid[] = "username";
        char pass[] = "YourPassword";

        // Hardware Serial - Set ESP8266 Serial object

        #define EspSerial Serial
        #define ESP8266_BAUD 115200
        ESP8266 wifi(&EspSerial);

        //Relay in Pin XX
        #define Relay1 5
        #define Relay2 6
        #define Relay3 7
        #define Relay4 8
        #define Relay5 13
        #define Relay6 12

        // This function will be called every time Slider Widget
        // in Blynk app writes values to the Virtual Pin 1

        BLYNK_WRITE(V1)
        {
          int RelayStatus1 = param.asInt();
          if (RelayStatus1 == 1){
              digitalWrite(Relay1, HIGH);
              }
          else {
              digitalWrite(Relay1, LOW); 
              }
        }

        BLYNK_WRITE(V2)
        {
          int RelayStatus2 = param.asInt();
          if (RelayStatus2 == 1){
              digitalWrite(Relay2, HIGH);
              }
          else {
              digitalWrite(Relay2, LOW); 
              }
        }

        BLYNK_WRITE(V3)
        {
          int RelayStatus3 = param.asInt();
          if (RelayStatus3 == 1){
              digitalWrite(Relay3, HIGH);
              }
          else {
              digitalWrite(Relay3, LOW); 
              }
        }


        BLYNK_WRITE(V4)
        {
          int RelayStatus4 = param.asInt();
          if (RelayStatus4 == 1){
              digitalWrite(Relay4, HIGH);
              }
          else {
              digitalWrite(Relay4, LOW); 
              }
        }

        BLYNK_WRITE(V5)
        {
          int RelayStatus5 = param.asInt();
          if (RelayStatus5 == 1){
              digitalWrite(Relay5, HIGH);
              }
          else {
              digitalWrite(Relay5, LOW); 
              }
        }

        BLYNK_WRITE(V6)
        {
          int RelayStatus6 = param.asInt();
          if (RelayStatus6 == 1){
              digitalWrite(Relay6, HIGH);
              }
          else {
              digitalWrite(Relay6, LOW); 
              }
        }

        void setup()
        {

          pinMode(Relay1, OUTPUT); // sets the digital pin as output
          digitalWrite(Relay1, LOW); // Prevents relays from starting up engaged 

          pinMode(Relay2, OUTPUT); // sets the digital pin as output
          digitalWrite(Relay2, LOW); // Prevents relays from starting up engaged

          pinMode(Relay3, OUTPUT); // sets the digital pin as output
          digitalWrite(Relay3, LOW); // Prevents relays from starting up engaged

          pinMode(Relay4, OUTPUT); // sets the digital pin as output
          digitalWrite(Relay4, LOW); // Prevents relays from starting up engaged

          pinMode(Relay5, OUTPUT); // sets the digital pin as output
          digitalWrite(Relay5, LOW); // Prevents relays from starting up engaged

          pinMode(Relay6, OUTPUT); // sets the digital pin as output
          digitalWrite(Relay6, LOW); // Prevents relays from starting up engaged

          // Debug console
          Serial.begin(9600);

          // Set ESP8266 baud rate
          EspSerial.begin(ESP8266_BAUD);
          delay(10);

          Blynk.begin(auth, wifi, ssid, pass);
         
        }

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

Your sketch is not possible with a single Serial port device like an Uno.
Read the notes in the original sketch.

Can you elaborate it a bit please ? Sorry I am new to this

Are you referring to me using this for uno

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

Instead of this

       #define EspSerial Serial           
       ESP8266 wifi(&EspSerial);

Yes that’s the area that needs to be changed.

The Blynk sketch as it is written is for Mega’s and Leonardo’s that have more than one Serial port.

You need to mod the sketch for Uno use. Unless you can code with your eyes closed then it’s going to be difficult. The Uno mod is to fit a second serial port i.e. a USB 2 TTL adaptor (< $1).

Without one you will have no debug information. So if you don’t have an adaptor ESP will be using the Uno’s Serial port connection and you need to keep Serial Monitor closed.

I think this would be your starting sketch, review the changes to the standard Blynk sketch

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano... IF YOU HAVE A USB 2TTL ADAPTOR
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Uno, Nano without USB 2 TTL ADAPTOR
#define EspSerial Serial

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

void setup()
{
  // Set console baud rate
  //Serial.begin(9600);
  //delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
}

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

I would reduce the baud rate to 9600 or 19200… I was never success with higher rates using the ESP as shield
Remember that you MUST change the Baud Rate at the ESP too…

1 Like

so by commenting the following out of the code

Serial.begin(9600);

you are halting serial communication between ardiuno and the host computer ? If we just comment that out then wont it effect the communication of arduino with the computer ?

What my understanding is that using Software Serial library (that allows serial communication on other digital pins) to talk to the ESP8266 should solve the problem. The hardware serial wont go busy. Should be smooth running ?

Please have a look at the starting sketch.

    #include <ESP8266_Lib.h>
    #include <BlynkSimpleShieldEsp8266.h>

    char auth[] = "YourAuthToken";
    char ssid[] = "YourNetworkName";
    char pass[] = "YourPassword";

    #include <SoftwareSerial.h>
    SoftwareSerial EspSerial(2, 3); // RX, TX 

    // Your ESP8266 baud rate:
    #define ESP8266_BAUD 115200

    ESP8266 wifi(&EspSerial);

    void setup()
    {
      // Set console baud rate
       Serial.begin(9600);
       delay(10);

      // Set ESP8266 baud rate
      EspSerial.begin(ESP8266_BAUD);
      delay(10);

      Blynk.begin(auth, wifi, ssid, pass);
    }

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

Your views are welcome.

@Junaid_Dar have you tried your sketch and if so what happens?

Thank you for mentioning that, I had this in my mind. I would most definitely experiment with the baud rates first. 9600 seems to me the most popular one.

I am missing two components actually

  • Bi-directional I2C Logic Level Converter
  • AMS1117 Voltage Regulator

to complete my setup. Should receive them in a day or two.

Will update this post then.

So finally did find time to try the sketch, worked like a charm. Had no issues.

1 Like

@Junaid_Dar could you please post your working code.

Sure, here it is

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h> // include Blynk ESP8266
#define BLYNK_PRINT Serial  // Comment this out to disable prints and save space

char auth[] = "***************************";
char ssid[] = "********";
char pass[] = "********";

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // ( RX, TX )
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);

//Relay in Pin XX
#define Relay1 10
#define Relay2 9
#define Relay3 8
#define Relay4 7
#define Relay5 6
#define Relay6 5

// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
  int RelayStatus1 = param.asInt();
  if (RelayStatus1 == 1) {
    digitalWrite(Relay1, LOW);
  }
  else {
    digitalWrite(Relay1, HIGH);
  }
}

BLYNK_WRITE(V2)
{
  int RelayStatus2 = param.asInt();
  if (RelayStatus2 == 1) {
    digitalWrite(Relay2, LOW);
  }
  else {
    digitalWrite(Relay2, HIGH);
  }
}

BLYNK_WRITE(V3)
{
  int RelayStatus3 = param.asInt();
  if (RelayStatus3 == 1) {
    digitalWrite(Relay3, LOW);
  }
  else {
    digitalWrite(Relay3, HIGH);
  }
}


BLYNK_WRITE(V4)
{
  int RelayStatus4 = param.asInt();
  if (RelayStatus4 == 1) {
    digitalWrite(Relay4, LOW);
  }
  else {
    digitalWrite(Relay4, HIGH);
  }
}

BLYNK_WRITE(V5)
{
  int RelayStatus5 = param.asInt();
  if (RelayStatus5 == 1) {
    digitalWrite(Relay5, LOW);
  }
  else {
    digitalWrite(Relay5, HIGH);
  }
}

BLYNK_WRITE(V6)
{
  int RelayStatus6 = param.asInt();
  if (RelayStatus6 == 1) {
    digitalWrite(Relay6, LOW);
  }
  else {
    digitalWrite(Relay6, HIGH);
  }
}

void setup()
{

  pinMode(Relay1, OUTPUT); // sets the digital pin as output
  digitalWrite(Relay1, HIGH); // Prevents relays from starting up engaged

  pinMode(Relay2, OUTPUT); // sets the digital pin as output
  digitalWrite(Relay2, HIGH); // Prevents relays from starting up engaged

  pinMode(Relay3, OUTPUT); // sets the digital pin as output
  digitalWrite(Relay3, HIGH); // Prevents relays from starting up engaged

  pinMode(Relay4, OUTPUT); // sets the digital pin as output
  digitalWrite(Relay4, HIGH); // Prevents relays from starting up engaged

  pinMode(Relay5, OUTPUT); // sets the digital pin as output
  digitalWrite(Relay5, HIGH); // Prevents relays from starting up engaged

  pinMode(Relay6, OUTPUT); // sets the digital pin as output
  digitalWrite(Relay6, HIGH); // Prevents relays from starting up engaged

  // communication with the host computer
  Serial.begin(9600);
  delay(10);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

}

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

this coding working?

@awegadget Try it :wink:

1 Like