KalbeAbbas
Published © MIT

Ambient Light Sensing with XinaBox, Zerynth and Ubidots

Learn how to measure the ambient light level with XinaBox xChip SL06 (APDS-9960) remotely using Ubidots and Zerynth.

BeginnerFull instructions provided15 minutes552
Ambient Light Sensing with XinaBox, Zerynth 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
Create new Zerynth project and add two files: main.py and helpers.py.
Enter your Wi-Fi credentials and Unique 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 light sensing
SL06.enableLightSensor()

print('connecting to wifi...')
 
#Connect to the specified Wi-Fi device
while not wifi.is_linked():
    try:
    # FOR THIS EXAMPLE TO WORK, "SSID" AND "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:
    #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)
 
device.mqtt.loop()

while True:
    light = SL06.getAmbientLight()  # read the the ambient light level
    print('Ambient Light Level: ', light)
    device.publish({"value":light},variable="als")
    sleep(2000)

helpers.py

Python
Auxiliary code for Ubidots library to work
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
25 projects • 20 followers
An enthusiastic and dedicated Electronic engineer graduated from SSUET Karachi, Pakistan. Loves to discover Embedded electronics projects.
Contact
Thanks to XinaBox.

Comments

Please log in or sign up to comment.