Skip to content

Commit 52183a1

Browse files
authored
Merge pull request #6 from tomekmalek/master
Implementation of example with VL6180X sensor
2 parents ddd3a43 + 07fe0e0 commit 52183a1

File tree

12 files changed

+609
-7
lines changed

12 files changed

+609
-7
lines changed

7_distance_and_light_sensor.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Copyright (C) Orange
3+
#
4+
# This software is distributed under the terms and conditions of the 'MIT'
5+
# license which can be found in the file 'LICENSE.md' in this package distribution
6+
import sys
7+
import time
8+
import LiveObjects
9+
10+
# Create LiveObjects
11+
lo = LiveObjects.Connection()
12+
sensor = LiveObjects.SensorVL6180X()
13+
14+
MESSAGE_RATE = 5
15+
16+
# Main program
17+
lo.connect() # Connect to LiveObjects
18+
last = uptime = time.time()
19+
20+
while True:
21+
if (time.time()) >= last + MESSAGE_RATE:
22+
lo.add_to_payload("distance", sensor.range)
23+
lo.add_to_payload("ambient_light", sensor.amb_light())
24+
lo.send_data() # Sending data to cloud
25+
last = time.time()
26+
lo.loop() # Check for incoming messages and if connection is still active

LiveObjects/Connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def get_parameter(self, name):
212212
return 0
213213

214214
def add_to_payload(self, name, val):
215+
self.add_model("Orange")
215216
self.__payload[self.__value][name] = val
216217

217218
def set_object_as_payload(self, val):

LiveObjects/hal.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,70 @@ def get_client_id(self):
210210
return self.get_lang_str() + ':' + get_mac()
211211

212212

213-
class BoardsFactory:
213+
class RaspberryPi(Linux):
214+
pass
215+
216+
217+
def is_raspberrypi():
218+
try:
219+
with open('/proc/device-tree/model') as f:
220+
return f.read().startswith('Raspberry')
221+
except FileNotFoundError:
222+
return False
223+
214224

225+
class BoardsFactory:
215226
def __new__(cls, net_type):
216227
s = sys.platform
217228
sn = s[0].upper() + s[1:] # capitalize first letter
229+
if sn == 'Linux':
230+
sn = 'RaspberryPi' if is_raspberrypi() else 'Linux'
218231
board = eval(sn)(net_type) # instance of board w/ net type: WiFi, LTE, etc.
219232
return board
233+
234+
235+
MAX_DEV_NB = 20
236+
237+
238+
def get_i2c():
239+
import machine
240+
typical_gpio = ([22, 23], [5, 4], [22, 21], [23, 18])
241+
for gpio in typical_gpio:
242+
scl, sda = gpio
243+
i2c = None
244+
try: # MicroPython 1.19.1 20220618-v1.19.1
245+
i2c = machine.SoftI2C(scl=machine.Pin(scl), sda=machine.Pin(sda), freq=100000)
246+
if i2c.scan() and len(i2c.scan()) < MAX_DEV_NB:
247+
return i2c
248+
except ValueError: # next sda, scl
249+
pass
250+
except AttributeError: # Pycom MicroPython 1.20.2.r6 [v1.11-c5a0a97] on 2021-10-28
251+
i2c = machine.I2C(0)
252+
return i2c
253+
del i2c
254+
raise RuntimeError("No I2C devices found. Check SDA and SCL lines and add respective GPIO to 'typical_gpio'.")
255+
256+
257+
class SensorVL6180X:
258+
def __new__(cls):
259+
try: # Python@RPi
260+
import busio
261+
import adafruit_vl6180x
262+
import board
263+
264+
class VL6180X(adafruit_vl6180x.VL6180X):
265+
def amb_light(self):
266+
"""Implementing default gain"""
267+
return self.read_lux(gain=0x06) # ALS_GAIN_1
268+
269+
i2c = busio.I2C(board.SCL, board.SDA)
270+
return VL6180X(i2c)
271+
272+
except ImportError: # microPython
273+
import vl6180x_micro
274+
i2c = get_i2c()
275+
return vl6180x_micro.Sensor(i2c)
276+
277+
except NotImplementedError: # if no I2C device
278+
print("No GPIO present.")
279+
sys.exit()

README.md

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Prototype with Orange using Live Objects
2-
### Discover Orange [**Live Objects**](https://liveobjects.orange-business.com) using dedicated SDK for **Python and uPython compatible** boards and systems.
2+
### Discover Orange [**Live Objects**](https://liveobjects.orange-business.com) using dedicated SDK for **Python 3 and uPython compatible** boards and systems.
33

44
This code wraps all the functions necessary to make your object work with Live Objects.
55

@@ -23,7 +23,7 @@ Code uses MQTT connection to exchange data with Live objects under the hood to k
2323
This code needs a few libraries to run:
2424
- Python needs [paho-mqtt](https://pypi.org/project/paho-mqtt/)
2525
- Python for Windows needs [python-certifi-win32](https://pypi.org/project/python-certifi-win32/)
26-
- uPython needs [umqttsimple, umqttrobust and ssl](https://github.com/micropython/micropython-lib)
26+
- uPython needs [umqttsimple](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.simple/umqtt/simple.py) and [umqttrobust](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.robust/umqtt/robust.py)
2727

2828
## How to use ##
2929

@@ -236,12 +236,55 @@ You need to override specific methods - e.g. `connect` which is depended on type
236236
All specific functions are placed in `services.py`.
237237
If your board needs function supporting its equipment you need to put it in this file.
238238

239+
## VL6180X Sensor use-case ##
240+
241+
We can connect sensor using I<sup>2</sup>C to board supporting Python like **Raspberry Pi**.
242+
243+
The [VL6180X](https://www.st.com/en/imaging-and-photonics-solutions/vl6180x.html) is the latest product based on ST’s patented FlightSense™technology.
244+
This is a ground-breaking technology allowing absolute distance to be measured independent of target reflectance.
245+
Instead of estimating the distance by measuring the amount of light reflected back from the object (which is significantly influenced by color and surface),
246+
the VL6180X precisely measures the time the light takes to travel to the nearest object and reflect back to the sensor (Time-of-Flight).
247+
Description from st.com.
248+
249+
### Prerequisites ###
250+
251+
#### Enabling I<sup>2</sup>C ####
252+
Enable (if needed) **I<sup>2</sup>C** interface on your Raspberry Pi using terminal and command:
253+
```bash
254+
sudo raspi-config
255+
```
256+
and selecting: **3 Interface Options** -> **P5 I2C** -> **\<Yes\>**
257+
258+
![I2C_Enabling](image/enable_I2C.png)
259+
260+
#### Wiring ####
261+
![Wiring](https://www.raspberrypi-spy.co.uk/wp-content/uploads/2012/06/Raspberry-Pi-GPIO-Header-with-Photo-768x512.png "Mapping")
262+
263+
<br>
264+
265+
Example of development module using VL6180X you can find [here](https://kamami.pl/en/kamod-kamami-peripheral-modules/559362-kamodvl6180x-a-module-with-distance-gesture-and-als-sensor.html). Below diagram shows how to connect it to Raspberry Pi.
266+
267+
![Schematics](image/RPi_VL6180X.png "Schematics")
268+
269+
#### Adding VL6180X Python module ####
270+
Necessary module by [Adafruit](https://learn.adafruit.com/adafruit-vl6180x-time-of-flight-micro-lidar-distance-sensor-breakout/python-circuitpython) can be installed using `pip`
271+
```bash
272+
pip3 install adafruit-circuitpython-vl6180x
273+
```
274+
275+
#### How to use ####
276+
To run you need to use below command:
277+
```bash
278+
python3 7_distance_and_light_sensor.py
279+
```
280+
281+
---
239282

240283
# Installation guide for uPython #
241284
## Example for ESP32 / ESP8266 ##
242285
### Requirements ###
243286
1. [ampy](https://learn.adafruit.com/micropython-basics-load-files-and-run-code/install-ampy)
244-
2. [umqttsimple, umqttrobust and ssl](https://github.com/micropython/micropython-lib)
287+
2. [umqttsimple, umqttrobust and ssl](https://github.com/micropython/micropython-lib) (for your convenience they are included in `micropython` folder)
245288
3. [PuTTY](https://www.putty.org/) (for Windows)
246289

247290
### Installation steps ###
@@ -264,6 +307,7 @@ You can use one of example ones (`1_send_data.py`, ...) renaming it to `main.py`
264307
> ampy -pCOMXX put main.py
265308
```
266309

310+
267311
4. Connect to device and check if it's working using PuTTY
268312

269313
Ctrl + D soft resets device
@@ -274,25 +318,75 @@ You can use one of example ones (`1_send_data.py`, ...) renaming it to `main.py`
274318

275319
After all steps content of the device should look like below:
276320
```commandline
277-
> ampy -pCOMXX ls
321+
> ampy --port COMx ls
278322
/LiveObjects
279323
/boot.py
280324
/main.py
281325
/umqttrobust.py
282326
/simple.py
283327
284-
> ampy -pCOMXX ls LiveObjects
328+
> ampy --port COMx ls LiveObjects
285329
/LiveObjects/Connection.py
286330
/LiveObjects/__init__.py
287331
/LiveObjects/hal.py
288332
/LiveObjects/credentials.py
289333
/LiveObjects/services.py
290334
```
335+
where COMx means port on your computer (e.g. COM8) with connected microPython board.
291336

292337
## Example for LoPy / GPy ##
293338

294339
You can do the steps as above but better is to use [Pymakr plug-in](https://pycom.io/products/supported-networks/pymakr/) for **Visual Studio Code** or **Atom** delivered by [Pycom](https://pycom.io/).
295340
Plug-in supports code development, its upload to the board and communication with board.
296341

342+
## VL6180X Sensor use-case ##
343+
344+
Sensor described in this [section](#vl6180x-sensor-use-case) can be used on boards supporting microPython.
345+
346+
### Prerequisites ###
347+
348+
#### Wiring ####
349+
You need to connect I<sup>2</sup>C interface (SCL & SDA) and power lines on the board with corresponding pins on the sensor.
350+
You need to be aware that **boards can use different GPIOs for I<sup>2</sup>C** purposes. Set of typical pairs is placed
351+
in function `get_i2c()` in file `hal.py`. If your board uses other GPIO pins, you need to add them to the tuple `typical_gpio`.
352+
```Python
353+
def get_i2c():
354+
import machine
355+
typical_gpio = ([22, 23], [5, 4], [22, 21])
356+
...
357+
```
358+
![ESP32_sch](image/ESP32_VL6180X_sch.png)
359+
360+
Example of wiring ESP32 board with GPIO22 and GPIO21 (_source: https://randomnerdtutorials.com/esp32-pinout-reference-gpios/_)
361+
362+
![ESP32](image/ESP32_VL6180X.jpg)
363+
364+
#### How to use ####
365+
1. You need to upload additional library for VL6180X support (it is placed in `micropython` folder):
366+
```commandline
367+
> ampy -pCOMXX put vl6180x_micro.py
368+
```
369+
2. Copy `7_distance_and_light_sensor.py` as `main.py` and upload it into board.
370+
371+
After above operations you can see:
372+
```commandline
373+
> ampy --port COMx ls
374+
/LiveObjects
375+
/boot.py
376+
/main.py
377+
/umqttrobust.py
378+
/simple.py
379+
/vl6180x_micro.py
380+
381+
> ampy --port COMx ls LiveObjects
382+
/LiveObjects/Connection.py
383+
/LiveObjects/__init__.py
384+
/LiveObjects/hal.py
385+
/LiveObjects/credentials.py
386+
/LiveObjects/services.py
387+
```
388+
3. Connect to device and check if it's working using PuTTY.
389+
390+
297391
## Troubleshooting ##
298392
If you are getting 'MQTT exception: 5' check your api key

image/ESP32_VL6180X.jpg

145 KB
Loading

image/ESP32_VL6180X_sch.png

200 KB
Loading

image/RPi_VL6180X.png

323 KB
Loading

image/enable_I2C.png

45.8 KB
Loading

0 commit comments

Comments
 (0)