0

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

Hi,

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()
{
  Serial.begin(9600);

  //Connect to WiFi
  Serial.print("\n\nConnecting Wifi... ");
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }

  Serial.println("OK!");

  //Subscribe to the onoff feed
  mqtt.subscribe(&onoff);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  // set the speed at 60 rpm:
  myStepper.setSpeed(90);
}

void loop()
{
  MQTT_connect();
 
  //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:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
      }
      if (!strcmp((char*) onoff.lastread, "OFF"))
      {
        digitalWrite(LED_BUILTIN, HIGH);
        // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
      }
    }
  }

  // ping the server to keep the mqtt connection alive
  if (!mqtt.ping())
  {
    mqtt.disconnect();
  }
}


/***************************************************
  Adafruit MQTT Library ESP8266 Example

  Must use ESP8266 Arduino from:
    https://github.com/esp8266/Arduino

  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())
  {
    return;
  }

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

  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();
       delay(5000);  // wait 5 seconds
       retries--;
       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.

markvanhaze
 
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?

adafruit_support_mike
 
Posts: 53063
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.