Example of AWS IoT connection and Web Dashboard thru STM32 Nucleo evaluation board and mbed OS.
Dependencies: X_NUCLEO_IKS01A1 mbed FP MQTTPacket DnsQuery ATParser
Introduction
The demo is aimed to STM32 Nucleo board with WiFi and sensors expansions. The board is a "thing" for the AWS IoT service. It updates IoT service shadow with sensors data every second and checks subscription messages.
Hardware Configuration
- NUCLEO-F401RE - Nucleo Development Board
- X-NUCLEO-IDW01M1 - Wi-Fi expansion board for STM32 Nucleo
- X-NUCLEO-IKS01A1 - Motion MEMS and environmental sensor expansion board for STM32 Nucleo
Software Configuration
- Import this Project to mbed online compiler
- Find the next part of code in main.cpp file ...
WiFi network credential
#include "mbed.h" // WiFi network credential #define SSID "" // Network must be visible otherwise it can't connect #define PASSW "" #error "Wifi SSID & password empty"
- ... And set it to your Network Name and Password. Do not forget to remove "#error" pragma line.
Information
Nucleo WiFi module is not the same as your smartphone or laptope - it is based on demo board. To avoid connection problems:
- Place Nucleo as close to WiFi hot spot as possible. Or...
- Turn on mobile hot spot in your laptop as close to the device as possible.
- Make sure that hot spot permits 2.4 GHz band communications
- Setup BackEnd and store certificates using this backend setup instruction
- Find AWS_IOT_MQTT_HOST define and change it to HTTPS point mentioned in your AWS IoT thing properties named "interact"
#define AWS_IOT_MQTT_HOST "xxxxxxxxxx.iot.us-east-1.amazonaws.com" //Use your own host.
- Find the certificate defines clientCRT and clientKey in main.cpp file and change it to ones provided by Amazon.
/********************************************************************************************** *********************************************************************************************** Device Identity Certificates: Modify for your AWS IoT Thing *********************************************************************************************** ***********************************************************************************************/ /**************************************** (somecode)-certificate.pem.crt - Amazon signed PEM sertificate. *****************************************/ //This Client cert is example. Use own instead. const uint8_t clientCRT[] = "\ -----BEGIN CERTIFICATE-----\n\ MIIDBjCCAe6gAwIBAgIUVph856omeIxW3UPioq+UrX1DbwowDQYJKoZIhvcNAQEL\ BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g\ SW5jLiBMPVNlYXR0bGUgU1Q9V2FzaGluZ3RvbiBDPVVTMB4XDTE3MDUyNTExNTEy\ OVoXDTQ5MTIzMTIzNTk1OVowgZUxCzAJBgNVBAYTAkJZMQ4wDAYDVQQIDAVNaW5z\ azEOMAwGA1UEBwwFTWluc2sxFzAVBgNVBAoMDktsaWthLVRlY2ggTExDMRcwFQYD\ VQQLDA5LbGlrYS1UZWNoIExMQzEMMAoGA1UEAwwDUm5EMSYwJAYJKoZIhvcNAQkB\ FhdtdmF0YWxldUBrbGlrYS10ZWNoLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEH\ A0IABCJgOQJmoTBJVPfli9Hm/JVixaxkY5rtlgrYO3hSl633A2hg0P/ue0wXDbF3\ aQ0X57IRFE4k4FEbr3UXjT/IczKjYDBeMB8GA1UdIwQYMBaAFK3YzTUPlYB2Li75\ i/z8rEogr1d6MB0GA1UdDgQWBBT18HXBaXFJuAR/0SwegnxJ+pyJ6TAMBgNVHRMB\ Af8EAjAAMA4GA1UdDwEB/wQEAwIHgDANBgkqhkiG9w0BAQsFAAOCAQEAb0Ux1aH5\ RLxjrfGqXN6rPVqh8QQRS+AyBfzmaQN8HaPZMkX5WxXLvcn0A3uWlwQxPPkcZ4zf\ 51GHtFFQWB4YZ8dx8mUQ0v/j7onHjCJgZ8iDgwOyKMGtnsDZWCakQw+a6cj+NrMZ\ tzhjwCzEEP6ePcbXwErI5OOzLuWns2L/JEr2wWNkokgRuS8ewr/SQ9OLWIWa2rFM\ ahPNTb3y/qBeWdjeJmhI+TOxdqIpsF8roWP25zwo/zkzCHCjXFBrL+0CA4MpxIl9\ x02i7aAhlJ6ys80lDxdeWeeQJXRKkGknP8mcmKn3iEqqJ5s1dQePj2b5d3ldatya\ wsxQBqqZXzIWEw==\ \n\ -----END CERTIFICATE-----\n"; /********************************************************************************************** *********************************************************************************************** Private Key: Modify for your AWS IoT Thing *********************************************************************************************** ***********************************************************************************************/ /********************************************************************8**************************************** nucleo.key.pem - client key generated according to readme. **************************************************************************************************************/ //This Client Key is example. Use own instead. const uint8_t clientKey[] ="\ -----BEGIN EC PARAMETERS-----\n\ BggqhkjOPQMBBw==\ -----END EC PARAMETERS-----\n\ -----BEGIN EC PRIVATE KEY-----\n\ MHcCAQEEIHPRfWSC8/k/BsqDWKuP15dXsI9fGwpkTIsLZe6mIrAAoAoGCCqGSM49\ AwEHoUQDQgAEImA5AmahMElU9+WL0eb8lWLFrGRjmu2WCtg7eFKXrfcDaGDQ/+57\ TBcNsXdpDRfnshEUTiTgURuvdReNP8hzMg==\ -----END EC PRIVATE KEY-----\n";
Build and Check
- Plugin your board to USB of your PC. USB Disk Drive and USB COM Port should appear in your system.
- Open any Serial Console, connect it to your USB Serial Port and setup speed equal to 115200.
- Compile this Project and save .bin file to USB Disk Drive
- After board reset you should see next log in serial console:
X-NUCLEO-IDW01M1 mbed Application connecting to AP LOG: int main() L#361 Connected to WiFI. LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#186 ===================================== LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#187 Connecting WiFi. LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#188 Nucleo IP ADDRESS: X.X.X.X LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#189 Nucleo MAC ADDRESS: 00:11:22:33:44:55 LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#190 Server Hostname: xxxxxxxx.iot.us-east-1.amazonaws.com port: 8883 LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#191 Client ID: Nucleo LOG: int connect(MQTT::Client<MQTTWiFi, Countdown, 350, 5> *, MQTTWiFi *) L#194 ===================================== LOG: int MQTTSocket::getNTPtime(int) L#58 Success receiving time from ntp server. Tick from 1 Jan 1970 is equal to 1505399292. --->TCP Connected --->MQTT Connected --->>>MQTT subscribed to: Nucleo/test Length - 245, Publishing {"state": {"reported": {"temperature": 23.690001, "humidity": 98.190002, "pressure": 982.869141, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.420000, -2.660000, 1.750000], "magnetometer": [-3.600000, -7.100000, 53.300000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.660000, "humidity": 98.010002, "pressure": 982.770264, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.770000, -2.310000, 1.470000], "magnetometer": [-3.100000, -8.300000, 54.200000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.670000, "humidity": 98.129997, "pressure": 982.724121, "accelerometer": [-0.008000, 0.029000, 0.971000], "gyroscope": [0.630000, -2.380000, 1.400000], "magnetometer": [-3.100000, -7.900000, 53.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.690001, "humidity": 98.019997, "pressure": 982.840088, "accelerometer": [-0.009000, 0.030000, 0.972000], "gyroscope": [0.700000, -2.450000, 1.540000], "magnetometer": [-3.700000, -7.900000, 53.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.709999, "humidity": 98.040001, "pressure": 982.828613, "accelerometer": [-0.009000, 0.030000, 0.971000], "gyroscope": [0.630000, -2.520000, 1.470000], "magnetometer": [-2.900000, -7.400000, 52.400000]}}} Length - 245, Publishing {"state": {"reported": {"temperature": 23.719999, "humidity": 97.860001, "pressure": 982.917236, "accelerometer": [-0.026000, 0.103000, 0.891000], "gyroscope": [1.050000, -2.310000, 1.260000], "magnetometer": [-3.300000, -7.100000, 53.500000]}}}
Information
Device connection state might be checked by Green Led on the board. Green light means that device is connected and transferring data to cloud.
- Configure and start your dashboard using instruction and corresponding sources from github
- Use Blue button to set up markers to charts.
- Use AWS IoT console MQTT Client to test device subscription to "Nucleo/test". Just publish any message to this topic and serial port output.
- PROFIT!
X_NUCLEO_IDW01M1v2/SpwfInterface.cpp
- Committer:
- PavelSavyhin
- Date:
- 2017-09-27
- Revision:
- 0:4cdaf9b1e7d0
File content as of revision 0:4cdaf9b1e7d0:
/* mbed Microcontroller Library * Copyright (c) 20015 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** ****************************************************************************** * @file SpwfInterface.cpp * @author STMicroelectronics * @brief Implementation of the NetworkStack for the SPWF Device ****************************************************************************** * @copy * * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2> ****************************************************************************** */ #include "SpwfInterface.h" // Various timeouts for different SPWF operations #define SPWF_CONNECT_TIMEOUT 20000 #define SPWF_SEND_TIMEOUT 500 #define SPWF_RECV_TIMEOUT 500 #define SPWF_MISC_TIMEOUT 15000 /** spwf_socket class * Implementation of SPWF socket structure */ struct spwf_socket { int id; int server_port; nsapi_protocol_t proto; bool connected; }; /** * @brief SpwfSAInterface constructor * @param tx: Pin USART TX * rx: Pin USART RX * rst: reset pin for Spwf module * wkup: reset pin for Spwf module * rts: Pin USART RTS * debug : not used * @retval none */ SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, bool debug) : _spwf(tx, rx, PC_12, PC_8, debug) { memset(_ids, 0, sizeof(_ids)); isInitialized = false; isListening = false; } SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName reset, PinName wakeup, bool debug) : _spwf(tx, rx, reset, wakeup, debug) { memset(_ids, 0, sizeof(_ids)); isInitialized = false; isListening = false; } /** * @brief SpwfSAInterface destructor * @param none * @retval none */ SpwfSAInterface::~SpwfSAInterface() { } /** * @brief init function initializes SPWF FW and module * @param none * @retval error value */ int SpwfSAInterface::init(void) { if(_spwf.startup(0)) { isInitialized=true; return true; } else return NSAPI_ERROR_DEVICE_ERROR; } /** * @brief network connect connects to Access Point * @param ap: Access Point (AP) Name String * pass_phrase: Password String for AP * security: type of NSAPI security supported * @retval NSAPI Error Type */ int SpwfSAInterface::connect(const char *ap, const char *pass_phrase, nsapi_security_t security) { int mode; //initialize the device before connecting if(!isInitialized) { if(!init()) return NSAPI_ERROR_DEVICE_ERROR; } switch(security) { case NSAPI_SECURITY_NONE: mode = 0; pass_phrase = NULL; break; case NSAPI_SECURITY_WEP: mode = 1; break; case NSAPI_SECURITY_WPA: case NSAPI_SECURITY_WPA2: mode = 2; break; default: mode = 2; break; } return (_spwf.connect((char*)ap, (char*)pass_phrase, mode)); } /** * @brief network disconnect disconnects from Access Point * @param none * @retval NSAPI Error Type */ int SpwfSAInterface::disconnect() { return (_spwf.disconnect()); } /** * @brief Get the local IP address * @param none * @retval Null-terminated representation of the local IP address * or null if not yet connected */ const char *SpwfSAInterface::get_ip_address() { return _spwf.getIPAddress(); } /** * @brief Get the MAC address * @param none * @retval Null-terminated representation of the MAC address * or null if not yet connected */ const char *SpwfSAInterface::get_mac_address() { return _spwf.getMACAddress(); } /** * @brief open a socket handle * @param handle: Pointer to handle * proto: TCP/UDP protocol * @retval NSAPI Error Type */ int SpwfSAInterface::socket_open(void **handle, nsapi_protocol_t proto) { int id = -1; struct spwf_socket *socket = new struct spwf_socket; if (!socket) { return NSAPI_ERROR_NO_SOCKET; } socket->id = id; socket->server_port = id; socket->proto = proto; socket->connected = false; *handle = socket; return 0; } /** * @brief connect to a remote socket * @param handle: Pointer to socket handle * addr: Address to connect to * @retval NSAPI Error Type */ int SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr) { int sock_id = 99; struct spwf_socket *socket = (struct spwf_socket *)handle; const char *proto; switch (socket->proto) { case NSAPI_UDP: proto = "u"; break; case NSAPI_TCP: proto = "t"; break; case NSAPI_TLS: proto = "s"; break; default: return NSAPI_ERROR_UNSUPPORTED; break;// defensive programming } if (!_spwf.open(proto, &sock_id, addr.get_ip_address(), addr.get_port())) {;//sock ID is allocated NOW return NSAPI_ERROR_DEVICE_ERROR; } //TODO: Maintain a socket table to map socket ID to host & port //TODO: lookup on client table to see if already socket is allocated to same host/port //multimap <char *, vector <uint16_t> > ::iterator i = c_table.find((char*)ip); if(sock_id <= SPWFSA_SOCKET_COUNT) { socket->id = sock_id;//the socket ID of this Socket instance _ids[socket->id] = true; socket->connected = true; } else return NSAPI_ERROR_NO_SOCKET; return 0; } /** * @brief bind to a port number and address * @param handle: Pointer to socket handle * proto: address to bind to * @retval NSAPI Error Type */ int SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address) { struct spwf_socket *socket = (struct spwf_socket *)handle; socket->server_port = address.get_port(); return 0; } /** * @brief start listening on a port and address * @param handle: Pointer to handle * backlog: not used (always value is 1) * @retval NSAPI Error Type */ int SpwfSAInterface::socket_listen(void *handle, int backlog) { return NSAPI_ERROR_UNSUPPORTED; } /** * @brief accept connections from remote sockets * @param handle: Pointer to handle of client socket (connecting) * proto: handle of server socket which will accept connections * @retval NSAPI Error Type */ int SpwfSAInterface::socket_accept(void **handle, void *server) { return NSAPI_ERROR_UNSUPPORTED; } /** * @brief close a socket * @param handle: Pointer to handle * @retval NSAPI Error Type */ int SpwfSAInterface::socket_close(void *handle) { struct spwf_socket *socket = (struct spwf_socket *)handle; int err = 0; if(socket->id!=-1) { if (_spwf.close(socket->id)) { if(socket->id==SERVER_SOCKET_NO) isListening = false; else _ids[socket->id] = false; } else err = NSAPI_ERROR_DEVICE_ERROR; } delete socket; return err; } /** * @brief write to a socket * @param handle: Pointer to handle * data: pointer to data * size: size of data * @retval no of bytes sent */ int SpwfSAInterface::socket_send(void *handle, const void *data, unsigned size) { struct spwf_socket *socket = (struct spwf_socket *)handle; //int err; /*if(socket->id==SERVER_SOCKET_NO) { if(socket->server_port==-1 || !isListening) return NSAPI_ERROR_NO_SOCKET; //server socket not bound or not listening err = _spwf.socket_server_write((uint16_t)size, (char*)data); } else { err = _spwf.send(socket->id, (char*)data, (uint32_t)size); }*/ if (!_spwf.send(socket->id, (char*)data, (uint32_t)size)) { return NSAPI_ERROR_DEVICE_ERROR; } return size; } /** * @brief receive data on a socket * @param handle: Pointer to handle * data: pointer to data * size: size of data * @retval no of bytes read */ int SpwfSAInterface::socket_recv(void *handle, void *data, unsigned size) { struct spwf_socket *socket = (struct spwf_socket *)handle; int32_t recv; //CHECK:Receive for both Client and Server Sockets same? recv = _spwf.recv(socket->id, (char*)data, (uint32_t)size); if (recv < 0) { //wait_ms(1);//delay of 1ms <for F4>?? //printf("."); if (recv == -1) return NSAPI_ERROR_WOULD_BLOCK;//send this if we want to block call (else timeout will happen) else if (recv == -2)return NSAPI_ERROR_DEVICE_ERROR; else if (recv == -3)return NSAPI_ERROR_NO_CONNECTION; } return recv; } /** * @brief send data to a udp socket * @param handle: Pointer to handle * addr: address of udp socket * data: pointer to data * size: size of data * @retval no of bytes sent */ int SpwfSAInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) { struct spwf_socket *socket = (struct spwf_socket *)handle; if (!socket->connected) { int err = socket_connect(socket, addr); if (err < 0) { return err; } } return socket_send(socket, data, size); } /** * @brief receive data on a udp socket * @param handle: Pointer to handle * addr: address of udp socket * data: pointer to data * size: size of data * @retval no of bytes read */ int SpwfSAInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) { struct spwf_socket *socket = (struct spwf_socket *)handle; return socket_recv(socket, data, size); } /** * @brief attach function/callback to the socket * Not used * @param handle: Pointer to handle * callback: callback function pointer * data: pointer to data * @retval none */ void SpwfSAInterface::socket_attach(void *handle, void (*callback)(void *), void *data) { //No implementation yet } /** * @brief utility debug function for printing to serial terminal * @param string: Pointer to data * @retval none */ void SpwfSAInterface::debug(const char * string) { //_spwf.debug_print(string); } /** * @brief Set the socket options * Not used * @param handle: Pointer to handle * level: SOL_SOCKET * optname: option name * optval: pointer to option value * optlen: option length @retval NSAPI Error Type */ int SpwfSAInterface::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) { // struct spwf_socket *socket = (struct spwf_socket *)handle; switch (optname) { case NSAPI_REUSEADDR: /*!< Allow bind to reuse local addresses */ case NSAPI_KEEPALIVE: /*!< Enables sending of keepalive messages */ case NSAPI_LINGER: /*!< Keeps close from returning until queues empty */ case NSAPI_SNDBUF: /*!< Sets send buffer size */ case NSAPI_RCVBUF: /*!< Sets recv buffer size */ default: printf("SpwfSAInterface::setsockopt> ERROR!!!! Unknown optname: %d \r\n", optname); return -1; } return NSAPI_ERROR_UNSUPPORTED;// defensive programming } /** * @brief Get the socket options * Not used * @param handle: Pointer to handle * level: SOL_SOCKET * optname: option name * optval: pointer to option value * optlen: pointer to option length @retval NSAPI Error Type */ int SpwfSAInterface::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) { return NSAPI_ERROR_UNSUPPORTED; }