All Collections
IoT Projects Tutorials
Control a Stepper Motor remotely using Xinabox xChip OC06(PCA9554A DRV8825) and CW02(ESP32) using Ubidots over MQTT
Control a Stepper Motor remotely using Xinabox xChip OC06(PCA9554A DRV8825) and CW02(ESP32) using Ubidots over MQTT

Control a Stepper Motor remotely using xChip OC06 (PCA9554A I/O Expander & DRV8825 Stepper Motor Controller) and CW02 (ESP32) using Ubidots.

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

The ESP-WROOM-32 (ESP32) Core and Wi-Fi, Bluetooth module (CW02) allows users to send data from XinaBox’s modular xChips to the cloud, and control the device according to the obtained data.

The xCHIP OC06 uses DRV8825 Stepper Motor Controller to precisely drive and control a connected stepper motor. The DRV8825 is interfaced with I2C through a PCA9554A I/O Expander which provides all the required control signals.

By the end of this guide, you will able to control stepper motor with basic functionality to move the motor Forward or Reverse remotely using Ubidots and Xinabox xChips IP01, CW02 and OC06.

Requirements

Step-by-Step

  1. Hardware Setup

  2. Setting up the Arduino IDE

  3. Modify Ubidots ESP MQTT Library

  4. Create Switch Button Widget

  5. Coding

  6. Compile and Upload the code

  7. Summary

1. Hardware Setup

Connect CW02, OC06 and IP01 together using the XC10 xBUS connectors. You may connect it as shown in the diagram below. Please see this guide on how to assemble xChips generally.

Then connect your device and PC through the IP01’s USB.

2. Setting up the Arduino IDE

1. Install Arduino IDE 1.8.8.

2. Install these libraries/cores to Arduino:

NOTE: If you are not familiar with how to install libraries, please refer to the link: Installing Arduino libraries

3. With the ESP32 core installed, select the ESP32 device you are working with. In this case, we are working with a “CW02(ESP32 module)”. To select your board from the Arduino IDE, select Tools > Board “XinaBox CW02”.

3. Modify Ubidots ESP MQTT Library

Ubidots ESP MQTT Library is by default made for ESP8266. Few modification have to be made inside the UbidotsESPMQTT library to make it useable for ESP32.

Open UbidotsESPMQTT.h , from <sketchbook location>\libraries  with Notepad or Notepad++ (preferred):

Comment or remove #include <ESP8266WiFi.h> , and add these two lines:

#include <Arduino.h>
#include <WiFi.h>


Replace the following line:

#define SERVER "things.ubidots.com"


with:

#define SERVER "industrial.ubidots.com"


The final code may look like this:

4. Create Switch Button

1. Login to your Ubidots account:

2. Create a new device. To create a new device, click the "+" icon in the top right corner of the Device's section of your account.  Then, assign “OC06” as device name.

Once the device is created it will be appear listed in the device section:

3. Enter to the device created and add a new raw variable by pressing the "+" icon. The variable ought to be called “state”:

Once the variable is created you should have the following result:

IMPORTANT NOTE: In order to be able to establish the communication, the device & variable label assigned in the platform should be the same assigned in the code. To learn more about Devices & Variable Labels, refer to the following article

4. Go to the dashboard ("Data > Dashboards") section to create a new control widget. To create a new control widget into the Dashboard click on the “+” icon in the top-right corner of the dashboard user interface. Then, select "Switch" as widget type and assign the device and variable previously created.

Once the widget is created you should have the following result:

4. Coding

Including libraries:

#include "UbidotsESPMQTT.h"
#include <xOC06.h>
#include <xCore.h>

Enter your Wi-Fi Credential and Ubidots TOKEN, where indicated:

#define TOKEN "" // Put here your Ubidots TOKEN
#define WIFINAME "" //Put here your WiFi SSID
#define WIFIPASS "" // Put here your Wifi Pass

Defining constants:

#define DEVICE "oc06" // Put here your Ubidots device label
#define VARIABLE "degrees" // Put here your Ubidots variable label

IMPORTANT NOTE: Make sure that Device and Variable LABEL are respectively assigned.

Creating Variables and Objects:

Ubidots client(TOKEN,"esp32");
xOC06 OC06;

float value;

Ubidots MQTT needs to be assigned a callback function to be called every-time change in variable is detected.

void callback(char* topic, byte* payload, unsigned int length) {
  //Print payload of subscribed topic
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }

  //Convert byte payload into float
  value=atof((char*)payload);

  /*Rotate move forward if switch is ON, else move reverse
  if(value==1)
  {
    OC06.move(400, 200, FORWARD);
    delay(20);
  }else{
    OC06.move(400, 200, REVERSE);
    delay(20);
  }
}

 
One time setup:

void setup() {
  //Serial.begin(115200); //Remove comment to enable serial debugging

  client.setDebug(true); // Pass a true or false bool value to activate   //debug messages

  //Connect to the specified Wi-Fi Access Point
  client.wifiConnection(WIFINAME, WIFIPASS);

  //Connect to Ubidots MQTT with “callback” function
  client.begin(callback);
 
  //Begin I2C communication  
  Wire.begin();
 
  //Begin OC06
  OC06.begin();

  //Enable OC06
  OC06.enable();

  value=0;

}


Loop operation, keeps running and updating again and again

void loop() {
  //Reconnect to Ubidot MQTT if not connected
  if(!client.connected()){
      client.reconnect();
      //Subscribe to the specified DEVICE and VARIABLE
      client.ubidotsSubscribe(DEVICE,VARIABLE);
  }
 
  //Keep tracking Ubidots MQTT    
  client.loop();
}


The complete code:

#include "UbidotsESPMQTT.h"
#include <xOC06.h>
#include <xCore.h>

#define TOKEN "" // Put here your Ubidots TOKEN
#define WIFINAME "" //Put here your WiFi SSID
#define WIFIPASS "" // Put here your Wifi Pass
#define DEVICE "oc06" // Put here your Ubidots device label
#define VARIABLE "degrees" // Put here your Ubidots variable label
 
void callback(char* topic, byte* payload, unsigned int length) {

  //Print payload of subscribed topic
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }

  //Convert byte payload into float
  value=atof((char*)payload);

  /*Rotate move forward if switch is ON, else move reverse

  if(value==1)
  {
    OC06.move(400, 200, FORWARD);
    delay(20);
  } else {
    OC06.move(400, 200, REVERSE);
    delay(20);
  }
}

void setup() {

  //Serial.begin(115200); //Remove comment to enable serial debugging

  client.setDebug(true); // Pass a true or false bool value to activate   //debug messages

  //Connect to the specified Wi-Fi Access Point
  client.wifiConnection(WIFINAME, WIFIPASS);

  //Connect to Ubidots MQTT with “callback” function
  client.begin(callback);

  //Begin I2C communication  
  Wire.begin();

  //Begin OC06
  OC06.begin();

  //Enable OC06
  OC06.enable();

  value=0;
}

void loop() {

  //Reconnect to Ubidot MQTT if not connected
  if(!client.connected()){
      client.reconnect();
      //Subscribe to the specified DEVICE and VARIABLE
      client.ubidotsSubscribe(DEVICE,VARIABLE);
  }

  //Keep tracking Ubidots MQTT    
  client.loop();
}

5. Compile and Upload the code

You will now use Arduino IDE to Compile and then upload the code to the CW02, having made sure you have selected CW02 board, and connected on the correct USB port.

The motor rotates forward when the switch is turned ON, and it moves backward when the switch is turned off:

6. Summary

In this tutorial, we have learned how to control Stepper motor using CW02/OC06/IP01 with Ubidots remotely from anywhere. With XinaBox and Ubidots, you can now send 3D prints to 3D printer remotely from anywhere. The tutorial was fairly simple and can take upto 20-25 minutes.

Other readers have also found useful...

Did this answer your question?