Servo + IFTTT

hello everyone,
i am trying to control a servo (controlled by a slider which activates the servo relay at 0 and 180) via ifttt (google assistant my case) the only way i could think of is:
-“PUT” via webhook the values 0 or 180 in the servo vpin (V25 my case)
-use eventor to activate the servo power relay at values 0 or 180. (works manually from the app, servo relay V21)

i already control normal vpins of relays (lights) successfully but i am not successful with the servo, since after i send the webhook the slider in the app does not change its position or value

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on NodeMCU.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right NodeMCU module
  in the Tools -> Board menu!

  For advanced settings please follow ESP examples :
   - ESP8266_Standalone_Manual_IP.ino
   - ESP8266_Standalone_SmartConfig.ino
   - ESP8266_Standalone_SSL.ino

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
// define the physical pins the relays are connected to. Make sure you use GPIO numbers and not D numbers. D6 = GPIO12 etc 
#define RELAY1_PIN 5  // D1
#define RELAY2_PIN 4  // D2
#define RELAY3_PIN 14  // D5
#define RELAY4_PIN 12  // D6
#define servoPin D0  // D0

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <ArduinoOTA.h>
#include <Servo.h>
#include <SPI.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "FRITZ!Box 3490";
char pass[] = "password";
char hostOTA[] = "OTAUpdates";
char passOTA[] = "1234";

BlynkTimer timer;

WidgetRTC rtc;

Servo servo;

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);
}

void turnRelayOff() {
    digitalWrite(RELAY1_PIN, HIGH);
    Blynk.virtualWrite(V21, LOW);
}

void uptimer()
{
  static unsigned long lastTimeUpdateMillis = 0;
  if(millis() - lastTimeUpdateMillis > 1000)
  {
    char str[15] = "";
    timeToString(str, sizeof(str));
    Blynk.virtualWrite(V3, str);
    lastTimeUpdateMillis = millis();;
  }
}

// t is time in seconds = millis()/1000;
void timeToString(char* string, size_t size)
{
  unsigned long nowMillis = millis();
  unsigned long seconds = nowMillis / 1000;
  int days = seconds / 86400;
  seconds %= 86400;
  byte hours = seconds / 3600;
  seconds %= 3600;
  byte minutes = seconds / 60;
  seconds %= 60;
  snprintf(string, size, "%04d:%02d:%02d:%02d", days, hours, minutes, seconds);
}

// Keep this flag not to re-sync on every reconnection
bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncVirtual(V21, V22, V23, V24, V25);
    isFirstConnect = false;
  }
  }


void setup()
{
  // Debug console
  Serial.begin(9600);
 
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  servo.attach(servoPin);
  
// Begin synchronizing time
  rtc.begin();

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html

  // Display digital clock every 1 seconds
  timer.setInterval(1000L, clockDisplay);
  
  // Setup a function to be called every second
  timer.setInterval(1517L, uptimer);
  
  ArduinoOTA.setHostname(hostOTA);
  //ArduinoOTA.setPassword(passOTA);
  ArduinoOTA.onStart([]() {
    Serial.println("OTA: Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nOTA: End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("OTA: Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("OTA: Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("OTA: Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("OTA: Ready");

  pinMode(RELAY1_PIN,OUTPUT);
  pinMode(RELAY2_PIN,OUTPUT);
  pinMode(RELAY3_PIN,OUTPUT);
  pinMode(RELAY4_PIN,OUTPUT);
  digitalWrite(RELAY1_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
  digitalWrite(RELAY2_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
  digitalWrite(RELAY3_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
  digitalWrite(RELAY4_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
}

BLYNK_WRITE(21){ // virtual pin 21... create a button widget in SWITCH mode
  if(param.asInt()){
    digitalWrite(RELAY1_PIN, LOW); // if button is HIGH, turn on relay1
    timer.setTimeout(780, turnRelayOff);
  } else {
    digitalWrite(RELAY1_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

BLYNK_WRITE(22){ // virtual pin 22... create a button widget in SWITCH mode
  if(param.asInt()){
    digitalWrite(RELAY2_PIN, LOW); // if button is HIGH, turn on relay1    
  } else {
    digitalWrite(RELAY2_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

BLYNK_WRITE(23){ // virtual pin 23... create a button widget in SWITCH mode
  if(param.asInt()){
    digitalWrite(RELAY3_PIN, LOW); // if button is HIGH, turn on relay1
  } else {
    digitalWrite(RELAY3_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

BLYNK_WRITE(24){ // virtual pin 24... create a button widget in SWITCH mode
  if(param.asInt()){
    digitalWrite(RELAY4_PIN, LOW); // if button is HIGH, turn on relay1
  } else {
    digitalWrite(RELAY4_PIN, HIGH); // if button is LOW, turn off relay1
  }

}  

BLYNK_WRITE(V25){
  Blynk.virtualWrite(V25, param.asInt());
  servo.write(param.asInt());
  }


void loop()
{
  Blynk.run();
  timer.run(); // BlynkTimer is working...
  ArduinoOTA.handle();
}

if you think of better ways please tell me

This is not a code repair forum, so your troubleshooting and Google skills and are you best resource. We can help with Blynk specific issues like how a command works, proper use of timers instead of delays, etc.

Start with breaking down the separate functions and systematically test them… E.g. If moving the slider works, then it is in your ifttt implementation, if the slider doesn’t work then the issue in with that code… and so on.

i have to say your advice (separate functions) has helped me, making me realize i was looking at the problem from the wrong angle so i changed my approach:

  • the webhook doesnt send the servo value anymore (now embed in the blynk code) but only the vpin state value (V26 and V27)

  • eventor doesnt activate the relay at certain pin value anymore

i wrote small sections for each specific action (close/open door) that are triggered by the state of a single VPin, and by so even brought the servo jittering to pratically 0. Works perfectly with IFTTT and google Assistant with the standard applet settings (…/pin/Vx — PUT — application.json — 1 or 0). here is the new full code hoping to help.

/*************************************************************
  Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator:           http://examples.blynk.cc
Blynk community:            http://community.blynk.cc
Social networks:            http://www.fb.com/blynkapp
                            http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on NodeMCU.

  Note: This requires ESP8266 support package:
https://github.com/esp8266/Arduino

  Please be sure to select the right NodeMCU module
  in the Tools -> Board menu!

  For advanced settings please follow ESP examples :
   - ESP8266_Standalone_Manual_IP.ino
   - ESP8266_Standalone_SmartConfig.ino
   - ESP8266_Standalone_SSL.ino

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
// define the physical pins the relays are connected to. Make sure you use GPIO numbers and not D numbers. D6 = GPIO12 etc 
#define RELAY1_PIN 5  // D1
#define RELAY2_PIN 4  // D2
#define RELAY3_PIN 14  // D5
#define RELAY4_PIN 12  // D6
#define servoPin D0  // D0

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <ArduinoOTA.h>
#include <Servo.h>
#include <SPI.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "0000000000";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "FRITZ!Box 3490";
char pass[] = "zeus1289";
char hostOTA[] = "OTAUpdates";
char passOTA[] = "1234";

BlynkTimer timer;

WidgetRTC rtc;

Servo servo;

WidgetLED led1(V21);

// Digital clock display of the time
void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();

  // Send time to the App
  Blynk.virtualWrite(V1, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V2, currentDate);
}

void turnRelayOff() {
digitalWrite(RELAY1_PIN, HIGH);
Blynk.virtualWrite(V21, LOW);
led1.off();
}

void uptimer()
{
  static unsigned long lastTimeUpdateMillis = 0;
  if(millis() - lastTimeUpdateMillis > 1000)
  {
char str[15] = "";
timeToString(str, sizeof(str));
Blynk.virtualWrite(V3, str);
lastTimeUpdateMillis = millis();;
  }
}

// t is time in seconds = millis()/1000;
void timeToString(char* string, size_t size)
{
  unsigned long nowMillis = millis();
  unsigned long seconds = nowMillis / 1000;
  int days = seconds / 86400;
  seconds %= 86400;
  byte hours = seconds / 3600;
  seconds %= 3600;
  byte minutes = seconds / 60;
  seconds %= 60;
  snprintf(string, size, "%04d:%02d:%02d:%02d", days, hours, minutes, seconds);
}

// Keep this flag not to re-sync on every reconnection
bool isFirstConnect = true;
BLYNK_CONNECTED() {
  if (isFirstConnect) {
Blynk.syncVirtual(V21, V22, V23, V24, V25);
isFirstConnect = false;
  }
  }


void setup()
{
  // Debug console
  Serial.begin(9600);
 
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);

  servo.attach(servoPin);
  
// Begin synchronizing time
  rtc.begin();

  // Other Time library functions can be used, like:
  //   timeStatus(), setSyncInterval(interval)...
  // Read more: http://www.pjrc.com/teensy/td_libs_Time.html

  // Display digital clock every 1 seconds
  timer.setInterval(1000L, clockDisplay);
  
  // Setup a function to be called every second
  timer.setInterval(1517L, uptimer);
  
  ArduinoOTA.setHostname(hostOTA);
  //ArduinoOTA.setPassword(passOTA);
  ArduinoOTA.onStart([]() {
Serial.println("OTA: Start");
  });
  ArduinoOTA.onEnd([]() {
Serial.println("\nOTA: End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("OTA: Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("OTA: Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("OTA: Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("OTA: Ready");

  pinMode(RELAY1_PIN,OUTPUT);
  pinMode(RELAY2_PIN,OUTPUT);
  pinMode(RELAY3_PIN,OUTPUT);
  pinMode(RELAY4_PIN,OUTPUT);
  digitalWrite(RELAY1_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
  digitalWrite(RELAY2_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
  digitalWrite(RELAY3_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
  digitalWrite(RELAY4_PIN, HIGH); // most modules are LOW active... so on boot start inactive (HIGH)
}

BLYNK_WRITE(21){ // virtual pin 21... create a button widget in SWITCH mode
  if(param.asInt()){
digitalWrite(RELAY1_PIN, LOW); // if button is HIGH, turn on relay1
led1.on();
timer.setTimeout(780, turnRelayOff);
  } else {
digitalWrite(RELAY1_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

BLYNK_WRITE(22){ // virtual pin 22... create a button widget in SWITCH mode
  if(param.asInt()){
digitalWrite(RELAY2_PIN, LOW); // if button is HIGH, turn on relay1    
  } else {
digitalWrite(RELAY2_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

BLYNK_WRITE(23){ // virtual pin 23... create a button widget in SWITCH mode
  if(param.asInt()){
digitalWrite(RELAY3_PIN, LOW); // if button is HIGH, turn on relay1
  } else {
digitalWrite(RELAY3_PIN, HIGH); // if button is LOW, turn off relay1
  }
}

BLYNK_WRITE(24){ // virtual pin 24... create a button widget in SWITCH mode
  if(param.asInt()){
digitalWrite(RELAY4_PIN, LOW); // if button is HIGH, turn on relay1
  } else {
digitalWrite(RELAY4_PIN, HIGH); // if button is LOW, turn off relay1
  }

}  

BLYNK_WRITE(26){ // virtual pin 26... APERTURA PORTA WEBHOOK
  if (param.asInt()) {
    //HIGH
   servo.write(180);
   digitalWrite(RELAY1_PIN, LOW); // if button is HIGH, turn on relay1
   led1.on();
   timer.setTimeout(780, turnRelayOff);
   digitalWrite(V26, LOW);
   Blynk.virtualWrite(V26, LOW);
   Blynk.virtualWrite(V25, 180);
}else {
   //LOW
      
}

} 

BLYNK_WRITE(27){ // virtual pin 27... CHIUSURA PORTA WEBHOOK
  if (param.asInt()) {
    //HIGH
   servo.write(0);
   digitalWrite(RELAY1_PIN, LOW); // if button is HIGH, turn on relay1
   led1.on();
   timer.setTimeout(780, turnRelayOff);
   digitalWrite(V27, 0);
   Blynk.virtualWrite(V27, LOW);
   Blynk.virtualWrite(V25, 0);
}else {
   //LOW
      
}

} 

BLYNK_WRITE(V25){
  servo.write(param.asInt());
  }


void loop()
{
  Blynk.run();
  timer.run(); // BlynkTimer is working...
  ArduinoOTA.handle();
}
1 Like

Thank you @dipippaf. You saved my day. I searched all over web and the only thing people said was to drop the idea use arduino. But you brought the little hope i had as I dont know how to code.

A post was merged into an existing topic: Not able to control its speed through ifttt (google assistance)

Can I get the cricuit connection for this project

no luck !
@dipippaf didn’t come here since nov 2017 .
:wink:

Hello, i will provide you pictures of the circuit connection as soon as i can but for this project i had used a ncu lua shield

hey, you’re alive ? :wink:

2 Likes