KalbeAbbas
Published © MIT

Remote Gesture Sensing with XinaBox CW02-SL06 and Ubidots

Learn how to monitor the gestures passed through the XinaBox xChip SL06 (APDS-9960) from anywhere using Ubidots.

EasyFull instructions provided15 minutes60
Remote Gesture Sensing with XinaBox CW02-SL06 and Ubidots

Things used in this project

Hardware components

CW02
XinaBox CW02
×1
IP01
XinaBox IP01
×1
SL06
XinaBox SL06
×1
(Optional) USB Extension cable
×1
XC10
XinaBox XC10
×1

Software apps and online services

Zerynth Studio
Zerynth Studio
Ubidots
Ubidots

Story

Read more

Code

main.py

Python
Enter your Wi-Fi credentials and Ubidots TOKEN where indicated.
import streams
from wireless import wifi
from xinabox.sl06 import sl06
 
streams.serial()
# SL06 instance
SL06 = sl06.SL06(I2C0)
 
# choose a wifi chip supporting secure sockets
from espressif.esp32net import esp32wifi as wifi_driver
wifi_driver.auto_init()
 
# import ubidots iot module
from ubidots.iot import iot
 
# configure SL06
SL06.init()
 
# enable SL06 for gesture sensing
SL06.enableGestureSensor()
 
#Callback function for getting updates
def callback(value):
    global update
    print('Updated')
    update=True
 
sleep(1000)
 
print('connecting to wifi...')
 
#Connect to the specified Wi-Fi device
while not wifi.is_linked():
    try:
    # FOR THIS EXAMPLE TO WORK, "Network-Name" AND "Wifi-Password" MUST BE SET
    # TO MATCH YOUR ACTUAL NETWORK CONFIGURATION
      wifi.link("SSID",wifi.WIFI_WPA2,"PASSWORD")
      print("Link Established")
    except Exception as e:
      print("ooops, something wrong while linking :(", e)
      print(".")
    sleep(1000)
 
print('connecting to mqtt broker...')
try:
    # create ubidots iot device instance, connect to mqtt broker, set       #variable update callback and start mqtt reception loop
 
    #Enter your DEVICE-NAME and Unique Ubidots TOKEN
    device = iot.Device('DEVICE-NAME',' business','TOKEN’)
    device.mqtt.connect()
    print("Connected to mqtt broker")
except Exception as e:
    print("ooops, something went wrong :(", e)
    while True:
       sleep(1000)
 
#Subscribe to the ‘direction’ variable     
device.on_variable_update('DEVICE-NAME', 'direction', callback, json=False)
device.mqtt.loop()
 
#Numerical representation of Gesture
d=0
 
update=False
 
while True:
    print('wait to update')
    while not update:  #Force publishing until updated
        print('.')
        sleep(1000)
        if SL06.isGestureAvailable():   # check for gesture
         dir = SL06.getGesture()     # read direction
         print(dir)                  # print direction on console
         if dir=='up':
             d=1
         elif dir=='down':
             d=0
        device.publish({ 'value': d }, variable='direction')
    update=False

helpers.py

Python
Auxiliary code for main.py
# -*- coding: utf-8 -*-
# @Author: Lorenzo
# @Date:   2017-10-03 10:56:02
# @Last Modified by:   lorenzo
# @Last Modified time: 2017-10-26 10:44:43

import json

def load_device_conf():
    confstream = open('resource://device.conf.json')
    conf = ''
    while True:
        line = confstream.readline()
        if not line:
            break
        conf += line
    return json.loads(conf)

Credits

KalbeAbbas

KalbeAbbas

10 projects • 3 followers
An enthusiastic and dedicated Electronic engineer graduated from SSUET Karachi, Pakistan. Loves to discover Embedded electronics projects.
Thanks to XinaBox.

Comments