The ESP8266 Core and Wi-Fi module (CW01) allows users to send data from XinaBox’s modular xChips to the cloud, and control device according to the obtained data.

The xChip OC06 uses a 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, CW01 and OC06.

Requirements

Step-by-Step

  1. Hardware Setup
  2. Setting up the Arduino IDE
  3. Create Switch Button Widget
  4. Coding
  5. Create binary file
  6. Summary

1. Hardware Setup

Connect CW01, 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. You will need to install Arduino IDE 1.8.8.

2. Install these libraries to Arduino:

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

3. With the ESP8266 platform installed, select the ESP8266 device you are working with. In the case, we are working with a “CW01 (ESP8266)”. To select your board from the Arduino IDE, select Tools > Board “XinaBox CW01”.

3. Create Switch Button Widget

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:

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 "UbidotsMicroESP8266.h"
#include <xOC06.h>
#include <xCore.h>

Enter your Wi-Fi Credentials and Ubidots TOKEN where indicated:

#define DEVICE "OC06" // Put here your Ubidots device label
#define VARIABLE "degrees" // Put here your Ubidots variable label
#define TOKEN "your-ubidots-token" // Put here your Ubidots TOKEN
#define WIFISSID "your-accesspoint-ssid" // Put here your Wi-Fi SSID
#define PASSWORD "your-accesspoint-password" // Put here your Wi-Fi password


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

One time setup, see the comments for self-explanation:

void setup() {
    //Serial.begin(74880); //Uncomment this line to enable serial debugging
    client.wifiConnection(WIFISSID, PASSWORD); //Connect to the Access Point
    Wire.begin(); //Begin I2C communication
    OC06.begin(); //Begin OC06 Stepper Motor Controller
    OC06.enable(); //Enable OC06
    //client.setDebug(true); // Uncomment this line to set DEBUG on
}

Loop operation, keeps running and updating again and again

void loop() {
    //Get switch state from Ubidots
    value= client.getValueWithDevice(DEVICE, VARIABLE);     //Move motor forward or reverse
    if(value==1) {
      OC06.move(400, 200, FORWARD); //(Steps,Speed,Direction)
      delay(20);
    } else {
      OC06.move(400, 200, REVERSE);
      delay(20);
    }
}

   
The complete code:

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

#define DEVICE "OC06" // Put here your Ubidots device label
#define VARIABLE "degrees" // Put here your Ubidots variable label
#define TOKEN "your-ubidots-token" // Put here your Ubidots TOKEN

#define WIFISSID "your-accesspoint-ssid" // Put here your Wi-Fi SSID
#define PASSWORD "your-accesspoint-password" // Put here your Wi-Fi password

float value; //Store value of state

xOC06 OC06; //Create OC06 object
Ubidots client(TOKEN); //Create Ubidots client

void setup() {
    //Serial.begin(74880);
    client.wifiConnection(WIFISSID, PASSWORD);
    Wire.begin();
    OC06.begin();
    OC06.enable();
    //client.setDebug(true); // Uncomment this line to set DEBUG on
}

 void loop() {
    //Get switch state from Ubidots
    value= client.getValueWithDevice(DEVICE, VARIABLE);

    //Move motor forward or reverse
    if(value==1){
      OC06.move(400, 200, FORWARD); //(Steps,Speed,Direction)
      delay(20);
    } else {
      OC06.move(400, 200, REVERSE);
      delay(20);
    }
}

5. Compile and Upload Code

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

2. Go to the Ubidots Dashboard to change the state of the switch widget previously created and see the result in the OC06:

6. Summary

In this tutorial, we have learned how to control Stepper motor using CW01/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 15-20 minutes.

Other readers have also found useful...


Did this answer your question?