All Collections
Connect your Devices
Connect the STM32 Nucleo-64 development board to Ubidots over Wi-Fi
Connect the STM32 Nucleo-64 development board to Ubidots over Wi-Fi

Connect you STM32 Nucleo-64 to Ubidots and launch your asset tracking and monitoring solutions.

S
Written by Sergio M
Updated over a week ago

The STM32 Nucleo-64 is an affordable and flexible board that combines various different combinations of performance, power consumption, and features to optimize your connected applications. The Arduino™ Uno V3 connectivity support and the ST morpho headers allow you to easily expand the functionality of the STM32 Nucleo's open development platform to deploy devices for cargo tracking and monitoring or simple home automation projects like water flow meters or electric monitoring. 

The STM32 family offers an extraordinary menu of options, including ARM Cortex-M cores, giving us the flexibility to find the perfect STM32 for our applications. And the various MCU series of the STM32 family allow for various high performance, mainstream and ultra low power options.

In the following guide we will introduce the NucleoL476RG of the STM32L4 MCU series which offers an excellent option for ABC SOLUTIONs. For additional information into the STM32L4 MCU series, reference the ST product materials.

Requirements

Setup

To program the microcontroller simply access the free MBed OS online compiler; to access your account or sign up click here.

  1.  Attach the X-Nucleo-IDW01M1 (Wi-Fi shield) over NucleoL476RG board with Morpho connector:

Your completed device assembly will look as follows:

IMPORTANT NOTE: Some X-Nucleo-IDW01M1 shields require a little additional modification to fully connect the hardware. Check if your device has an R21 resistor? if so, you may have to remove the R21 resistor to fully connect the shield with the board. 

2. Go to the the Mbed OS developer site, press Hardware  > Components. Then, in the communication menu press Wi-Fi to filter the search and select the X-Nucleo-IDW01M1. 

3. Then "Import program" of the Hello World example.

Please wait a couple of seconds until the compiler is opened. Then, in the import project window, change the import name to "Ubidots_test" and check "Update all libraries to the latest revision", to complete press the import button:

4. To verify, check if the Nucleo-L476RG board is available in the up right-hand pane of the compiler. 

5. Next, verify that the libraries "NetworkSocketAPI" and "X_NUCLEO_IDW01M1v2" are imported into the project.

6. Then, open then main.cpp file and replace all content of main.cpp with the code provided below:

Once you've properly replaced the code with the one below, update your Wi-Fi credentials, assign your Ubidots TOKEN, and assign the "NucleoL476" as the device label where is indicated in the code. Note that this code is for example purposes only and should be developed to your exact specifications. In this code we will send

/*
Copyright (C) 2017 romain reicher

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   
    About
    ---------------------------------------------------------------------

Send sensors value to Ubidots.
This example sends 3 variables to Ubidots.
- STM32L476 VBAT/3 internal channel in mV
- STM32L476 Internal Temperature Sensor in C
- The status of onboard User Button (blue) on NucleoL476RG
Use NucleoL476RG with X-Nucleo-IDW01M1v2 wifi shield
 
Important note: Some IDW01M1 wifi shield had resistor R21 mounted
which interfere with STLink/SWD programmer.
  Please unmount R21 to fix it.
 
    romain reicher
    Date     : 20/09/2017
    Revision :  v0.1  
*/


#include "mbed.h"
#include "SpwfInterface.h"  
#include "TCPSocket.h"

/* Wifi Acces Point Settings */
#define AP_SSID         "YOUR_WIFI_SSID"
#define AP_PASSWORD     "YOUR_WIFI_PASSWORD"
#define UBIDOTS_SERVER  "industrial.api.ubidots.com"
#define UBIDOTS_PORT    80
#define UBIDOTS_TOKEN   "YOUR_UBIDOTS_TOKEN"
#define UBIDOTS_DEVICE  "YOUR_UBIDOTS_LABEL_DEVICE"

/* Communication ressources */
SpwfSAInterface spwf(D8, D2, false);    
Serial pc(USBTX, USBRX);

/* Digital ressources */
DigitalOut myLed(LED1);
DigitalIn myButton(USER_BUTTON);

/* Analog ressources */
AnalogIn adc_vbat(ADC_VBAT);    // VBAT / 3 internal to ADC channel
AnalogIn adc_temp(ADC_TEMP);    // Internal Temp Sensor to ADC Channel

/* Global variables */
float temp = adc_temp.read() * 100; // Converted in C
float batt = adc_vbat.read() * 30000;   // Converted in mV  
bool status = false;

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main()
{
    /* Configure Serial baud rate */
    pc.baud(115200);
   
/* Update status variable state depending USER_BUTTON state */
    if (myButton == 0)
        status = true;
    else
        status = false;
   
    TCPSocket socket(&spwf);
    char sendBuffer[256];
    char message[64];
    int err;

    /* ######################## WIFI CONNECTION ######################## */

    pc.printf("IDW01M1 NetworkSocketAPI TCP Client Ubidots\r\n");
    pc.printf("Connecting to AP\r\n");
   
    //* Connect to wifi acces point */    
    if(spwf.connect(AP_SSID, AP_PASSWORD, NSAPI_SECURITY_WPA2))
    {      
        pc.printf("Now connected\r\n");
    }
    else
    {
        pc.printf("Error connecting to AP.\r\n");
        return -1;
    }  
   
    /* #################### GET CONNECTION INFOS ######################## */
   
    /* Get and print network connection parameters ip and mac adress */  
    const char *ip = spwf.get_ip_address();
    const char *mac = spwf.get_mac_address();    

    pc.printf("IP address is: %s\r\n", ip ? ip : "No IP");
    pc.printf("MAC address is: %s\r\n", mac ? mac : "No MAC");
   
    /* ##################### UBIDOATS SEND DATA ######################### */

    printf("Sending HTTP Data to Ubidots...\r\n");
 
    /* Open a socket , create a TCP connection to Ubidots */
    err = socket.connect(UBIDOTS_SERVER, UBIDOTS_PORT);
    if (err!=0)
    {
      pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err);
      return -1;
    }
    else
        pc.printf("\r\nconnected to host server\r\n");
   
    /* Construct content of HTTP command */
    sprintf(message, "{\"temperature\": %0.2f, \"battery\": %0.2f, \"status\": %d}", temp, batt, (int)status);
    printf("Content Length = %d\r\n", (int)strlen(message));
   
    /* Construct HTTP command to send */
    sprintf(sendBuffer, "POST /api/v1.6/devices/%s/?token=%s HTTP/1.1\r\nHost: industrial.api.ubidots.com\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", UBIDOTS_DEVICE, UBIDOTS_TOKEN, (int)strlen(message),message);
    pc.printf("HTTP command %s\r\n", sendBuffer);
    wait(2.0);
     
    /* Send http request to Ubidots */
    int scount = socket.send(sendBuffer, (int)strlen(sendBuffer));
    printf("sent %d [%.*s]\r\n", scount, strstr(sendBuffer, "\r\n") - sendBuffer, sendBuffer);

    /* Receive a simple http response and print out the response line */
    char respBuffer[64];
    int rcount = socket.recv(respBuffer, sizeof respBuffer);
    printf("recv %d [%.*s]\r\n", rcount, strstr(respBuffer, "\r\n") - respBuffer, respBuffer);

    /* Close the socket to return its memory and bring down the network interface */
    pc.printf("Close Socket\r\n");
    socket.close();
   
    /* Disconnect */
    pc.printf("Disconnect Wifi\r\n");
    spwf.disconnect();
    wait(1.0);
    pc.printf("Done\r\n");

    myLed = 0;
       
    while(1)
    {
myLed = !myLed;
wait(1.0);
    }
}

7. When the code is updated with the parameters mentioned above, press the "compile" button to finish this step: 

  • The compilation will generate a BIN file, download and save it for later. 

8. Next we need to upload the BIN file into the board. To do so, you must install the STLink driver based on your computer's Operating System. (For a quick download, see here.)

9. Now, connect the Nucleo mini USB to your computer.

10. Then upload the BIN file into the board. The board will appear as an USB drive icon, so just drag-and-drop the BIN file previously downloaded into the USB drive assigned to the board; for additional details, reference this guide.  

Also, you will be able to debug each step of the program and visualize the response for the HTTP request using a terminal console. To do this, note the Virtual Com Port assigned by your operating system and open a Serial Terminal (Like Teraterm or Putty) with the specifications below: 

  • Baud rate: 115200 

  • Dat bits: 8

  • Stop bits: 1

  • Xon/Xoff: Disable

11. Now your device is connected with Wifi and able to display status, temperature, and battery. Plus, using the code provided, a new device will automatically generate in your Ubidots account with the name assigned from the code and will contain these 3 variables in your dashboard:

  • Status variable is a boolean and shows the status of the user blue button on the Nucleo Board- a 0 in Ubidots means the blue light will be off and when the button is pressed, the light will illuminate and a 1 variables will be sent to Ubidots for visualization.

  • Temperature variable is updated with STM32 Internal temperature sensor connected to ADC

  • Battery variable is updated with VBAT / 3 Internal ADC Channel

To visualize your work, go to your Ubidots account and select "Devices": 

Results

The STM32 Nucleo-64 board is a versatile option for nearly any IoT application. With the ability to connect different shields and purchase different memory and pinout specifications, this hardware can solve most, if not all, of your required IoT deployments. From tracking cargo conditions as they ship across the country or monitoring water flow speed for the local hospital, the STM32 Nucleo-64 can do it.

Using this simple tutorial you can now connect your STM32 Nucleo-64 to Ubidots and deploy your applications with ease. If you need additional resources in setting up your device or deploying your applications, reference Ubidots Help Center with any doubts.

Happy Hacking! :)

Did this answer your question?