File tree Expand file tree Collapse file tree 2 files changed +30
-20
lines changed Expand file tree Collapse file tree 2 files changed +30
-20
lines changed Original file line number Diff line number Diff line change 77import time
88import LiveObjects
99
10- import busio
11- import adafruit_vl6180x
12-
13- try :
14- import board
15- except NotImplementedError : # if no I2C device
16- print ("No GPIO present." )
17- sys .exit ()
18-
19- # Create I2C bus
20- i2c = busio .I2C (board .SCL , board .SDA )
21- # Create sensor instance.
22- sensor = adafruit_vl6180x .VL6180X (i2c )
23- # You can add an offset to distance measurements here (e.g. calibration)
24- # Swapping for the following would add a +10 millimeter offset to measurements:
25- # sensor = adafruit_vl6180x.VL6180X(i2c, offset=10)
26-
2710# Create LiveObjects
2811lo = LiveObjects .Connection ()
12+ sensor = LiveObjects .SensorVL6180X ()
2913
3014MESSAGE_RATE = 5
3115
3519
3620while True :
3721 if (time .time ()) >= last + MESSAGE_RATE :
38- # lo.add_to_payload("uptime", int(time.time() - uptime)) # Add value to payload: name - value
39- lo .add_to_payload ("distance" , sensor .range )
40- lo .add_to_payload ("ambient_light" , sensor .read_lux (adafruit_vl6180x .ALS_GAIN_1 ))
22+ lo .add_to_payload ("distance" , sensor .range ())
23+ lo .add_to_payload ("ambient_light" , sensor .read_lux (0x06 ))
4124 lo .send_data () # Sending data to cloud
4225 last = time .time ()
4326 lo .loop () # Check for incoming messages and if connection is still active
Original file line number Diff line number Diff line change @@ -231,3 +231,30 @@ def __new__(cls, net_type):
231231 sn = 'RaspberryPi' if is_raspberrypi () else 'Linux'
232232 board = eval (sn )(net_type ) # instance of board w/ net type: WiFi, LTE, etc.
233233 return board
234+
235+
236+ class SensorVL6180X :
237+ def __new__ (cls ):
238+ try : # Python@RPi
239+ import busio
240+ import adafruit_vl6180x
241+ import board
242+ # Create I2C bus
243+ i2c = busio .I2C (board .SCL , board .SDA )
244+ # Create sensor instance.
245+ return adafruit_vl6180x .VL6180X (i2c )
246+ # You can add an offset to distance measurements here (e.g. calibration)
247+ # Swapping for the following would add a +10 millimeter offset to measurements:
248+ # sensor = adafruit_vl6180x.VL6180X(i2c, offset=10)
249+ except ImportError : # microPython
250+ import machine
251+ import vl6180x
252+ # Create I2C bus @8266
253+ i2c = machine .I2C (scl = machine .Pin (5 ), sda = machine .Pin (4 ), freq = 100000 )
254+ # Create I2C bus @ESP32
255+ # i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=100000)
256+ # Create sensor instance.
257+ return vl6180x .Sensor (i2c , address = 0x29 )
258+ except NotImplementedError : # if no I2C device
259+ print ("No GPIO present." )
260+ sys .exit ()
You can’t perform that action at this time.
0 commit comments