Hey guys, my code is working fine with my Blynk app. It turns the light switch on and off like it should with my 3D printed mount to my light switch with a sg90 servo. The only problem with using the Blynk app is, to my knowledge, that i can only have one button prgrammed to a single pin at a time.
Originally i thought i could have 2 buttons one that turned the servo to 0* from 90* and then went back to 90* and then another that turned the servo from 90* to 180* and back to 90*.
The reason the servo needs to rest at the 90* position after turning the light on and off is because that allows manual operation of the light switch so you dont have to open the app up every time you walk into the room. Originally i had this working in my web based app.. see below code labeled 1.2 but i cant seem to get that code to mesh with my code for the blynk app.
1.2
void moveServoToOnPosition(void){
for(pos = 90; pos<=170; pos+=1) // goes from 90 degrees to 170 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 170; pos>=90; pos-=1) // goes from 170 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println(pos);
}
}
void moveServoToOffPosition(void){
for(pos = 90; pos>=0; pos-=1) // goes from 90 degrees to 0 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println(pos);
}
for(pos = 0; pos<=90; pos+=1) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println(pos);
}
}
void turnSwitchOnCb(){
moveServoToOnPosition();
server.send(200, "text/html", "ON!");
}
void turnSwitchOffCb(){
moveServoToOffPosition();
server.send(200, "text/html", "OFF!");
}
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
char auth[] = "09192f7ec5cd47fdb8ea5cae59f51ede";
char ssid[] = ""; //hidden for forum purposes
char pass[] = ""; //hidden for forum purposes
Servo servo;
BLYNK_WRITE(V3) {
servo.write(param.asInt());
}
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
servo.attach(2); // 2 means D4 pin of ESP8266
}
void loop(){
Blynk.run();
}