Connecting the Nicla Sense ME to IoT Cloud

This tutorial shows you how to set up the Arduino Nicla Sense ME and the Arduino Portenta H7 to connect to Arduino Cloud IoT and upload sensor data.

Overview

In this tutorial you will learn how to upload data from the Nicla module to the IoT Cloud. You will use the Portenta H7 to interface with the Nicla Sense ME using the eslov connector and upload the data using the Portenta Wireless capabilities.

Goals

  • How to configure the Portenta H7 to read temperature values from the Nicla Sense using the ESLOV connector.
  • How to Connect the Portenta H7 to the IoT cloud
  • Publish the temperature values that we obtained fron the Nicla board to the Arduino IoT Cloud.

Required Hardware and Software

  • Portenta H7 board
  • Nicla sense ME board
  • Eslov cable (included with the Nicla Sense ME)
  • USB-C to USB-A / C depending on your hardware (Portenta H7)
  • USB-A to micro USB-A (Nicla Sense ME)
  • Wi-fi Access point with access to the internet
  • Arduino Create account

Instructions

Hardware Connection

For the hardware setup just connect the Nicla board to the Portenta H7 using the Eslov cable like in the illustration below. Then connect the Portenta H7 to your computer using an USB-C cable.

Nicla connection

Setup Eslov Communication with the NICLA Board

There are three ways to read from the on-board sensors:

  1. Read the sensors directly from Nicla Sense ME in standalone mode.
  2. Read sensor values through BLE
  3. Read sensor values through UART by connecting an ESLOV cable.

For further tips on how to operate the Nicla module check the cheat sheet.

Client Board (Nicla Sense ME)

The Nicla Sense ME will be listening to the Host board to send back the required data, this is all automated by the libraries Arduino_BHY2 and Arduino_BHY2Host

The code its available inside the examples provided with the Arduino_BHY2 Library. Open it by going to Examples -> Arduino_BHY2 -> App.ino

This is the code, which initialize the sensors, and maintain the communication

1/*
2 * Use this sketch if you want to control nicla from
3 * an external device acting as a host.
4 * Here, nicla just reacts to external stimuli coming from
5 * the eslov port or through BLE
6*/
7
8#include "Arduino.h"
9#include "Arduino_BHY2.h"
10
11void setup(){
12 BHY2.begin(NICLA_I2C, NICLA_VIA_ESLOV);
13}
14
15void loop(){
16 // Update and then sleep X ms
17 BHY2.update(100);
18}

Set up the IoT Cloud

To configure the IoT cloud you can follow this tutorial.

Create a new thing at https://create.arduino.cc/iot/things, we called it "PRO - Portenta and Nicla". You will need to attach the Portenta H7 as a new device to your Thing setup. After that go to Variables and click the add button and select a float variable called temperature to store the temperature readings.

Arduino IoT Cloud - Thing Setup
Arduino IoT Cloud - Thing Setup

Don't forget to add your Wi-Fi SSID name, and its password (you can do that inside the Thing setup tab) to be able to connect to the Arduino IoT Cloud

Host Board: Edit the Cloud Sketch

You can edit the sketch by clicking the sketch tab inside your Thing page. The sketch is automatically generated with the enough code to upload it and connect to the cloud.

Before uploading you should add the following code:

First include the headers that you need and declare the temperature sensor by adding the temperature sensor ID.

1#include "thingProperties.h"
2#include "Arduino.h"
3#include "Arduino_BHY2Host.h"
4
5Sensor tempSensor(SENSOR_ID_TEMP);

You can find all the Sensor IDs at https://docs.arduino.cc/tutorials/nicla-sense-me/cheat-sheet#sensor-ids

Inside

void setup()
initialize the
Serial
communication, configure the variables and configuration for the Arduino IoT Cloud(properties), wait until the Portenta H7 is connected to the Wi-Fi and IoT Cloud and after that it will setup the communication with the Nicla Sense ME and configure the temperature sensor.

Note: Now we are using "NICLA_VIA_ESLOV". In case you mount it as a shield use "NICLA_AS_SHIELD" as the second parameter of the

begin()
function, or "NICLA_VIA_BLE" if you use BLE

1void setup(){
2 Serial.begin(9600);
3 delay(1500);
4
5 Serial.print("Configuring the Arduino IoT Cloud");
6 // Defined in thingProperties.h
7 initProperties();
8
9 // Connect to Arduino IoT Cloud
10 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
11
12 // Wait to be connected before intitalize the communication with the Nicla Sense ME
13 Serial.println("Connecting to the Arduino IoT Cloud");
14 while (ArduinoCloud.connected() != 1) {
15 ArduinoCloud.update();
16 delay(500);
17 }
18
19 delay(1500);
20
21 Serial.println("Initialize the Nicla communication")
22 BHY2Host.begin(false, NICLA_VIA_ESLOV);
23
24 //If you want to connect the NICLA through BLE use the following line instead of the above
25 //while(!BHY2Host.begin(false, NICLA_VIA_BLE)) {}
26
27 tempSensor.configure(1, 0);
28 temperature = tempSensor.value();
29 }

If you use

yourSensor.begin()
it will be configured the same as with
yourSensor.configure(1,0)

If the Nicla Sense ME shall communicate through BLE, we recommend wrapping

BHY2Host.begin(false, NICLA_VIA_BLE)
in a
while
clause to make sure the connection is established before the sketch continues.

Inside the

void loop()
function you will make the Portenta H7 get all the needed data from the Nicla Sense ME, store and print the temperature sensor's value and update the data to the IoT Cloud.

1void loop(){
2 BHY2Host.update();
3 temperature = tempSensor.value();
4
5 Serial.print("Temperature: ");
6 Serial.println(temperature);
7
8 ArduinoCloud.update();
9}

Upload the sketch from the sketch tab by clicking the second button at the top left of the left side of the sketch bar. Once it has been uploaded you can see the temperature value that it is being uploaded by going to your Thing Setup tab, and looking at the last value of the temperature's value, you can also open the serial monitor to see the live data.

Arduino IoT Cloud - Sketch tab
Arduino IoT Cloud - Sketch tab
The Sketch:

1/*
2 Sketch generated by the Arduino IoT Cloud Thing "PRO - Portenta and Nicla IoT"
3
4 Arduino IoT Cloud Variables description
5
6 The following variables are automatically generated and updated when changes are made to the Thing
7
8 float temperature;
9
10 Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
11 which are called when their values are changed from the Dashboard.
12 These functions are generated with the Thing and added at the end of this sketch.
13*/
14
15#include "thingProperties.h"
16#include "Arduino_BHY2Host.h"
17
18Sensor tempSensor(SENSOR_ID_TEMP);
19
20void setup() {
21 Serial.begin(9600);
22 delay(1500);
23
24 Serial.print("Configuring the Arduino IoT Cloud");
25 // Defined in thingProperties.h
26 initProperties();
27
28 // Connect to Arduino IoT Cloud
29 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
30
31 Serial.println("Connecting to the Arduino IoT Cloud");
32 while (ArduinoCloud.connected() != 1) {
33 ArduinoCloud.update();
34 delay(500);
35 }
36
37 delay(1500);
38
39
40 Serial.println("Initialize the Nicla and the ");
41 BHY2Host.begin(false, NICLA_VIA_ESLOV);
42 tempSensor.configure(1, 0);
43 temperature = tempSensor.value();
44}
45
46void loop() {
47 // Your code here
48 BHY2Host.update();
49 temperature = tempSensor.value();
50 Serial.print("Value: ");
51 Serial.println(temperature);
52 ArduinoCloud.update();
53
54}
55
56/*
57 Since temperature is READ_WRITE variable, onTemperatureChange() is
58 executed every time a new value is received from IoT Cloud.
59*/
60void onTemperatureChange() {
61 // Add your code here to act upon temperature change
62}

Conclusion

In this tutorial you learned how to upload the temperature values from the Nicla sense to the IoT cloud. You followed the process to set up the Nicla board to send data via the Eslov connection, and you configured the IoT cloud to receive the temperature data.

Next Steps

  • Try to upload other sensor data from he Nicla sense. You can see the available sensors in the cheat sheet.
  • Experiment with the dashboard to add more data for a more sophisticated project.

Troubleshooting

ArduinoIoT Cloud

If you encounter any issue in the process of using the Arduino IoT Cloud, please visit the IoT Cloud Getting started https://docs.arduino.cc/cloud/iot-cloud/tutorials/iot-cloud-getting-started

Tutorial Toolbox

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.