The STM32L4 Discovery kit IoT node allows direct connections to the cloud, making it ideal for several IoT applications. In this tutorial, we’re going to use it for a smart containers application built over scriptr.io. The following steps will guide you through the process of connecting your device to scriptr.io, and getting your smart containers application running in under an hour. The application features a map to locate your device, a real-time dashboard, data logging and a business rules editor. You will have all what you need to take your application from proof of concept to production.
You can also check the accompanying video tutorial which is a good visual reference: https://youtu.be/YEOTlKrcGCg
- STM32L4 Discovery kit IoT node: attach the STM32 device to your PC using a USB cable.
- STM32 Cube Expansion: customized for this app with the capability to accept the MQTT pubsub topics via terminal at runtime. You need to download this binary file in order to install it on the device.
- A scriptr.io account pre-loaded with the B-L475E-IOT01A apps: you can register for free using the following URL: https://www.scriptr.io/register?promocode=B-L475E-IOT01A.
Recipe: Step by Step
Registering for a free scriptr.io account with the B-L475E-IOT01A promo code
In this step you will need to create a scriptr.io account using the B-L475E-IOT01A promotion code in order to have the B-L475E-IOT01A Smart Containers app automatically installed on your account, then copy the MQTT configuration to use it with the device in the subsequent steps:
- Create a scriptr.io account using the B-L475E-IOT01A promo code using the following link in order to get the device’s apps automatically installed on your account: https://www.scriptr.io/register?promocode=B-L475E-IOT01A
- Complete the registration process. For this, you need to click the link in the verification email (check the spam folder if you can’t find it).
- Log in to your scriptr.io Workspace
- Click your username on the top right corner, then select Messaging Protocols
- Under the Protocol section, make sure MQTT is selected
- Under the Credentials section, select myDevice from the Device drop-down. This will display the Username and Password right under that drop-down, which you will need later.
- Under the Publishing Details section, select deviceDigest from the Channel drop-down. This will display the Messaging Topic right under that drop-down, which you will need later.
Install & Configure the STM32L4 App
2.1. Import and Install the STM32L4 Project
Plug in your device into a USB port. If you’re using Windows, you can deploy the bin file (i.e. flash the device) and trigger the appropriate ignition behavior by dragging and dropping the B-L475E-IOT01_GenericMQTT.bin file into the corresponding mounted drive in Windows Explorer. After a few seconds, Explorer will close and re-open, and the device will reboot. Should you disconnect and connect the device again, you will not need to re-install the bin file.
If you wish to modify the code on the device and install your own modified version of the STM32 Cube Expansion, you can do so by following the below steps:
- Download and install the System Workbench for STM32 eclipse-based IDE. You will need to register on the website in order to download it.
- Download and extract the Generic IoT cloud software expansion for STM32Cube (X-CUBE-CLD-GEN). You need to scroll down to the bottom of the page and click Get Software to download the package. You can read more about it here.
- Open System Workbench for STM32 and import the STM32CubeExpansion_Cloud_CLD_GEN_V1.0.0/Projects/B-L475E-IOT01/Applications/Cloud/GenericMQTT/SW4STM32/B-L475E-IOT01_GenericMQTT project from the extracted package in the previous step.
- Open our STM32-B-L475E-IOT01A GitHub repository which contains two modified files under the src folder, download these 2 files and copy them to your local project by replacing the ones you downloaded from the STMicroelectronics website in step 2 above.
- You now have the project with our changes applied, and you will be able to modify it as you see fit. Once you do, just build the project and copy the B-L475E-IOT01_GenericMQTT.bin (which you can find under the project’s Debug folder) to the device.
2.2. Configure the Application on the Device
We now need to configure the application on the device:
- Open a terminal tool such as Tera Term for Windows and configure it as follows:
- Set the serial port’s baud rate to 115,200
- Set the terminal’s new line to AUTO for Receive, and LF for transmit
- The first time you launch the board and press the black button, it will ask you for your WiFi configuration. Press the Blue Button on the device if you wish to change your WiFi settings, an enter the values that match your local WiFi settings, they will be saved in the device’s memory (MCU Flash memory) so that next time you reboot you will not have to re-enter them.
- Additionally, you will have to set the MQTT connection settings (the next time you boot the device, you will have the option to modify these settings; to do so, press the blue button when it asks if you wish to modify the security settings). The connection string has the below format; here you need to use the Username, Password and Messaging Topic that you retrieved from scriptr.io’s Messaging Protocols dialog in the first step of this tutorial. Once you build the connection string, enter it in the terminal and press Enter.
HostName=mqtt.scriptr.io;HostPort=1883;ConnSecurity=0;MQClientId=mySTM32L4;MQUserName=[Username];MQUserPwd=[Password];SubscriptionTopic=[Messaging Topic];PublicationTopic=[Messaging Topic];
Make sure you use the proper values as follows:
- HostName: mqtt.scriptr.io
- HostPort: 1883
- ConnSecurity: possible values are:
- -1: Undefined
- 0: Clear connection
- 1: Encrypted TLS connection, with no authentication of the remote host (should not be used in a production environment)
- 2: Encrypted TLS connection, with authentication of the remote host
- 3: Encrypted TLS connection, with mutual authentication
- MQClientId: MQTT Client ID
- MQUserName: the Username as per section 1.6 above
- MQUserPwd: the Password as per section 1.6 above
- SubscriptionTopic: the Messaging Topic as per section 1.7 above
- PublicationTopic: the Messaging Topic as per section 1.7 above
- Once you enter the above in the terminal tool, it’s going to prompt you for the root CA. Copy the below root CA for scriptr.io which is valid till 12/3/2020 and submit it in the terminal (you could always retrieve the root CA from www.scriptr.io using your web browser). If you’re using Tera Term, you need to use the Paste<CR>option under the Edit menu, click OK, then hit Enter:
-----BEGIN CERTIFICATE----- MIIEADCCAuigAwIBAgIBADANBgkqhkiG9w0BAQUFADBjMQswCQYDVQQGEwJVUzEh MB8GA1UEChMYVGhlIEdvIERhZGR5IEdyb3VwLCBJbmMuMTEwLwYDVQQLEyhHbyBE YWRkeSBDbGFzcyAyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTA0MDYyOTE3 MDYyMFoXDTM0MDYyOTE3MDYyMFowYzELMAkGA1UEBhMCVVMxITAfBgNVBAoTGFRo ZSBHbyBEYWRkeSBHcm91cCwgSW5jLjExMC8GA1UECxMoR28gRGFkZHkgQ2xhc3Mg MiBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTCCASAwDQYJKoZIhvcNAQEBBQADggEN ADCCAQgCggEBAN6d1+pXGEmhW+vXX0iG6r7d/+TvZxz0ZWizV3GgXne77ZtJ6XCA PVYYYwhv2vLM0D9/AlQiVBDYsoHUwHU9S3/Hd8M+eKsaA7Ugay9qK7HFiH7Eux6w wdhFJ2+qN1j3hybX2C32qRe3H3I2TqYXP2WYktsqbl2i/ojgC95/5Y0V4evLOtXi EqITLdiOr18SPaAIBQi2XKVlOARFmR6jYGB0xUGlcmIbYsUfb18aQr4CUWWoriMY avx4A6lNf4DD+qta/KFApMoZFv6yyO9ecw3ud72a9nmYvLEHZ6IVDd2gWMZEewo+ YihfukEHU1jPEX44dMX4/7VpkI+EdOqXG68CAQOjgcAwgb0wHQYDVR0OBBYEFNLE sNKR1EwRcbNhyz2h/t2oatTjMIGNBgNVHSMEgYUwgYKAFNLEsNKR1EwRcbNhyz2h /t2oatTjoWekZTBjMQswCQYDVQQGEwJVUzEhMB8GA1UEChMYVGhlIEdvIERhZGR5 IEdyb3VwLCBJbmMuMTEwLwYDVQQLEyhHbyBEYWRkeSBDbGFzcyAyIENlcnRpZmlj YXRpb24gQXV0aG9yaXR5ggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQAD ggEBADJL87LKPpH8EsahB4yOd6AzBhRckB4Y9wimPQoZ+YeAEW5p5JYXMP80kWNy OO7MHAGjHZQopDH2esRU1/blMVgDoszOYtuURXO1v0XJJLXVggKtI3lpjbi2Tc7P TMozI+gciKqdi0FuFskg5YmezTvacPd+mSYgFFQlq25zheabIZ0KbIIOqPjCDPoQ HmyW74cNxA9hi63ugyuV+I6ShHI56yDqg+2DzZduCLzrTia2cyvk0/ZM/iZx4mER dEr/VxqHD3VILs9RaRegAhJhldXRQLIQTO7ErBBDpqWeCtWVYpoNz4iCxTIM5Cuf ReYNnyicsbkqWletNw+vHX/bvZ8= -----END CERTIFICATE-----
Once you’re done configuring MQTT, the application will try to set the RTC. The first time you turn the board on, you will get the below error. Ignore it – for more in this subject, refer to first paragraph of section 11.5 (Application runtime) of the official documentation.
Setting the RTC from the network time. C:/en.x-cube-cld-gen/STM32CubeExpansion_Cloud_CLD_GEN_V1.0.0/Middlewares/Third_Party/mbedTLS/library/ssl_tls.c:4587: x509_verify_cert() returned -9984 (-0x2700)
Run your Scriptr.io Application
- Navigate to your scriptr.io Workspace
- Open the /smartcontainer/index.html file and click View to open it in the browser
- Now that the app is running, log in using the credentials demo / demo
- For the device to start sending data to scriptr.io and have it displayed in the app, you have to press the blue button on the device. You can perform either a single push or a double push on the blue button (User button). According to the device’s documentation, the two user actions are as follows:
- Single push on the User button
Triggers a sampling of the sensor values and their publication to the broker through a publish MQTT message on the telemetry topic (/sensors/ in the generic case).
- Double push on the User button
Starts or stops the publication loop of the sensor values. When the loop is running, the sensor values are published every Telemetry interval seconds. Each sensor values publication is signaled by the user LED blinking quickly for half a second.
- Single push on the User button
You should now see your device on the map (it will be shown in a predefined location since the device doesn’t send a GPS location). Zoom in on the device and click its marker/pin to see the sensors’ readings. This data is updated in real-time, i.e. every time the device sends a new message to scriptr.io, you can see it on the terminal window if you’re connected to the device, and you will also see the new values reflected in the app as well:
You can click View Dashboard to go to a dashboard showing several charts, including time series charts showing the variations over time. This view is updated in real-time as well:
You can also click View Alerts in order to see the warnings logged whenever a value exceeds a certain preset threshold:
Thresholds are set as rules in the Rules Designer which you can access from the left side bar:
Here you can set several rules for each of the sensors values, i.e. temperature, humidity and pressure, which are listed in the first column of the rules designer. As you can see, in the “Rule 1” column, the first row (temperature row) reads “>8”, meaning that whenever the temperature is great than 8, an email notification is sent to the user, as indicated in the sendEmail row, and an alert is logged, as indicated in the last row corresponding to the isAlert row when set to true. Similarly, “Rule 2” means than an email alert is sent to the user whenever the humidity is greater than 30; and so on…
In order to set the recipients of the alerts, click on the user icon on the top right corner of the screen and select Notification Settings. This will open a form allowing you to enter a comma-separated list of email addresses to receive the email alerts:
Scriptr.io allows you to reduce the time to market of your IoT applications by accelerating your development pace, thanks to a plethora of API and visual development tools, such as our dashboard builder, our decision table editor, and the multitude of tools at your disposal in the Workspace. As you can see, you have the source code of your application in your Workspace. But before making any changes, we recommend you read the Readme.md file which you can find in the tree in your Workspace. To learn more about scriptr.io, you can check out our one-page documentation as well as our how-to guides. Finally, if you have any questions or feedback, we’d love to hear from you at firstname.lastname@example.org.