Skip to content

Commit d1210f8

Browse files
authored
Merge pull request #16 from tomekmalek/master
Adding example 7 regarding sensor VL6180X
2 parents 9dbc535 + 37b5936 commit d1210f8

19 files changed

+308
-5
lines changed

.github/workflows/compile-examples.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
build:
55
runs-on: ubuntu-latest
66
env:
7-
LIBRARIES: WiFi101 WiFiNINA MKRNB MKRGSM ArduinoJson VidorPeripherals PubSubClient
7+
LIBRARIES: WiFi101 WiFiNINA MKRNB MKRGSM ArduinoJson VidorPeripherals PubSubClient "SparkFun VL6180 Sensor"
88

99
strategy:
1010
matrix:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/******************************************************************************
2+
INCLUDES
3+
******************************************************************************/
4+
#include "arduino_secrets.h"
5+
#include <LiveObjects.h>
6+
#include "ExtDevices.h"
7+
8+
/******************************************************************************
9+
USER VARIABLES
10+
******************************************************************************/
11+
uint32_t messageRate = 5000; // stores the current data message rate in Milliseconds
12+
unsigned long lastMessageTime = 0; // stores the time when last data message was sent
13+
14+
VL6180x sensor(VL6180X_ADDRESS);
15+
u_int8_t distance; // distance from obstruction [mm]
16+
float ambient_light; // ambient light [lx]
17+
18+
/******************************************************************************
19+
USER PROGRAM
20+
******************************************************************************/
21+
void setup() {
22+
Serial.begin(115200);
23+
24+
VL6180XInit(); // initialization of VL6180X sensor
25+
26+
Serial.print("\n*** Live Objects for Arduino MKR boards, revision ");
27+
Serial.print(SW_REVISION);
28+
Serial.println(" ***");
29+
lo.setSecurity(NONE);
30+
lo.begin(MQTT, BINARY, true);
31+
lo.connect(); // connects to the network + Live Objects
32+
}
33+
34+
void loop() {
35+
if (millis() - lastMessageTime > messageRate) {
36+
// collect data periodically
37+
Serial.println("Sampling data");
38+
39+
distance = sensor.getDistance();
40+
lo.addToPayload(distance); // adding 'distance' value to the current payload
41+
// limiting value to 2 decimal places
42+
ambient_light = (static_cast<int>(sensor.getAmbientLight(GAIN_1) * 100.0)) / 100.0;
43+
lo.addToPayload(ambient_light);
44+
45+
Serial.println("Sending data to Live Objects");
46+
lo.sendData(); // send the data to Live Objects
47+
lastMessageTime = millis();
48+
}
49+
lo.loop(); // don't forget to keep this in your main loop
50+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Sending data from VL6180 sensor to Live Objects
2+
3+
This example shows how to send real data from sensor [VL6180X](https://www.st.com/resource/en/datasheet/vl6180x.pdf). Sensor delivers two useful values:
4+
- ambient light [[lx](https://en.wikipedia.org/wiki/Lux)],
5+
- distance from disturbance [mm].
6+
7+
In this use-case [an example of board using sensor](https://kamami.pl/en/kamod-kamami-peripheral-modules/559362-kamodvl6180x-a-module-with-distance-gesture-and-als-sensor.html) was being used.
8+
9+
![sensor](img/KAmodVL6180X_module.png)
10+
11+
## Our Setup view:
12+
13+
![setup_view](img/setup_view.jpg)
14+
15+
## Preparations
16+
### Installation VL6180X library
17+
Using **Library Manager** from Arduino IDE (*Tools -> Manage Libraries...*) you need to install library:
18+
19+
![library_installation](img/VL6180X_libr.png)
20+
21+
### Diagram of connection
22+
Module is controlled via I<sup>2</sup>C and accepts power and signal levels 3.3V.
23+
MKR1010 WiFi setup diagram:
24+
25+
![setup_diagram](img/setup_diagram.png)
26+
27+
For your board you need to connect power lines (**+3.3V, GND**) and I<sup>2</sup>C lines (**SDA, SCL**) to corresponding VL6180X lines.
28+
29+
### Decoder's preparation for MQTT devices
30+
If you want to limit amount of data which are being sent from board to Live Objects portal you can use **BINARY** mode which is defined in ```lo.begin()``` in *7_distance_and_light_sensor.ino*: ```lo.begin(MQTT, BINARY, true);```. In this case both values: ambient light (1 byte) and distance (4 bytes) will be sent as only five bytes (see terminal output in point 6).
31+
32+
To achieve this goal, you need to create **private binary decoder** in Live Objects portal. Use: *Data -> Decoders -> +Add*. You an enter data as below and click **Save**.
33+
34+
![binary_decoder](img/binary_decoder.png)
35+
36+
<br>
37+
38+
### MQTT interface modification
39+
40+
You need to select your device for which you want to activate previously created **private binary decoder**. Click selected device from the list uder tab **Devices**.
41+
42+
![select_dev](img/select_device_1.png)
43+
44+
<br>
45+
46+
Then click **Identity** and next click icon in red circle. After that in field **Decoder** you can select your decoder. Clicking **Save** finishes this step and activates our private decoder **VL6180Xbin** for MQTT interface for selected device.
47+
48+
![mod_interface](img/mod_interface_1.png)
49+
50+
## Running
51+
First of all, be sure that you installed the required libraries and generated an API key mentioned in the main **README.md** file, then:
52+
1. Open *7_distance_and_light_sensor.ino* sketch using Arduino IDE,
53+
2. Replace ```const char SECRET_LIVEOBJECTS_API_KEY[]="...";``` in *arduino_secrets.h* with API key you generated,
54+
3. Define necessary credentials for your board in *arduino_secrets.h*:
55+
- for WiFi boards:
56+
- ```extern const String SECRET_SSID = "...;```
57+
- ```extern const String SECRET_WIFI_PASS = "...";```
58+
- for cellular boards:
59+
- ```extern const String SECRET_PINNUMBER = "...";``` (if needed),
60+
- ```extern const String SECRET_APN = "";``` (if needed),
61+
- ```extern const String SECRET_APN_USER = "...";``` (if needed),
62+
- ```extern const String SECRET_APN_PASS = "...";``` (if needed),
63+
4. In ```lo.setSecurity()``` select security mode using ```TLS``` or ```NONE``` according to board abilities shown in **Compatibility** point in main **README.md**,
64+
5. Upload *7_distance_and_light_sensor.ino* sketch to your board.
65+
6. In your terminal window you should see similiar output:
66+
67+
```
68+
15:31:22.732 ->
69+
15:31:22.732 -> VL6180X sensor
70+
15:31:22.732 -> Model ID = 180
71+
15:31:22.732 -> Model Rev = 13
72+
15:31:22.732 -> Module Rev = 12
73+
15:31:22.732 -> Manufacture Date = 16/11/14 Phase: 1
74+
15:31:22.732 ->
75+
15:31:23.762 ->
76+
15:31:23.762 -> *** Live Objects for Arduino MKR boards, revision 2.1.0 ***
77+
15:31:24.526 -> [INFO] WiFi firmware version 1.2.4
78+
15:31:24.526 -> [INFO] Attempting to connect to SSID: EdekAD57BA
79+
15:31:28.279 ->
80+
15:31:28.279 -> [INFO] Connecting to MQTT broker mqtt.liveobjects.orange-business.com:1883
81+
15:31:30.439 -> [INFO] You're connected to the MQTT broker
82+
15:31:30.439 -> Sampling data
83+
15:31:30.572 -> Sending data to Live Objects
84+
15:31:30.572 -> [INFO] Publishing message on topic: dev/v1/data/binary
85+
15:31:30.572 -> [INFO] 4041A9C28F
86+
15:31:35.554 -> Sampling data
87+
15:31:35.687 -> Sending data to Live Objects
88+
15:31:35.687 -> [INFO] Publishing message on topic: dev/v1/data/binary
89+
15:31:35.687 -> [INFO] 4841AEE148
90+
...
91+
```
92+
93+
## Verify
94+
**Is device online:**
95+
If all went fine under **Devices** tab on Live Live Objects portal you should see online your device identified by its modem IMEI or WiFi MAC address:
96+
97+
![device_online](img/device_online.png)
98+
99+
**Is device sending data:**
100+
Under **Data** tab on Live Objects portal you should see messages sent by your device, along with values eg. *{ "distance": 110, "amblight": 192.63 }*
101+
102+
![data_portal](img/data_portal.png)
103+
104+
<br><br>
105+
106+
***
107+
## SMS (cellular boards)
108+
109+
## Preparations
110+
111+
### SMS device creation
112+
You need to manually add your cellular device as it is described in [this point](../6_sms_send_data/README.md).
113+
114+
### Decoder's preparation for SMS devices
115+
If you want to limit amount of data which are being sent from board to Live Objects portal you can use pure **TEXT** mode which is defined in ```lo.begin()``` in *7_distance_and_light_sensor.ino*: ```lo.begin(SMS, TEXT, true);```. In this case both values: ambient light and distance will be transmitted as raw text.
116+
117+
To achieve this goal, you need to create **private sms decoder** in Live Objects portal. Use: *Data -> Decoders -> +Add*. You an enter data as below and click **Save**.
118+
119+
![sms_decoder](img/sms_decoder.png)
120+
121+
### SMS interface modification
122+
123+
You need to modify SMS interface for you device in similar way as for MQTT device selecting **VL6180Xsms** decoder.
124+
125+
## Running
126+
First of all, be sure that you installed the required libraries and generated an API key mentioned in the main **README.md** file, then:
127+
1. Open *7_distance_and_light_sensor.ino* sketch using Arduino IDE,
128+
2. Replace ```const char SECRET_LIVEOBJECTS_API_KEY[]="...";``` in *arduino_secrets.h* with API key you generated,
129+
3. Specify the number of sever (gate) in *arduino_secrets.h*:
130+
- ```extern const String SECRET_SERVER_MSISDN = "...";```,
131+
4. In ```lo.setSecurity()``` select security mode ```NONE```
132+
5. Upload *7_distance_and_light_sensor.ino* sketch to your board.
133+
6. In your terminal window you should see similiar output:
134+
135+
```
136+
3:00:46.470 -> VL6180X sensor
137+
13:00:46.470 -> Model ID = 180
138+
13:00:46.470 -> Model Rev = 13
139+
13:00:46.470 -> Module Rev = 12
140+
13:00:46.470 -> Manufacture Date = 16/11/14 Phase: 1
141+
13:00:46.470 ->
142+
13:00:47.467 ->
143+
13:00:47.467 -> *** Live Objects for Arduino MKR boards, revision 2.1.0 ***
144+
13:00:52.151 -> [INFO] Connecting to cellular network
145+
13:01:00.558 ->
146+
13:01:00.758 -> [INFO] You're connected to the network
147+
13:01:00.758 -> Sampling data
148+
13:01:00.891 -> Sending data to Live Objects
149+
13:01:00.891 -> [INFO] Publishing message: 122;310.17;
150+
13:01:06.740 -> Sampling data
151+
13:01:06.840 -> Sending data to Live Objects
152+
13:01:06.840 -> [INFO] Publishing message: 255;300.67;
153+
...
154+
```
155+
156+
## Verify
157+
158+
You can verify if device is online and data are delivered in the same way as it is described for MQTT device.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Cellular connection cerdentials, used only for GSM boards
2+
extern const String SECRET_PINNUMBER = ""; // unless PIN is deactivated, specify your SIM card PIN number
3+
extern const String SECRET_APN = ""; // specify the APN name (if needed)
4+
extern const String SECRET_APN_USER = ""; // specify the username for your APN (if needed)
5+
extern const String SECRET_APN_PASS = ""; // specify the password for your APN (if needed)
6+
extern const String SECRET_SERVER_MSISDN = ""; // specify the number of server(gate)
7+
8+
// WIFI connection credentials, used only for WiFi boards
9+
extern const String SECRET_SSID = ""; // unless PIN is deactivated, specify your SIM card PIN number
10+
extern const String SECRET_WIFI_PASS = ""; // specify the APN name (if needed)
11+
12+
// Live Objects credential: paste below your API key (see Configuration > API keys on the portal).
13+
// You API key must have at least the predefined 'MQTT Device' rights profile
14+
// (alternatively: 'Device Access' read + write rights if need to customise the rights).
15+
// Please note that you *must* use a TLS connection (MQTTS) if you grant more rights to the API key.
16+
extern const String SECRET_LIVEOBJECTS_API_KEY = "...";
464 KB
Loading
22.9 KB
Loading
34.1 KB
Loading
49.4 KB
Loading
46.8 KB
Loading
73.1 KB
Loading

0 commit comments

Comments
 (0)