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();
}
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…
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 ?
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.