|
| 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 | +char ambient_light[20]; // ambient light level [lx] |
| 16 | +u_int8_t distance; // distance from obstruction [mm] |
| 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(TLS); |
| 30 | + lo.begin(MQTT, TEXT, 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", distance); // adding 'distance' value to the current payload |
| 41 | + sprintf(ambient_light, "%.2f", sensor.getAmbientLight(GAIN_1)); |
| 42 | + lo.addToPayload("ambient_light", ambient_light); // adding 'ambient_light' value to the current payload |
| 43 | + |
| 44 | + Serial.println("Sending data to Live Objects"); |
| 45 | + lo.sendData(); // send the data to Live Objects |
| 46 | + lastMessageTime = millis(); |
| 47 | + } |
| 48 | + lo.loop(); // don't forget to keep this in your main loop |
| 49 | +} |
0 commit comments