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!
main.cpp@0:4cdaf9b1e7d0, 2017-09-27 (annotated)
- Committer:
- PavelSavyhin
- Date:
- Wed Sep 27 14:40:52 2017 +0300
- Revision:
- 0:4cdaf9b1e7d0
- Child:
- 1:042ca9148926
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PavelSavyhin | 0:4cdaf9b1e7d0 | 1 | /* SpwfInterface NetworkSocketAPI Example Program |
PavelSavyhin | 0:4cdaf9b1e7d0 | 2 | * Copyright (c) 2015 ARM Limited |
PavelSavyhin | 0:4cdaf9b1e7d0 | 3 | * Copyright (c) 2017 KLIKA TECH, LLC |
PavelSavyhin | 0:4cdaf9b1e7d0 | 4 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 6 | * you may not use this file except in compliance with the License. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 7 | * You may obtain a copy of the License at |
PavelSavyhin | 0:4cdaf9b1e7d0 | 8 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 10 | * |
PavelSavyhin | 0:4cdaf9b1e7d0 | 11 | * Unless required by applicable law or agreed to in writing, software |
PavelSavyhin | 0:4cdaf9b1e7d0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 14 | * See the License for the specific language governing permissions and |
PavelSavyhin | 0:4cdaf9b1e7d0 | 15 | * limitations under the License. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 16 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 17 | Contributors: |
PavelSavyhin | 0:4cdaf9b1e7d0 | 18 | * Klika Tech - completely adopted to Amazon AWS IoT service |
PavelSavyhin | 0:4cdaf9b1e7d0 | 19 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 20 | */ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 21 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 22 | #include "mbed.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 23 | #include "SpwfInterface.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 24 | #include "TCPSocket.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 25 | #include "MQTTClient.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 26 | #include "MQTTWiFi.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 27 | #include <ctype.h> |
PavelSavyhin | 0:4cdaf9b1e7d0 | 28 | #include "x_nucleo_iks01a1.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 29 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 30 | //------------------------------------ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 31 | // Hyperterminal default configuration |
PavelSavyhin | 0:4cdaf9b1e7d0 | 32 | // 9600 bauds, 8-bit data, no parity |
PavelSavyhin | 0:4cdaf9b1e7d0 | 33 | //------------------------------------ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 34 | Serial pc(SERIAL_TX, SERIAL_RX); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 35 | DigitalOut myled(LED2); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 36 | DigitalOut butled(LED3); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 37 | InterruptIn mybutton(USER_BUTTON); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 38 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 39 | bool myButtonPressed = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 40 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 41 | #define MQTT_MAX_PACKET_SIZE 350 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 42 | #define MQTT_MAX_PAYLOAD_SIZE 300 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 43 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 44 | #define AWS_IOT_MQTT_HOST "xxxxxxxxxx.iot.us-east-1.amazonaws.com" //Use your own host. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 45 | #define AWS_IOT_MQTT_PORT 8883 |
PavelSavyhin | 0:4cdaf9b1e7d0 | 46 | #define AWS_IOT_MQTT_CLIENT_ID "Nucleo" //Should be kept if you are using same device clent. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 47 | #define AWS_IOT_MY_THING_NAME "Nucleo" //Should be kept if you are using same device thing name. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 48 | #define AWS_IOT_MQTT_TOPIC_TEST "Nucleo/test" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 49 | #define AWS_IOT_MQTT_TOPIC_DATA "Nucleo/data" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 50 | #define AWS_IOT_MQTT_TOPIC_SHADOW "$aws/things/Nucleo/shadow/update" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 51 | #define AWS_IOT_ID "" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 52 | #define AWS_IOT_AUTH_TOKEN "" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 53 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 54 | // WiFi network credential |
PavelSavyhin | 0:4cdaf9b1e7d0 | 55 | #define SSID "" // Network must be visible otherwise it can't connect |
PavelSavyhin | 0:4cdaf9b1e7d0 | 56 | #define PASSW "" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 57 | #error "Wifi SSID & password empty" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 58 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 59 | #include "stdint.h" |
PavelSavyhin | 0:4cdaf9b1e7d0 | 60 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 61 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 62 | /********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 63 | *********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 64 | Root CA certificate: Never modify |
PavelSavyhin | 0:4cdaf9b1e7d0 | 65 | *********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 66 | ***********************************************************************************************/ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 67 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 68 | //This root CA can be used. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 69 | const uint8_t rootCA[] = "\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 70 | -----BEGIN CERTIFICATE-----\n\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 71 | MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 72 | yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 73 | ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 74 | U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 75 | ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 76 | aG9yaXR5IC0gRzUwHhcNMDYxMTA4MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCByjEL\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 77 | MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZW\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 78 | ZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJpU2ln\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 79 | biwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxWZXJp\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 80 | U2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9y\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 81 | aXR5IC0gRzUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCvJAgIKXo1\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 82 | nmAMqudLO07cfLw8RRy7K+D+KQL5VwijZIUVJ/XxrcgxiV0i6CqqpkKzj/i5Vbex\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 83 | t0uz/o9+B1fs70PbZmIVYc9gDaTY3vjgw2IIPVQT60nKWVSFJuUrjxuf6/WhkcIz\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 84 | SdhDY2pSS9KP6HBRTdGJaXvHcPaz3BJ023tdS1bTlr8Vd6Gw9KIl8q8ckmcY5fQG\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 85 | BO+QueQA5N06tRn/Arr0PO7gi+s3i+z016zy9vA9r911kTMZHRxAy3QkGSGT2RT+\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 86 | rCpSx4/VBEnkjWNHiDxpg8v+R70rfk/Fla4OndTRQ8Bnc+MUCH7lP59zuDMKz10/\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 87 | NIeWiu5T6CUVAgMBAAGjgbIwga8wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8E\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 88 | BAMCAQYwbQYIKwYBBQUHAQwEYTBfoV2gWzBZMFcwVRYJaW1hZ2UvZ2lmMCEwHzAH\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 89 | BgUrDgMCGgQUj+XTGoasjY5rw8+AatRIGCx7GS4wJRYjaHR0cDovL2xvZ28udmVy\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 90 | aXNpZ24uY29tL3ZzbG9nby5naWYwHQYDVR0OBBYEFH/TZafC3ey78DAJ80M5+gKv\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 91 | MzEzMA0GCSqGSIb3DQEBBQUAA4IBAQCTJEowX2LP2BqYLz3q3JktvXf2pXkiOOzE\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 92 | p6B4Eq1iDkVwZMXnl2YtmAl+X6/WzChl8gGqCBpH3vn5fJJaCGkgDdk+bW48DW7Y\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 93 | 5gaRQBi5+MHt39tBquCWIMnNZBU4gcmU7qKEKQsTb47bDN0lAtukixlE0kF6BWlK\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 94 | WE9gyn6CagsCqiUXObXbf+eEZSqVir2G3l6BFoMtEMze/aiCKm0oHw0LxOXnGiYZ\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 95 | 4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 96 | hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao8WNq\n\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 97 | -----END CERTIFICATE-----\n"; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 98 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 99 | /********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 100 | *********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 101 | Device Identity Certificates: Modify for your AWS IoT Thing |
PavelSavyhin | 0:4cdaf9b1e7d0 | 102 | *********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 103 | ***********************************************************************************************/ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 104 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 105 | /**************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 106 | (somecode)-certificate.pem.crt - Amazon signed PEM sertificate. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 107 | *****************************************/ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 108 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 109 | //This Client cert is example. Use own instead. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 110 | const uint8_t clientCRT[] = "\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 111 | -----BEGIN CERTIFICATE-----\n\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 112 | MIIDBjCCAe6gAwIBAgIUVph856omeIxW3UPioq+UrX1DbwowDQYJKoZIhvcNAQEL\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 113 | BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 114 | SW5jLiBMPVNlYXR0bGUgU1Q9V2FzaGluZ3RvbiBDPVVTMB4XDTE3MDUyNTExNTEy\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 115 | OVoXDEQ5MTIzMEIzNTk1OVowgZUxCzAJBgNVBAETAkJZMQ4wDAYDVQQIDAVNaW5z\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 116 | azEOMAwGA1UEBwwFTWluc2sxFzAVBgNVBAoMDktsaWthLVRlY2ggTExDMRcwFQYD\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 117 | VQQLDA5LbGlrYS1UZWNoIExMQzEMMAoGA1UEAwwDUm5EMSYwJAYJKoZIhvcNAQkB\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 118 | FhdtdmF0YExldUBrbGlrYS10ZWNoLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEH\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 119 | A0IABCJgOQJmoTBJVPfEi9Hm/JVixaxkY5rtlgrYO3hSl633A2hg0P/ue0wXDbF3\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 120 | aQ0X57IRFE4k4FEbr3UXjT/IczKjYDBeMB8GA1UdIwQYMBaAFK3YzTUPlYB2Li75\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 121 | i/z8rEogr1d6MB0GA1UdDgQWBBT18HXBaXFJuER/0SwegnxJ+pyJ6TAMBgNVHRMB\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 122 | Af8EAjAAMA4GA1UdDwEB/wQEAwIHgDANBgkqhkiG9w0BAQsFAAOCAQEAb0Ux1aH5\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 123 | RLxjrfGqXN6rPVqh8QQRS+AyBfzmaQN8HaPZMkX5WxXLvcn0A3uWlwQxPPkcZ4zf\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 124 | 51GHtFFQWB4YZ8dx8mUQ0v/j7onHjCJgZ8iDgwOyKMGtnsDEWCakQw+a6cj+NrMZ\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 125 | tzhjwCzEEP6EPcbXwErI5OOzLuWns2L/JEr2wWNkokgRuS8ewr/SQ9OLWIWa2rFM\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 126 | ahPNTb3y/qBeWdjeJmhI+TOxdqIpsF8roWP25zwo/zkzCHCjXFBrL+0CA4MpxIl9\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 127 | x02i7aAhlJ6ys80lDxdeWeeQJXRKkGknP8mcmKn3iEqqJ5s1dQePj2b5d3ldatya\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 128 | wsxQBqqZXzIWEw==\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 129 | \n\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 130 | -----END CERTIFICATE-----\n"; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 131 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 132 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 133 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 134 | /********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 135 | *********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 136 | Private Key: Modify for your AWS IoT Thing |
PavelSavyhin | 0:4cdaf9b1e7d0 | 137 | *********************************************************************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 138 | ***********************************************************************************************/ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 139 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 140 | /********************************************************************8**************************************** |
PavelSavyhin | 0:4cdaf9b1e7d0 | 141 | nucleo.key.pem - client key generated according to readme. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 142 | **************************************************************************************************************/ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 143 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 144 | //This Client Key is example. Use own instead. |
PavelSavyhin | 0:4cdaf9b1e7d0 | 145 | const uint8_t clientKey[] ="\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 146 | -----BEGIN EC PARAMETERS-----\n\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 147 | BggqhEEOPQMBEw==\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 148 | -----END EC PARAMETERS-----\n\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 149 | -----BEGIN EC PRIVATE KEY-----\n\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 150 | MHcCAQEEIHPRfWSC8/k/BsqDWKuP15dXsI9fGwpkTIsLZe6mIrAEoAoGCCqGSM49\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 151 | AwEHoUQDQEAEImAEAEahMElU9+WL0eb8lWLFrGRjmu2WCtg7eFKXrfcDaGDQ/+57\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 152 | TBcNsXdpDRfnshEETiTgURuvdReNP8hEMg==\ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 153 | -----END EC PRIVATE KEY-----\n"; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 154 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 155 | int connack_rc = 0; // MQTT connack return code |
PavelSavyhin | 0:4cdaf9b1e7d0 | 156 | int connectTimeout = 1000; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 157 | int retryAttempt = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 158 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 159 | PressureSensor *pressure_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 160 | HumiditySensor *humidity_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 161 | TempSensor *temp_sensor1; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 162 | MagneticSensor *magnetic_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 163 | GyroSensor *gyro_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 164 | MotionSensor *accel_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 165 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 166 | MQTT::Message message; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 167 | MQTTString TopicName= { AWS_IOT_MQTT_TOPIC_TEST }; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 168 | MQTT::MessageData MsgData(TopicName, message); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 169 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 170 | void subscribe_cb(MQTT::MessageData & msgMQTT) { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 171 | char msg[MQTT_MAX_PAYLOAD_SIZE]; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 172 | msg[0]='\0'; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 173 | strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 174 | printf ("--->>> subscribe_cb msg: %s\n\r", msg); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 175 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 176 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 177 | int subscribe(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 178 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 179 | char* pubTopic = AWS_IOT_MQTT_TOPIC_TEST; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 180 | return client->subscribe(pubTopic, MQTT::QOS0, subscribe_cb); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 181 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 182 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 183 | int connect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 184 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 185 | SpwfSAInterface& WiFi = ipstack->getWiFi(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 186 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 187 | // Network debug statements |
PavelSavyhin | 0:4cdaf9b1e7d0 | 188 | LOG("=====================================\n\r"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 189 | LOG("Connecting WiFi.\n\r"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 190 | LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address()); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 191 | LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address()); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 192 | LOG("Server Hostname: %s port: %d\n\r", AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 193 | LOG("Client ID: %s\n\r", AWS_IOT_MQTT_CLIENT_ID); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 194 | //LOG("Topic: %s\n\r", AWS_IOT_MQTT_TOPIC_TEST); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 195 | //LOG("Subscription URL: %s\n\r", subscription_url); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 196 | LOG("=====================================\n\r"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 197 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 198 | ipstack->open(&ipstack->getWiFi()); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 199 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 200 | int rc=ipstack->getNTPtime(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 201 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 202 | if (rc != 0) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 203 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 204 | ERROR("Get NTP time error: %d\n", rc); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 205 | return rc; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 206 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 207 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 208 | rc = WiFi.setSocketClientSecurity((uint8_t *)"m", (uint8_t *)rootCA, (uint8_t *)clientCRT, (uint8_t *)clientKey, (uint8_t *)AWS_IOT_MQTT_HOST, ipstack->getTime()); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 209 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 210 | if (rc != 0) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 211 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 212 | ERROR("Set security params error: %d\n", rc); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 213 | return rc; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 214 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 215 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 216 | rc = ipstack->connect(AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, connectTimeout); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 217 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 218 | if (rc != 0) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 219 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 220 | WARN("IP Stack connect returned: %d\n", rc); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 221 | return rc; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 222 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 223 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 224 | printf ("--->TCP Connected\n\r"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 225 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 226 | // MQTT Connect |
PavelSavyhin | 0:4cdaf9b1e7d0 | 227 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 228 | data.MQTTVersion = 4; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 229 | data.struct_version=0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 230 | data.clientID.cstring = AWS_IOT_MQTT_CLIENT_ID; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 231 | //data.username.cstring = "use-token-auth"; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 232 | //data.password.cstring = AWS_IOT_AUTH_TOKEN; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 233 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 234 | if ((rc = client->connect(data)) == 0) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 235 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 236 | printf ("--->MQTT Connected\n\r"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 237 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 238 | if (!subscribe(client, ipstack)) printf ("--->>>MQTT subscribed to: %s\n\r",AWS_IOT_MQTT_TOPIC_TEST); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 239 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 240 | else |
PavelSavyhin | 0:4cdaf9b1e7d0 | 241 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 242 | WARN("MQTT connect returned %d\n", rc); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 243 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 244 | if (rc >= 0) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 245 | connack_rc = rc; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 246 | return rc; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 247 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 248 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 249 | int getConnTimeout(int attemptNumber) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 250 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
PavelSavyhin | 0:4cdaf9b1e7d0 | 251 | // after 20 attempts, retry every 10 minutes |
PavelSavyhin | 0:4cdaf9b1e7d0 | 252 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 253 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 254 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 255 | void attemptConnect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 256 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 257 | while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 258 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 259 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 260 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 261 | printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 262 | return; // don't reattempt to connect if credentials are wrong |
PavelSavyhin | 0:4cdaf9b1e7d0 | 263 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 264 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 265 | int timeout = getConnTimeout(++retryAttempt); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 266 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 267 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 268 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
PavelSavyhin | 0:4cdaf9b1e7d0 | 269 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 270 | // this works - reset the system when the retry count gets to a threshold |
PavelSavyhin | 0:4cdaf9b1e7d0 | 271 | if (retryAttempt == 5) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 272 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 273 | ipstack->getWiFi().reset_chip(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 274 | NVIC_SystemReset(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 275 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 276 | else |
PavelSavyhin | 0:4cdaf9b1e7d0 | 277 | wait(timeout); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 278 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 279 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 280 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 281 | int publish(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 282 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 283 | MQTT::Message message; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 284 | char* pubTopic = AWS_IOT_MQTT_TOPIC_SHADOW; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 285 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 286 | char buf[MQTT_MAX_PAYLOAD_SIZE]; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 287 | float temp, press, hum; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 288 | int32_t magnet[3]; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 289 | int32_t gyro[3]; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 290 | int32_t accel[3]; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 291 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 292 | temp_sensor1->GetTemperature(&temp); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 293 | pressure_sensor->GetPressure(&press); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 294 | humidity_sensor->GetHumidity(&hum); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 295 | magnetic_sensor->Get_M_Axes(magnet); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 296 | gyro_sensor->Get_G_Axes(gyro); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 297 | accel_sensor->Get_X_Axes(accel); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 298 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 299 | /*sprintf(buf, |
PavelSavyhin | 0:4cdaf9b1e7d0 | 300 | "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%0.4f,\"Pressure\":%0.4f,\"Humidity\":%0.4f}}", |
PavelSavyhin | 0:4cdaf9b1e7d0 | 301 | temp, press, hum);*/ |
PavelSavyhin | 0:4cdaf9b1e7d0 | 302 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 303 | if (!myButtonPressed) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 304 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 305 | butled = 1; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 306 | sprintf(buf, "{\"state\": {\"reported\": {\"temperature\": %f, \"humidity\": %f, \"pressure\": %f, \"accelerometer\": [%f, %f, %f], \"gyroscope\": [%f, %f, %f], \"magnetometer\": [%f, %f, %f]}}}", |
PavelSavyhin | 0:4cdaf9b1e7d0 | 307 | temp, hum, press, accel[0]/1000.0, accel[1]/1000.0, accel[2]/1000.0, gyro[0]/1000.0, gyro[1]/1000.0, gyro[2]/1000.0, magnet[0]/10.0, magnet[1]/10.0, magnet[2]/10.0); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 308 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 309 | else |
PavelSavyhin | 0:4cdaf9b1e7d0 | 310 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 311 | myButtonPressed = false; // reset state |
PavelSavyhin | 0:4cdaf9b1e7d0 | 312 | butled = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 313 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 314 | sprintf(buf, "{\"temperature\": %f, \"humidity\": %f, \"pressure\": %f, \"accelerometer\": [%f, %f, %f], \"gyroscope\": [%f, %f, %f], \"magnetometer\": [%f, %f, %f], \"marker\": true}", |
PavelSavyhin | 0:4cdaf9b1e7d0 | 315 | temp, hum, press, accel[0]/1000.0, accel[1]/1000.0, accel[2]/1000.0, gyro[0]/1000.0, gyro[1]/1000.0, gyro[2]/1000.0, magnet[0]/10.0, magnet[1]/10.0, magnet[2]/10.0); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 316 | pubTopic = AWS_IOT_MQTT_TOPIC_DATA; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 317 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 318 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 319 | message.qos = MQTT::QOS0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 320 | message.retained = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 321 | message.dup = false; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 322 | message.payload = (void*)buf; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 323 | message.payloadlen = strlen(buf); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 324 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 325 | printf("Length - %d, Publishing %s\n\r", strlen(buf), buf); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 326 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 327 | return client->publish(pubTopic, message); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 328 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 329 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 330 | void pressed() |
PavelSavyhin | 0:4cdaf9b1e7d0 | 331 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 332 | myButtonPressed = true; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 333 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 334 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 335 | int main() |
PavelSavyhin | 0:4cdaf9b1e7d0 | 336 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 337 | const char * ssid = SSID; // Network must be visible otherwise it can't connect |
PavelSavyhin | 0:4cdaf9b1e7d0 | 338 | const char * seckey = PASSW; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 339 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 340 | pc.baud(115200); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 341 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 342 | SpwfSAInterface spwf(D8, D2, false); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 343 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 344 | myled=0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 345 | DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 346 | i2c->frequency(400000); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 347 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 348 | mybutton.fall(&pressed); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 349 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 350 | X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(i2c); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 351 | pressure_sensor = mems_expansion_board->pt_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 352 | temp_sensor1 = mems_expansion_board->ht_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 353 | humidity_sensor = mems_expansion_board->ht_sensor; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 354 | magnetic_sensor = mems_expansion_board->magnetometer; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 355 | gyro_sensor = mems_expansion_board->GetGyroscope(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 356 | accel_sensor = mems_expansion_board->GetAccelerometer(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 357 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 358 | pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 359 | pc.printf("\r\nconnecting to AP\r\n"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 360 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 361 | MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 362 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 363 | LOG("Connected to WiFI.\r\n"); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 364 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 365 | MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 366 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 367 | attemptConnect(&client, &ipstack); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 368 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 369 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 370 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 371 | while (true) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 372 | wait(1.0); // Permanent failures - don't retry |
PavelSavyhin | 0:4cdaf9b1e7d0 | 373 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 374 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 375 | myled=1; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 376 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 377 | int count = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 378 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 379 | while (true) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 380 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 381 | if (++count == 100) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 382 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 383 | myled = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 384 | // Publish a message every second |
PavelSavyhin | 0:4cdaf9b1e7d0 | 385 | if (publish(&client, &ipstack) != 0) |
PavelSavyhin | 0:4cdaf9b1e7d0 | 386 | { |
PavelSavyhin | 0:4cdaf9b1e7d0 | 387 | myled=0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 388 | ipstack.getWiFi().reset_chip(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 389 | NVIC_SystemReset(); |
PavelSavyhin | 0:4cdaf9b1e7d0 | 390 | attemptConnect(&client, &ipstack); // if we have lost the connection |
PavelSavyhin | 0:4cdaf9b1e7d0 | 391 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 392 | else myled=1; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 393 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 394 | count = 0; |
PavelSavyhin | 0:4cdaf9b1e7d0 | 395 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 396 | |
PavelSavyhin | 0:4cdaf9b1e7d0 | 397 | client.yield(10); // allow the MQTT client to receive messages |
PavelSavyhin | 0:4cdaf9b1e7d0 | 398 | } |
PavelSavyhin | 0:4cdaf9b1e7d0 | 399 | } |