Google Assistant Controlled Stepper Motor Using IFTTT and Adafruit IO on ESP32

Hey Google, open the gate!

Introduction

I did a tutorial related to Google Assistant before using Raspberry Pi. The operation is quite simple, just control the signal on GPIO pin to be HIGH and LOW. This time I would like to try to control a stepper motor using IFTTT and Adafruit IO on ESP32 board. In this case, the stepper motor will control my mini gate mechanism to open and close.

Before proceed to the video tutorial, please go through following tutorial first.

  1. Control Stepper Motor 28BYJ-48 Using TTGO T-Display ESP32
  2. Send Sensor Data to Adafruit IO Using ESP32

Video

This video will show you how to a control stepper motor using ESP32 through Google Assistant.

Hardware Preparation

This is the list of items used in the video.

For 3D printed part, you can download it here ? 28byj-48 Motor Halter ?

Sample Program

This is the Arduino sample program that uses MQTT protocol to subscribe topic on Adafruit IO, thus controlling the stepper motor. Before compile, please install following library on Library Manager.

  • Adafruit MQTT Library by Adafruit Version 1.0.3
  • TFT_eSPI by Bodmer Version 1.4.20
/*
Tutorial: Control Stepper Motor Through Google Assistant Using IFTTT and Adafruit IO on ESP32
Board:
– TTGO T-Display ESP32 1.14 Display Module
https://my.cytron.io/p-ttgo-t-display-esp32-1.14-display-module-presolder-header
Actuator:
– 12V 28BYJ-48 Stepper Motor
https://my.cytron.io/c-motor-and-motor-driver/c-dc-motor/c-stepper-motor/p-12v-28byj-48-stepper-motor-plus-uln2003-driver-board
Connection Battery | Stepper
12V – +ve
ESP32 | Stepper
GND – -ve
33 – IN1
25 – IN2
26 – IN3
27 – IN4
External libraries:
– Adafruit MQTT Library by Adafruit Version 1.0.3
– TFT_eSPI by Bodmer Version 1.4.20
Created by:
17 Jan 2020 Idris Zainal Abidin, Cytron Technologies
*/
#include <TFT_eSPI.h>
#include <SPI.h>
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID "YourWiFiSSID"
#define WLAN_PASS "YourWiFiPassword"
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "YourAdafruitIOUsername"
#define AIO_KEY "YourAdafruitIOKey"
#define BUTTON1 35
#define BUTTON2 0
#define LED_GREEN 15
#define LED_RED 17
#define STEPPER_IN1 33
#define STEPPER_IN2 25
#define STEPPER_IN3 26
#define STEPPER_IN4 27
#define REVOLUTION_STEP 2048 // 1 rotation = 360 degree
#define FF17 &FreeSans9pt7b
#define FF21 &FreeSansBold9pt7b
#define ROW1 0,16
#define ROW2 0,38
#define ROW3 0,60
#define ROW4 0,82
#define ROW5 0,104
#define ROW6 0,126
WiFiClient client; // Create an ESP8266 WiFiClient class to connect to the MQTT server.
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe ledControl = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/ledControl");
TFT_eSPI tft = TFT_eSPI();
boolean stepperDirection = false;
int stepperStep = 0;
int stepperStepCount = 0;
boolean stepperMove = false;
int intervalStepper = 2; // Minimum is 2
void setup()
{
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(STEPPER_IN1, OUTPUT);
pinMode(STEPPER_IN2, OUTPUT);
pinMode(STEPPER_IN3, OUTPUT);
pinMode(STEPPER_IN4, OUTPUT);
Serial.begin(115200);
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setFreeFont(FF17);
tft.setTextColor(TFT_YELLOW);
tft.setCursor(ROW1);
tft.print(WLAN_SSID);
tft.setCursor(ROW2);
tft.print("Connecting…");
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
tft.fillScreen(TFT_BLACK);
tft.setCursor(ROW1);
tft.print(WLAN_SSID);
tft.setCursor(ROW2);
tft.print(WiFi.localIP());
tft.setCursor(ROW3);
tft.print(AIO_SERVER);
tft.setCursor(ROW4);
tft.setTextColor(TFT_WHITE);
tft.print("—————————————");
tft.setTextColor(TFT_BLUE);
tft.setCursor(ROW5);
tft.print("MQTT:");
tft.setCursor(ROW6);
tft.print("Gate:");
tft.setTextColor(TFT_MAGENTA);
tft.setCursor(70, 126);
tft.print("Close");
mqtt.subscribe(&ledControl);
}
boolean button1Pressed = false;
boolean button2Pressed = false;
boolean ledState = LOW;
void loop()
{
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &ledControl) {
Serial.print(F("Got: "));
Serial.println((char *)ledControl.lastread);
if (!strcmp((char*) ledControl.lastread, "ON")) {
digitalWrite(LED_GREEN, HIGH);
tft.fillRect(70, 114, 170, 17, TFT_BLACK);
tft.setCursor(70, 126);
tft.print("In progress…");
stepperDirection = false;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;
}
else {
digitalWrite(LED_GREEN, HIGH);
tft.fillRect(70, 114, 170, 17, TFT_BLACK);
tft.setCursor(70, 126);
tft.print("In progress…");
stepperDirection = true;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;
}
}
}
ledState = !ledState;
digitalWrite(LED_RED, ledState);
if (digitalRead(BUTTON1) == LOW) {
stepperDirection = false;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;
}
if (digitalRead(BUTTON2) == LOW) {
stepperDirection = true;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;
}
while (stepperMove == true) {
if (stepperDirection) {
if (stepperStep++ >= 3) {
stepperStep = 0;
}
}
else {
if (stepperStep– == 0) {
stepperStep = 3;
}
}
if (stepperStepCount++ == REVOLUTION_STEP) {
stepperMove = false;
stepperStep = 4;
digitalWrite(LED_GREEN, LOW);
if (stepperDirection == false) {
tft.fillRect(70, 114, 170, 17, TFT_BLACK);
tft.setCursor(70, 126);
tft.print("Open");
}
else {
tft.fillRect(70, 114, 170, 17, TFT_BLACK);
tft.setCursor(70, 126);
tft.print("Close");
}
}
switch (stepperStep) {
case 0:
digitalWrite(STEPPER_IN1, HIGH);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, LOW);
break;
case 1:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, HIGH);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, LOW);
break;
case 2:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, HIGH);
digitalWrite(STEPPER_IN4, LOW);
break;
case 3:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, HIGH);
break;
default:
digitalWrite(STEPPER_IN1, LOW);
digitalWrite(STEPPER_IN2, LOW);
digitalWrite(STEPPER_IN3, LOW);
digitalWrite(STEPPER_IN4, LOW);
break;
}
delay(3);
}
}
void MQTT_connect()
{
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT… ");
tft.fillRect(70, 92, 170, 17, TFT_BLACK);
tft.setCursor(70, 104);
tft.print("Connecting…");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds…");
mqtt.disconnect();
tft.fillRect(70, 92, 170, 17, TFT_BLACK);
tft.setCursor(70, 104);
tft.print("Disconnected");
delay(5000); // wait 5 seconds
retries–;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
tft.fillRect(70, 92, 170, 17, TFT_BLACK);
tft.setCursor(70, 104);
tft.print("Connected");
}

Thank You

References:

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

3 thoughts on “Google Assistant Controlled Stepper Motor Using IFTTT and Adafruit IO on ESP32”

  1. I made project with esp 32 instead of TTGO T-DisplayESP32, Now i am facing an issue that the motor running only one direction frequently with delay of 8 seconds, but the esp is connected to mqtt. please help me to find out the issue

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Build Otto DIY Robot Using Maker Pi RP2040
Raspberry Pi RP2040 vs STM32F1 vs SAMD21G18 vs ESP32-S2
The Easiest Way to Print Temperature Tower Using Cura 4.9.1 Plugins
IoT on Raspberry Pi Pico using CircuitPython and Adafruit IO
ESP32 as WiFi co-processor for Raspberry Pi Pico
Tutorials of Cytron Technologies Scroll to Top