
How do you feel about this article? Help us to provide better content for you.

Thank you! Your feedback has been received.

There was a problem submitting your feedback, please try again later.

What do you think of this article?
Parts list
Qty | Product | Part number | |
---|---|---|---|
1 | XinaBox xCHIP Wi-Fi & Bluetooth Core Module CW02 | 174-3702 | |
1 | XinaBox, USB Programming Interface Module for FT232R - IP01 | 174-3703 | |
1 | XinaBox SW03, Weather Sensor Module for MPL3115A2 | 174-3746 | |
1 | Zerynth Studio | ||
1 | OKdo Cloud | ||
Part 2: Uploading Data to OKdo Cloud and Visualising Data teaches you how to program your XinaBox to upload data to OKdo Cloud and how to visualise the data. In this part, we guide you through how OKdo Cloud can send commands to XinaBox, controlling the LED colour, while XinaBox is collecting data from the SW03 sensor.
Connecting your XinaBox
Connect the modules of XinaBox as the image below:
- Connect CW02 to USB programming interface module IP01.
- Connect SW03 weather sensor to CW02.
- Connect the XinaBox to your computer over USB.
Programing the XinaBox
Same as inPart 2, Zerynth Studio is used to program the XinaBox. In order to control XinaBox from OKdo Cloud, several lines of code are needed to insert into the programme. A callback function from the example code of OKdo Cloud is used to handle the LED control task.
A full program code project is attached.
For more information of the callback function, please visithttps://docs.zerynth.com/latest/official/lib.okdo.iot/docs/official_lib.okdo.iot_iot.html#iot.watch_command.
Launch the Zerynth Studio. Create your own project and copy the code below.
# xinabox_okdo_sw03_led_control
# Created at 2019-09-04 03:31:45.282095
#################### SETTING 1 <BEGIN> ####################
## Uploading data to OKDO Cloud
## Example: MQTT at OKDO Cloud IoT
import streams
from wireless import wifi
# choose a wifi chip (esp32)
from espressif.esp32net import esp32wifi as wifi_driver
# let's import the OKDO cloud modules; first the IoT module...
from okdo.iot import iot
# ...then the mqtt client
from okdo.iot import mqtt_client
# Let's define a global variable to store the publishing rate (in ms)
rate = 3000
# Customize the following variables
ssid = "Rxxxxxxxs" # this is the SSID of the WiFi network
wifipwd = "axxxx" # this is the Password for WiFi
device_id = "MxxxxxxxxxxxxQ" # this is the device identifier. Can be obtained from the OKDO cloud dashboard
device_token = "maker:4QRxxxxxJo0" # this is the device token. Can be obtained from the OKDO cloud dashboard
# Remember to add a device to your okdo cloud and define some assets:
# - "led_red", "led_green", "led_blue" actuators, with type Boolean
pinMode(D25,OUTPUT) #red LED
pinMode(D26,OUTPUT) #green LED
pinMode(D27,OUTPUT) #blue LED
now_led_status = ""
## Visit
## ==> https://docs.allthingstalk.com/cloud/concepts/pinboards/controls/#actuator-controls
def led_control(asset,value, previous_value):
if(value == True and now_led_status == ''):
device.publish_asset(asset+"_status", value)
if(asset == "led_red"):
digitalWrite(D25, HIGH)
if(asset == "led_green"):
digitalWrite(D26, HIGH)
if(asset == "led_blue"):
digitalWrite(D27, HIGH)
now_led_status = asset
print("@@@@@@", asset+"_status", "is:", value)
if(value == True and asset != now_led_status):
print("@@@@@@", now_led_status,"is ON")
print("@@@@@@ Trun it off first!")
if(value == False and asset == now_led_status):
now_led_status = ''
device.publish_asset(asset+"_status", value)
if(asset == "led_red"):
digitalWrite(D25, LOW)
if(asset == "led_green"):
digitalWrite(D26, LOW)
if(asset == "led_blue"):
digitalWrite(D27, LOW)
print("@@@@@@", asset+"_status","is:", value)
streams.serial()
#################### SETTING 1 <END> ####################
#################### SETTING 2 <BEGIN> ####################
## Collecting ambient temperature, altitude and pressure sensor.
## Example: environmental_data, for SW03 weather sensor
#import streams
from xinabox.sw03 import sw03
#streams.serial()
# SW03 instance
SW03 = sw03.SW03(I2C0)
# configure SW03
SW03.init()
#################### SETTING 2 <END> ####################
# As a counter
a=0
try:
# Let's initialize the WiFi
wifi_driver.auto_init()
for _ in range(0,5):
try:
wifi.link(ssid,wifi.WIFI_WPA2,wifipwd)
break
except:
print("Trying to connect...")
sleep(2000)
else:
print("oops, can't attach to wifi!")
raise IOError
print("Connecting to OKDO IoT Cloud...")
# let's create a device passing the id, the token and the type of client
device = iot.Device(device_id,device_token,mqtt_client.MqttClient)
device.connect()
print("Device is connected")
# define the callbacks to call when an asset command is received
device.watch_command("led_red",led_control)
device.watch_command("led_green",led_control)
device.watch_command("led_blue",led_control)
# start the device
device.run()
print("Device is up and running")
while True:
# sleep as indicated by rate
sleep(rate)
############ Modified code <BEGIN> ############
###### SW03 weather sensor <BEGIN> ######
temp = SW03.getTempC() # return temp in degree celcius
alt = SW03.getAltitude() # return alitude in meters
pres = SW03.getPressure() # return pressure in pascals
###### SW03 weather sensor <END> ######
a=a+1
print("#", a)
msg = device.publish_asset("temperature",temp)
print("Published asset: Temperature",msg)
msg = device.publish_asset("altitude",alt)
print("Published asset: Altitude",msg)
msg = device.publish_asset("pressure",pres)
print("Published asset: Pressure",msg)
print(".")
print(".")
# alternatively, you can publish more than one asset state at a time
# by providing them as a dictionary to the following function (uncomment to test)
# msg = device.publish_state({"random":x})
# print("Published state",msg)
############ Modified code <END> ############
except Exception as e:
print(e)
Here is the extra inserted code. The code in lines 37 to 62 define which pin should be pulled high to give specific colour if a callback is received by XinaBox and a specific asset_name is received.
The purpose of the code in line 55 is used to publish the status of the LED back to OKdo Cloud.
device.publish_asset(asset+"_status", value)
Codes in line 37 to 62:
pinMode(D25,OUTPUT) #red LED
pinMode(D26,OUTPUT) #green LED
pinMode(D27,OUTPUT) #blue LED
now_led_status = ""
## Visit
## ==> https://docs.allthingstalk.com/cloud/concepts/pinboards/controls/#actuator-controls
def led_control(asset,value, previous_value):
if(value == True and now_led_status == ''):
device.publish_asset(asset+"_status", value)
if(asset == "led_red"):
digitalWrite(D25, HIGH)
if(asset == "led_green"):
digitalWrite(D26, HIGH)
if(asset == "led_blue"):
digitalWrite(D27, HIGH)
now_led_status = asset
print("@@@@@@", asset+"_status", "is:", value)
if(value == True and asset != now_led_status):
print("@@@@@@", now_led_status,"is ON")
print("@@@@@@ Trun it off first!")
if(value == False and asset == now_led_status):
now_led_status = ''
device.publish_asset(asset+"_status", value)
if(asset == "led_red"):
digitalWrite(D25, LOW)
if(asset == "led_green"):
digitalWrite(D26, LOW)
if(asset == "led_blue"):
digitalWrite(D27, LOW)
print("@@@@@@", asset+"_status","is:", value)
The codes below define the callback. The function "device.watch_command()" is executed if a callback is sent from OKdo Cloud and is received by the XinaBox. Inside this function, another function we defined above, led_control(), will be executed and will turn on that colour of the LED.
For more information about the callback function, please visithttps://docs.zerynth.com/latest/official/lib.okdo.iot/docs/official_lib.okdo.iot_iot.html#iot.watch_command.
# define the callbacks to call when an asset command is received
device.watch_command("led_red",led_control)
device.watch_command("led_green",led_control)
device.watch_command("led_blue",led_control)
Setting in OKdo Cloud
Despite creating a new "Sensor" asset, an "Actuator" asset with the "Boolean" type should be created. Please ensure the name of the asset is the same as the name in the code. Here are the full steps of the demonstration.
After creating the Actuator, create a Sensor asset to receive the LED status from the XinaBox and to show the state of the LED.
Result
The video below shows the final result of this demonstration. The dashboard is improved so that users can control the LED while seeing data uploaded from the SW03 weather sensor.
My articles
1. Building a Hill Fire Detection IoT with LoRa
2. Connecting XinaBox IoT with Zerynth to Cloud
Part 1: Introduction and Simple Example
Part 2: Uploading Data to OKdo Cloud and Visualising Data