Connect through Wifi to IBM MQTT cloud https://quickstart.internetofthings.ibmcloud.com

Dependencies:   MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 X_NUCLEO_IKS01A1 mbed NDefLib X_NUCLEO_NFC01A1

Fork of IDW01M1_Cloud_IBM by ST Expansion SW Team

To start the demo the following expansion boards are required

X_NUCLEO_IDW01M1v2, X_NUCLEO_IKS01A1, X_NUCLEO_NFC01A1

After having mounted the board stack on the Nucleo board the below steps should be followed:

  • Program in the application source code you local WiFi SSID and password and flash the binary. Make sure the Wifi network has visible SSID.
  • Reset the Nucleo board and after few seconds the Nucleo green led will be on (it means the Nucleo is connected to the local Wifi and to the IBM cloud server)
  • Read the NFC tag with an Android device and the browser will be automatically opened and directed to the specific brocker IBM demo page where the environmental values are displayed in form of a x-y graph. The values are updated every few seconds. On the Hyperterminal is possible to see the values sent to the IBM cloud server and the board mac address to be entered on the IBM quickstart web page if a manual connection is needed (eg. to connect from a PC browser).

main.cpp

Committer:
mridup
Date:
2016-04-19
Revision:
6:0d838d564181
Parent:
5:e913a401b174
Child:
8:6df01cb43137

File content as of revision 6:0d838d564181:

#include "mbed.h"
#include "SPWFInterface.h"
#include "TCPSocket.h"

//------------------------------------
// Hyperterminal configuration
// 9600 bauds, 8-bit data, no parity
//------------------------------------

Serial pc(SERIAL_TX, SERIAL_RX);
 
DigitalOut myled(LED1);

SpwfSAInterface spwf(PA_9, PA_10, PC_12, PC_8, PA_12, true);
TCPSocket tcp_(&spwf);    
    
int main() {
    int32_t err;    
    char * ssid = "STM";
    char * seckey = "STMdemoPWD";
    char * data = "Hello World!\r\n";
    uint32_t len = strlen(data);
    
    pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
    pc.printf("\r\n[APP] connecting to AP\r\n");
            
    err = spwf.connect(ssid, seckey, NS_SECURITY_WPA);
    if(err!=0)
    {
        pc.printf("\r\n[APP] error connecting to AP.\r\n");
        return -1;
    }
    
    pc.printf("\r\n[APP] now connected\r\n");
            
    const char *ip = spwf.getIPAddress();
    const char *mac = spwf.getMACAddress();
    
    pc.printf("\r\n[APP] IP Address is: %s\r\n", (ip) ? ip : "No IP");
    pc.printf("\r\n[APP] MAC Address is: %s\r\n", (mac) ? mac : "No MAC");
    
    pc.printf("\r\n[APP] opening socket\r\n");
          
    err = tcp_.open("time-d.nist.gov", 37);
            
    if(err==0)
    {                
        pc.printf("\r\n[APP] socket open OK\r\n");
        pc.printf("\r\n[APP] hostname resolved to: %s\r\n", tcp_.getIPAddress());
        
        err = tcp_.send(data, len);
        
        if(err == 0)
            pc.printf("\r\n[APP] socket send OK\r\n");
        else
            pc.printf("\r\n[APP] socket send error\r\n");
        
        char received[4];
        int32_t size = 0;
        pc.printf("\r\n[APP] waiting for data recv\r\n");
        size = tcp_.recv(received, sizeof(received));
        if(size==0) 
            pc.printf("\r\n[APP] Receive error.");
        else
            pc.printf("\r\n[APP] Received: %ld bytes, 0x%02x 0x%02x 0x%02x 0x%02x\r\n", size, received[0], received[1], received[2], received[3]);
    }            
    else 
        pc.printf("\r\n[APP] socket open Error\r\n");
                
    while(1) { 
      wait(1);
      myled = !myled;
    }
}