ESP8266 + 1 channel relay + arduino uno

Hi guys , I am new with arduino uno and also this kind of electronic stuff . I not really sure where to upload the code . to arduino or esp8266? Do I need to change the programmer on tools ?
I have problem :

  1. if I upload the coding1 to esp8266 , it has connection but the relay doesn’t response

  2. if I upload my other coding to arduino , the is no connection at all

I hope you all can help me :slight_smile:
I already review some of the topic similar to mine but it didn’t solve the problem

Coding for arduino

#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[] = "auth key";
char ssid[] = "wifi name ";
char pass[] = "wifi pass";

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

//Relay in Pin XX
#define Relay1 8

// 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);
  }
}



void setup()
{

  pinMode(Relay1, OUTPUT); // sets the digital pin as output
  digitalWrite(Relay1, 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 name", "pass");

}

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

coding for esp8266 ( board generic module esp 8266)

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

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "auth key";

// Your WiFi credentials.
// Set password to "" for open networks.
const char ssid[] = "wifi name";
const char pass[] = "wifi pass";



int relay_1 = 2; 

void setup()
{
  pinMode(relay_1,OUTPUT);
  digitalWrite(relay_1,V1);
  
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, "wifi name ", "wifi pass"); //insert here your SSID and password

  while (Blynk.connect() == false) 
  {
    // Wait until connected
  }
  Serial.print(" Blynk Setup ");
  
  // Setup a function to do reconnection attempts once a minute
 
}

BLYNK_WRITE(V1)
{
  pinMode(relay_1,OUTPUT);
  if(digitalRead(relay_1)==LOW) //Low = on relay
  {
    digitalWrite(relay_1,HIGH); // high = relay is switch off
    Serial.print(" Pin set to HIGH (OFF) ");
  }

  else //else if relay is off
  {
    digitalWrite(relay_1,LOW); //will be set to on
    Serial.print(" Pin set to LOW (ON) ");
  }

  pinMode(relay_1,INPUT);
}

void reconnectBlynk()
{
  if (!Blynk.connected())
  {
    if (Blynk.connect())
    {
      Serial.print(" Blynk Reconnected ");
    }
    else
    {
      Serial.print(" Blynk Not reconnected ");
    }
  }
}



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

Did you follow the Help Center directions?

ESP8266 with AT firmware essentially becomes just a simple a WiFi to Serial adapter, so all your code is installed on the UNO and in the App you set the device as UNO and connection method as WiFi.

1 Like

it say to upload code at esp
http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/arduino/esp8266-with-at-firmware

:frowning: im not really sure

it looks like you are using an ESP-01, so unless you have managed to flash it already with something else, it should already have the correct (or correct enough) AT firmware. You will need to set its BAUD rate to 9600 BAUD as per the documentation.

when I upload the esp8266 coding to “board : generic esp8266 module” it has connection but the relay doesn’t respond as I want it :frowning:

Aside from the AT firmware, you don’t upload anything else to the ESP-01… you upload to the Arduino through the Arduino’s USB port and connect to Blynk Cloud through the ESP (as an WiFi adapter).

Okay , thanks .

Can you help me check my code ? :smiley:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

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

char auth[] = "auth"; //get authentication key from mail


char ssid[] = "name"; // your wifi name
char pass[] = "pass"; // your wifi password 


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

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

#define relay1 8

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

void setup()
{
  // Debug console
  Serial.begin(9600);

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

  Blynk.begin(auth, wifi, "name" , "pass");

  pinMode(relay1,OUTPUT);
  digitalWrite(relay1,HIGH);

}

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