Question about Huzzah Stepper contr. via IO/MQTT
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Question about Huzzah Stepper contr. via IO/MQTT

by markvanhaze on Sun Dec 17, 2017 11:08 am


I need a pointer or two regarding a project with a Huzzah running a Nema17 stepper motor via a driver and all of it controlled via MQTT on IO. I want the motor to turn one revolution in one direction if the switch in IO is set to "ON" and then one revolution in the other direction if the switch is set to "OFF".

Here is the code:

Code: Select all | TOGGLE FULL SIZE
#include <ESP8266WiFi.h>
#include <Stepper.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WIFI_SSID "xxxx"
#define WIFI_PASS "xxxx"

#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "xxxx"
#define MQTT_PASS "xxxx"

//Set up MQTT and WiFi clients
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);

//Set up the feed you're subscribing to
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff");

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 14, 12, 13, 15);

void setup()

  //Connect to WiFi
  Serial.print("\n\nConnecting Wifi... ");
  while (WiFi.status() != WL_CONNECTED)


  //Subscribe to the onoff feed

  digitalWrite(LED_BUILTIN, HIGH);

  // set the speed at 60 rpm:

void loop()
  //Read from our subscription queue until we run out, or
  //wait up to 5 seconds for subscription to update
  Adafruit_MQTT_Subscribe * subscription;
  while ((subscription = mqtt.readSubscription(5000)))
    //If we're in here, a subscription updated...
    if (subscription == &onoff)
      //Print the new value to the serial monitor
      Serial.print("onoff: ");
      Serial.println((char*) onoff.lastread);
      //If the new value is  "ON", turn the light on.
      //Otherwise, turn it off.
      if (!strcmp((char*) onoff.lastread, "ON"))
        //Active low logic
        digitalWrite(LED_BUILTIN, LOW);
        // step one revolution  in one direction:
      if (!strcmp((char*) onoff.lastread, "OFF"))
        digitalWrite(LED_BUILTIN, HIGH);
        // step one revolution in the other direction:

  // ping the server to keep the mqtt connection alive
  if (!mqtt.ping())

  Adafruit MQTT Library ESP8266 Example

  Must use ESP8266 Arduino from:

  Works great with Adafruit's Huzzah ESP board & Feather
  ----> https://www.adafruit.com/product/2471
  ----> https://www.adafruit.com/products/2821

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Tony DiCola for Adafruit Industries.
  MIT license, all text above must be included in any redistribution

void MQTT_connect()
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected())

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
       Serial.println("Retrying MQTT connection in 5 seconds...");
       delay(5000);  // wait 5 seconds
       if (retries == 0)
         // basically die and wait for WDT to reset me
         while (1);
  Serial.println("MQTT Connected!");

I also have the LED light up as control. Now what happens is that the motor does rotate when I have the IO switch set but only in one direction. Flipping the switch on IO does not rotate the motor in the other direction. If I reset the Huzzah, and start with the "OFF" value, the motor rotates in the other direction. So basically all is working but I must be overlooking something in the Arduino code.

Any help is greatly appreciated.

Posts: 13
Joined: Thu Nov 17, 2016 5:30 am

Re: Question about Huzzah Stepper contr. via IO/MQTT

by adafruit_support_mike on Wed Dec 20, 2017 5:26 am

Do you get the expected 'clockwise' and 'counterclockwise' debug messages when you set the switch online?

Posts: 46768
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.