Hi all, I’m currently writing a program to control the servo motor. I used relay as a normally open switch, the servo will alternates between 0 and 180 angle only if there is a high received by relay. The program ran perfectly with arduino uno. But when I try to control the servo through Blynk, it doesn’t work. Kindly help me to solve this.
Below is the code that run perfectly without Blynk and ESP8266:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int relay = 7;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(relay,OUTPUT);
}
void loop() {
digitalWrite(relay,1);
myservo.write(0) ; //servo rotate clockwise
delay(5000); // waits 5s for servo to move up
myservo.write(180); //servo rotate anticlockwise
delay(4800);// waits 4.8s for the servo to move down
}
Below is the Blynk code run with ESP8266:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>
// You should get Auth Token in the Blynk App.
char auth[] = "013bfc7a32a14c2fab205935658d8147";
char ssid[] = "xxxx";
char pass[] = "yyyy";
Servo myservo;
int relay = 7;
#define EspSerial Serial
#define ESP8266_BAUD 115200 //ESP8266 baud rate
ESP8266 wifi(&EspSerial);
void setup()
{
Serial.begin(9600); // Set console baud rate
delay(10);
EspSerial.begin(ESP8266_BAUD); // Set ESP8266 baud rate
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
pinMode(relay, OUTPUT);
myservo.attach(9);
}
void loop()
{
Blynk.run();
motor();
}
void motor()
{
if (digitalRead(relay) == HIGH)
{
myservo.write(0);
delay(5000);
myservo.write(180);
delay(4800);
}
else
{
myservo.detach();
}
}