Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Last active January 21, 2020 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IdrisCytron/a0a03bd097dd89949fc7d7629ab5fca2 to your computer and use it in GitHub Desktop.
Save IdrisCytron/a0a03bd097dd89949fc7d7629ab5fca2 to your computer and use it in GitHub Desktop.
Control Stepper Motor Through Google Assistant Using IFTTT and Adafruit IO on ESP32
/*
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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment