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.
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.
There are three ways to read from the on-board sensors:
For further tips on how to operate the Nicla module check the cheat sheet.
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 from5 * 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 ms17 BHY2.update(100);18}
To configure the Arduino IoT cloud you can follow the tutorial Getting Started with the Arduino IoT 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.
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 IoT Cloud.
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 IoT 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
function, or "NICLA_VIA_BLE" if you use BluetoothĀ® Low Energy.begin()
1void setup(){2 Serial.begin(9600);3 delay(1500);4
5 Serial.print("Configuring the Arduino IoT Cloud");6 // Defined in thingProperties.h7 initProperties();8
9 // Connect to Arduino IoT Cloud10 ArduinoCloud.begin(ArduinoIoTPreferredConnection);11
12 // Wait to be connected before intitalize the communication with the Nicla Sense ME13 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 BluetoothĀ® Low Energy use the following line instead of the above25 //while(!BHY2Host.begin(false, NICLA_VIA_BLE)) {} 26
27 tempSensor.configure(1, 0);28 temperature = tempSensor.value();29 }
If you use
, it will be configured the same as with yourSensor.begin()
.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 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 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.
The Sketch:
1/*2 Sketch generated by the Arduino IoT Cloud Thing "PRO - Portenta and Nicla IoT"3
4 Arduino IoT Cloud Variables description5
6 The following variables are automatically generated and updated when changes are made to the Thing7
8 float temperature;9
10 Variables which are marked as READ/WRITE in the Cloud Thing will also have functions11 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.h26 initProperties();27
28 // Connect to Arduino IoT Cloud29 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 here48 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() is58 executed every time a new value is received from IoT Cloud.59*/60void onTemperatureChange() {61 // Add your code here to act upon temperature change62}
In this tutorial you learned how to upload the temperature values from the Nicla Sense ME to the Arduino IoT Cloud. You followed the process to set up the Nicla board in order to send data via ESLOV connection and you configured the Arduino IoT Cloud to receive the temperature data.
If you encounter any issue in the process of using the Arduino IoT Cloud, please visit the IoT Cloud Getting started