KalbeAbbas
Published © MIT

Remote Proximity Sensing with XinaBox, Zerynth and Ubidots

Learn how to monitor the proximity of an object to the XinaBox xChip SL06 (APDS-9960) remotely anywhere using Ubidots and Zerynth Studio..

EasyFull instructions provided15 minutes216
Remote Proximity Sensing with XinaBox, Zerynth and Ubidots

Things used in this project

Hardware components

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

Software apps and online services

Zerynth Studio
Zerynth Studio
Ubidots
Ubidots

Story

Read more

Code

main.py

Python
Create new Zerynth project and add two files: main.py and helpers.py.
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 proximity sensing
SL06.enableProximitySensor()

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.mqtt.loop()

while True:
    prox = SL06.getProximity()  # read the proximity level
    print(prox)
    device.publish({"value":prox},variable="proximity")
    sleep(2000)

helpers.py

Python
Auxiliary code for ubidots library to work
# -*- 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

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

Comments