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 ME using the ESLOV connector.
  • How to Connect the Portenta H7 to the Arduino Cloud
  • Publish the temperature values that you obtained from the Nicla board to the Arduino Cloud.

Required Hardware and Software

  • Portenta H7
  • Nicla Sense ME
  • 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 Cloud 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 Bluetooth® Low Energy
  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 via the libraries Arduino_BHY2 and Arduino_BHY2Host

The code is 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 Bluetooth® Low Energy
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 Arduino Cloud

To configure the Arduino Cloud you can follow the tutorial Getting Started with the Arduino Cloud.

Create a new Thing at https://create.arduino.cc/iot/things, you can call 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, click the add button and select a float variable called temperature to store the temperature readings.

Arduino Cloud - Thing Setup
Arduino Cloud - Thing Setup

Remember 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 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 enough code to upload it and connect it 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, set up the variables and configuration for the Arduino Cloud (properties) and wait until the Portenta H7 is connected to the Wi-Fi and IoT Cloud. At this point, the communication with the Nicla Sense ME will be set up and you can start configuring 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 Bluetooth® Low Energy.

1void setup(){
2 Serial.begin(9600);
3 delay(1500);
4
5 Serial.print("Configuring the Arduino Cloud");
6 // Defined in thingProperties.h
7 initProperties();
8
9 // Connect to Arduino 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 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 Bluetooth® Low Energy 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 communicates through Bluetooth® Low Energy, 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 value and update the data to the Arduino 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 side of the sketch bar.

Once it has been uploaded, you can see the temperature value just uploaded by going to your Thing Setup tab and looking at the last value of the temperature variable. You can also open the Serial Monitor to see your data live.

Arduino Cloud - Sketch tab
Arduino Cloud - Sketch tab

The Sketch:

1/*
2 Sketch generated by the Arduino Cloud Thing "PRO - Portenta and Nicla IoT"
3
4 Arduino 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 Cloud");
25 // Defined in thingProperties.h
26 initProperties();
27
28 // Connect to Arduino Cloud
29 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
30
31 Serial.println("Connecting to the Arduino 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 ME to the Arduino Cloud. You followed the process to set up the Nicla board in order to send data via ESLOV connection and you configured the Arduino Cloud to receive the temperature data.

Next Steps

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

Troubleshooting

Arduino Cloud

If you encounter any issue in the process of using the Arduino Cloud, please visit the Getting Started with Arduino Cloud

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.