Bosch IoT Device Management

Compile and run the sketch

In this tutorial, we will use the sketch example opla_kit.ino in order to connect the Arduino Oplà IoT Kit.

In a similar way, you can use the other two sketch examples available in the project, or write your own following this model:

#include <string.h>
 
#include "IoTAgent.h"
#include "feature_wifi.h"
#include "feature_test.h"
#include "features_opla.h"
 
#include "config_wifi.h"
#include "config.h"
 
#define BOARD_TYPE "Arduino MKR WiFi 1010"
#define BOARD_FQBN "arduino:samd:mkrwifi1010"
 
IoTAgent agent =
IoTAgent(SECRET_SSID, SECRET_PASS, A0);
 
MKRIoTCarrier carrier;
 
void setup() {
 
Serial.begin(9600);
// Waiting for Serial to start
while (!Serial);
 
CARRIER_CASE = true;
carrier.begin();
setCarrier(&carrier);
 
// enable IoTAgent debug
setDebugLevel(DebugLevel::TRACE);
 
agent
.addAttribute("Type", String(BOARD_TYPE))
.addAttribute("FQBN", String(BOARD_FQBN))
.addFeature(testFeature())
.addFeature(temperatureFeature())
.addFeature(humidityFeature())
.addFeature(pressureFeature())
.addFeature(buttonsFeature())
.addFeature(buzzerFeature())
.addFeature(ledsFeature())
.addFeature(accelerationFeature())
.addFeature(displayFeature());
 
// connecting the agent to the MQTT host
agent.connect(
MQTT_HOST, MQTT_PORT,
TENANT_ID, THING_NAMESPACE, THING_NAME,
DEVICE_AUTH_ID, DEVICE_PASSWORD
);
}
 
void loop() {
while(!carrier.Light.colorAvailable()) {
delay(5);
}
agent.loop();
delay(3000);
}


The sketch explained

Following the model above, this is what you should include in your sketch:

Import the project libraries

  • IoTAgent.h - mandatory, as this will ensure the connectivity and communication between the device and the cloud infrastructure

  • features_opla.h, feature_test.h, feature_wifi.h - optional, as it includes implementations of the Opla kit sensors in the form of features
    As an alternative to these ready feature implementations, you can add your own.

#include "IoTAgent.h"
#include "feature_wifi.h"
#include "feature_test.h"
#include "features_opla.h"

Import the config files

#include "config_wifi.h"
#include "config.h"

Define attributes

#define BOARD_TYPE "Arduino MKR WiFi 1010"
#define BOARD_FQBN "arduino:samd:mkrwifi1010"

Create an IoT Agent instance

The IoT Agent constructor takes the WiFi network properties (SECRET_SSID, SECRET_PASS) and an analog pin (A0) to be used to generate keys for SSL connections.

There can be only one instance of the agent in a single sketch.

IoTAgent agent =
IoTAgent(SECRET_SSID, SECRET_PASS, A0);

Include Carrier library

The Carrier library is a joint library which includes libraries providing functionalities about the Opla kit sensors and allows you to program them.

MKRIoTCarrier carrier;
 
void setup() {
 
Serial.begin(9600);
// Waiting for Serial to start
while (!Serial);
 
CARRIER_CASE = true;
carrier.begin();
setCarrier(&carrier);

Comment the line // while (!Serial); if you are connecting the Opla kit directly to the power suply and not to the Arduino IDE.


Enable debugging

Set your most appropriate debug level via the setDebugLevel method so that you can see logs from the IoT Agent.

The default debug level is WARN, however, the recommended one is TRACE.

// enable IoTAgent debug
setDebugLevel(DebugLevel::TRACE);

See also Set debug level and stream.

Add attributes and features

Inside the setup() function, you can configure the device by:

  • adding the attributes you have defined earlier in the sketch

  • adding features from the imported libraries

If you have created your own features, you can add them here as well.

Use the addAttribute and addFeature methods of the agent, respectively.

agent
.addAttribute("Type", String(BOARD_TYPE))
.addAttribute("FQBN", String(BOARD_FQBN))
.addFeature(testFeature())
.addFeature(temperatureFeature())
.addFeature(humidityFeature())
.addFeature(pressureFeature())
.addFeature(buttonsFeature())
.addFeature(buzzerFeature())
.addFeature(ledsFeature())
.addFeature(accelerationFeature())
.addFeature(displayFeature());

Connect the IoT Agent

Call the method agent.connect using the properties from the config.h file to connect the IoT Agent to the MQTT host.

// connecting the agent to the MQTT host
agent.connect(
MQTT_HOST, MQTT_PORT,
TENANT_ID, THING_NAMESPACE, THING_NAME,
DEVICE_AUTH_ID, DEVICE_PASSWORD
);

Keep the connection open

Inside the loop() function, call the method agent.loop() to keep the MQTT connection open.

The loop() function goes endlessly, until the device has been disconnected or turned off.

void loop() {
while(!carrier.Light.colorAvailable()) {
delay(5);
}
agent.loop();
delay(3000);

In this example, after initialization, the loop of the sketch executes everything within it at 3-second intervals.

Compile the sketch and upload it to the Arduino board

The Arduino IDE knows the board as Arduino MKR WiFi 1010.

images/confluence/download/attachments/2341255673/arduino-ide-compile-and-upload-buttons.png

Use the Arduino tooling (top left checkmark button) to verify that the project compiles. Otherwise fix the issues.

Use the Arduino tooling (top left arrow button) to flash the Arduino.

Check your work

The Arduino serial monitor should now show the data which the board is sending to the device connectivity layer.

images/confluence/download/attachments/2341255673/serial-monitor-with-data.png

The user interface of Bosch IoT Device Management should now show the device connected and new features which are updated with the respective values of the Arduino MKR IoT Carrier device.