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

CW01 communicates with OC03 via the I2C protocol. OC03 has an onboard IO expander (PCA9554A) connected to an optically isolated photorelay (TLP241A). CW01 retrieves data from the cloud and transmits it to OC03

By the end of this guide, you will able to control the photorelay remotely using Ubidots and Xinabox xChips IP01, CW01 and OC03.

Requirements

Steps-by-Step

  1. Hardware Setup
  2. Setting up the Arduino IDE
  3. Create Switch Button
  4. The Code
  5. Create Binary File
  6. Summary

1. Hardware Setup

Connect CW01, OC03 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 to Arduino:

NOTE: If you are not familiar with how to Install libraries, please refer to the following 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 ”. To select your board from the Arduino IDE, select Tools > Board “CW01”.

3. 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 “OC03” 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. The Code

1. Including libraries:

#include "Ubidots.h"
#include <xOC03.h>
#include <xCore.h>

2. Enter your Wi-Fi credentials and your Ubidots TOKEN where is indicated :

const char* UBIDOTS_TOKEN = "...";  // Put here your Ubidots TOKEN
const char* WIFI_SSID = "..."; // Put here your Wi-Fi SSID
const char* WIFI_PASS = "..."; // Put here your Wi-Fi password
const char* DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM = "oc03";  // Replace by your device label
const char* VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM = "state";       // Replace by your variable label

NOTE:  Before uploading the code into the board make sure to assign the device & variable label respectively assigned.

3. One time , see the comments for self-explanation:

void setup() {
  Serial.begin(74880);
#ifdef ESP8266
  Wire.pins(2, 14);
#endif
  pinMode(LED_BUILTIN, OUTPUT);
  ubidots.wifiConnect(WIFI_SSID, WIFI_PASS); //Establish Wi-Fi
  //connection with access point
  Wire.begin();
  OC03.begin();
  //ubidots.setDebug(true); // Uncomment this line to set DEBUG on

}

Loop the operation, to keep it running and updating continuously:

void loop() {

  float value = ubidots.get(DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM, VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM);

  if (value != ERROR_VALUE) {
    if (value == 1.0) {
      OC03.write(HIGH);
      digitalWrite(LED_BUILTIN, 1);
    } else {
      OC03.write(LOW);    
      digitalWrite(LED_BUILTIN, 0);
    }
  }
  delay(1000);
}

The complete Code, read the comments for a detailed explanation:

#include "Ubidots.h"
#include <xOC03.h>
#include <xCore.h>

#define LED_BUILTIN 13

const char* UBIDOTS_TOKEN = "...";  // Put here your Ubidots TOKEN
const char* WIFI_SSID = "..."; // Put here your Wi-Fi SSID
const char* WIFI_PASS = "..."; // Put here your Wi-Fi password
const char* DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM = "oc03";  // Replace by your device label
const char* VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM = "state";       // Replace by your variable label

xOC03 OC03;
Ubidots ubidots(UBIDOTS_TOKEN, UBI_HTTP);

void setup() {
  Serial.begin(74880);
#ifdef ESP8266
  Wire.pins(2, 14);
#endif
  pinMode(LED_BUILTIN, OUTPUT);
  ubidots.wifiConnect(WIFI_SSID, WIFI_PASS); //Establish Wi-Fi
  //connection with access point
  Wire.begin();
  OC03.begin();
  //ubidots.setDebug(true); // Uncomment this line to set DEBUG on

}

void loop() {

  float value = ubidots.get(DEVICE_LABEL_TO_RETRIEVE_VALUES_FROM, VARIABLE_LABEL_TO_RETRIEVE_VALUES_FROM);

  if (value != ERROR_VALUE) {
    if (value == 1.0) {
      OC03.write(HIGH);
      digitalWrite(LED_BUILTIN, 1);
    } else {
      OC03.write(LOW);    
      digitalWrite(LED_BUILTIN, 0);
    }
  }
  delay(1000);
}

5. Create Binary File

You will use our xFlasher software to flash the code once ready. The xFlasher uploads .bin files to the CW01 and CW02 cores. 

IMPORTANT NOTE: Please refer to the following guide on how to use xFlasher.

1. To create the .bin file using the Arduino IDE, "Sketch > Export compiled Binary":

At this point, the .bin file is exported to the same folder in which Arduino.ino file is saved.

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

Now you are able to control the OC03 remotely using Ubidots:

6. Summary

In this tutorial, we have shown you how to remote control a low voltage photorelay using XinaBox and Ubidots. The tutorial is fairly simple, and takes about 15-20 minutes.

Other readers have also found useful...

Did this answer your question?