An example program using NucleoL476RG with X-Nucleo-IDW01M1 wifi shield to sends analog data to Ubidots IoT Provider
Dependencies: NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of TLS_HelloWorld_IDW01M1 by
Complete tutorial at https://www.hackster.io/rreicher/nucleol476rg-to-ubidots-00d173
main.cpp@13:12401835892c, 2017-10-06 (annotated)
- Committer:
- rrom
- Date:
- Fri Oct 06 13:03:51 2017 +0000
- Revision:
- 13:12401835892c
- Parent:
- 12:7b6b23225fd0
Nucleo with IDW01M1 wifi shield used to send analog data to Ubidots. Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rrom | 13:12401835892c | 1 | /* |
rrom | 13:12401835892c | 2 | Copyright (C) 2017 romain reicher |
mridup | 0:dee849b0e6e6 | 3 | |
rrom | 13:12401835892c | 4 | This program is free software: you can redistribute it and/or modify |
rrom | 13:12401835892c | 5 | it under the terms of the GNU General Public License as published by |
rrom | 13:12401835892c | 6 | the Free Software Foundation, either version 3 of the License, or |
rrom | 13:12401835892c | 7 | (at your option) any later version. |
rrom | 13:12401835892c | 8 | |
rrom | 13:12401835892c | 9 | This program is distributed in the hope that it will be useful, |
rrom | 13:12401835892c | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
rrom | 13:12401835892c | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
rrom | 13:12401835892c | 12 | GNU General Public License for more details. |
mridup | 0:dee849b0e6e6 | 13 | |
rrom | 13:12401835892c | 14 | You should have received a copy of the GNU General Public License |
rrom | 13:12401835892c | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
rrom | 13:12401835892c | 16 | |
rrom | 13:12401835892c | 17 | About |
rrom | 13:12401835892c | 18 | --------------------------------------------------------------------- |
rrom | 13:12401835892c | 19 | |
rrom | 13:12401835892c | 20 | Send sensors value to Ubidots. |
rrom | 13:12401835892c | 21 | This example sends 3 variables to Ubidots. |
rrom | 13:12401835892c | 22 | - STM32L476 VBAT/3 internal channel in mV |
rrom | 13:12401835892c | 23 | - STM32L476 Internal Temperature Sensor in C |
rrom | 13:12401835892c | 24 | - The status of onboard User Button (blue) on NucleoL476RG |
rrom | 13:12401835892c | 25 | Use NucleoL476RG with X-Nucleo-IDW01M1v2 wifi shield |
rrom | 13:12401835892c | 26 | |
rrom | 13:12401835892c | 27 | Important note: Some IDW01M1 wifi shield had resistor R21 mounted |
rrom | 13:12401835892c | 28 | which interfere with STLink/SWD programmer. |
rrom | 13:12401835892c | 29 | Please unmount R21 to fix it. |
rrom | 13:12401835892c | 30 | |
rrom | 13:12401835892c | 31 | romain reicher |
rrom | 13:12401835892c | 32 | Date : 20/09/2017 |
rrom | 13:12401835892c | 33 | Revision : v0.1 |
rrom | 13:12401835892c | 34 | */ |
mapellil | 8:74b827befe72 | 35 | |
mapellil | 10:cc61a766cd1f | 36 | |
rrom | 13:12401835892c | 37 | #include "mbed.h" |
rrom | 13:12401835892c | 38 | #include "SpwfInterface.h" |
rrom | 13:12401835892c | 39 | #include "TCPSocket.h" |
mridup | 0:dee849b0e6e6 | 40 | |
rrom | 13:12401835892c | 41 | /* Wifi Acces Point Settings */ |
rrom | 13:12401835892c | 42 | #define AP_SSID "YOUR_WIFI_SSID" |
rrom | 13:12401835892c | 43 | #define AP_PASSWORD "YOUR_WIFI_PASSWORD" |
rrom | 13:12401835892c | 44 | #define UBIDOTS_SERVER "things.ubidots.com" |
rrom | 13:12401835892c | 45 | #define UBIDOTS_PORT 80 |
rrom | 13:12401835892c | 46 | #define UBIDOTS_TOKEN "YOUR_UBIDOTS_TOKEN" |
rrom | 13:12401835892c | 47 | #define UBIDOTS_DEVICE "YOUR_UBIDOTS_LABEL_DEVICE" |
mridup | 1:daf71fa3674c | 48 | |
rrom | 13:12401835892c | 49 | /* Communication ressources */ |
rrom | 13:12401835892c | 50 | SpwfSAInterface spwf(D8, D2, false); |
rrom | 13:12401835892c | 51 | Serial pc(USBTX, USBRX); |
rrom | 13:12401835892c | 52 | |
rrom | 13:12401835892c | 53 | /* Digital ressources */ |
rrom | 13:12401835892c | 54 | DigitalOut myLed(LED1); |
rrom | 13:12401835892c | 55 | DigitalIn myButton(USER_BUTTON); |
rrom | 13:12401835892c | 56 | |
rrom | 13:12401835892c | 57 | /* Analog ressources */ |
rrom | 13:12401835892c | 58 | AnalogIn adc_vbat(ADC_VBAT); // VBAT / 3 internal to ADC channel |
rrom | 13:12401835892c | 59 | AnalogIn adc_temp(ADC_TEMP); // Internal Temp Sensor to ADC Channel |
rrom | 13:12401835892c | 60 | |
rrom | 13:12401835892c | 61 | /* Global variables */ |
rrom | 13:12401835892c | 62 | float temp = adc_temp.read() * 100; // Converted in C |
rrom | 13:12401835892c | 63 | float batt = adc_vbat.read() * 30000; // Converted in mV |
rrom | 13:12401835892c | 64 | bool level = false; |
mridup | 1:daf71fa3674c | 65 | |
rrom | 13:12401835892c | 66 | /** |
rrom | 13:12401835892c | 67 | * @brief Main program |
rrom | 13:12401835892c | 68 | * @param None |
rrom | 13:12401835892c | 69 | * @retval None |
rrom | 13:12401835892c | 70 | */ |
rrom | 13:12401835892c | 71 | int main() |
rrom | 13:12401835892c | 72 | { |
rrom | 13:12401835892c | 73 | /* Configure Serial baud rate */ |
rrom | 13:12401835892c | 74 | pc.baud(115200); |
rrom | 13:12401835892c | 75 | |
rrom | 13:12401835892c | 76 | /* Update level variable state depending USER_BUTTON state */ |
rrom | 13:12401835892c | 77 | if (myButton == 0) |
rrom | 13:12401835892c | 78 | level = true; |
rrom | 13:12401835892c | 79 | else |
rrom | 13:12401835892c | 80 | level = false; |
rrom | 13:12401835892c | 81 | |
rrom | 13:12401835892c | 82 | TCPSocket socket(&spwf); |
rrom | 13:12401835892c | 83 | char sendBuffer[256]; |
rrom | 13:12401835892c | 84 | char message[64]; |
rrom | 13:12401835892c | 85 | int err; |
rrom | 13:12401835892c | 86 | |
rrom | 13:12401835892c | 87 | /* ######################## WIFI CONNECTION ######################## */ |
mridup | 1:daf71fa3674c | 88 | |
rrom | 13:12401835892c | 89 | pc.printf("IDW01M1 NetworkSocketAPI TCP Client Ubidots\r\n"); |
rrom | 13:12401835892c | 90 | pc.printf("Connecting to AP\r\n"); |
rrom | 13:12401835892c | 91 | |
rrom | 13:12401835892c | 92 | //* Connect to wifi acces point */ |
rrom | 13:12401835892c | 93 | if(spwf.connect(AP_SSID, AP_PASSWORD, NSAPI_SECURITY_WPA2)) |
rrom | 13:12401835892c | 94 | { |
rrom | 13:12401835892c | 95 | pc.printf("Now connected\r\n"); |
rrom | 13:12401835892c | 96 | } |
rrom | 13:12401835892c | 97 | else |
rrom | 13:12401835892c | 98 | { |
rrom | 13:12401835892c | 99 | pc.printf("Error connecting to AP.\r\n"); |
mridup | 0:dee849b0e6e6 | 100 | return -1; |
mridup | 0:dee849b0e6e6 | 101 | } |
rrom | 13:12401835892c | 102 | |
rrom | 13:12401835892c | 103 | /* #################### GET CONNECTION INFOS ######################## */ |
rrom | 13:12401835892c | 104 | |
rrom | 13:12401835892c | 105 | /* Get and print network connection parameters ip and mac adress */ |
rrom | 13:12401835892c | 106 | const char *ip = spwf.get_ip_address(); |
rrom | 13:12401835892c | 107 | const char *mac = spwf.get_mac_address(); |
mapellil | 8:74b827befe72 | 108 | |
rrom | 13:12401835892c | 109 | pc.printf("IP address is: %s\r\n", ip ? ip : "No IP"); |
rrom | 13:12401835892c | 110 | pc.printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); |
rrom | 13:12401835892c | 111 | |
rrom | 13:12401835892c | 112 | /* ##################### UBIDOATS SEND DATA ######################### */ |
rrom | 13:12401835892c | 113 | |
rrom | 13:12401835892c | 114 | printf("Sending HTTP Data to Ubidots...\r\n"); |
rrom | 13:12401835892c | 115 | |
rrom | 13:12401835892c | 116 | /* Open a socket , create a TCP connection to Ubidots */ |
rrom | 13:12401835892c | 117 | err = socket.connect(UBIDOTS_SERVER, UBIDOTS_PORT); |
rrom | 13:12401835892c | 118 | if (err!=0) |
rrom | 13:12401835892c | 119 | { |
rrom | 13:12401835892c | 120 | pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); |
rrom | 13:12401835892c | 121 | return -1; |
rrom | 13:12401835892c | 122 | } |
rrom | 13:12401835892c | 123 | else |
rrom | 13:12401835892c | 124 | pc.printf("\r\nconnected to host server\r\n"); |
rrom | 13:12401835892c | 125 | |
rrom | 13:12401835892c | 126 | /* Construct content of HTTP command */ |
rrom | 13:12401835892c | 127 | sprintf(message, "{\"temperature\": %0.2f, \"battery\": %0.2f, \"level\": %d}", temp, batt, (int)level); |
rrom | 13:12401835892c | 128 | printf("Content Length = %d\r\n", (int)strlen(message)); |
rrom | 13:12401835892c | 129 | |
rrom | 13:12401835892c | 130 | /* Construct HTTP command to send */ |
rrom | 13:12401835892c | 131 | sprintf(sendBuffer, "POST /api/v1.6/devices/%s/?token=%s HTTP/1.1\r\nHost: things.ubidots.com\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", UBIDOTS_DEVICE, UBIDOTS_TOKEN, (int)strlen(message),message); |
rrom | 13:12401835892c | 132 | pc.printf("HTTP command %s\r\n", sendBuffer); |
rrom | 13:12401835892c | 133 | wait(2.0); |
rrom | 13:12401835892c | 134 | |
rrom | 13:12401835892c | 135 | /* Send http request to Ubidots */ |
rrom | 13:12401835892c | 136 | int scount = socket.send(sendBuffer, (int)strlen(sendBuffer)); |
rrom | 13:12401835892c | 137 | printf("sent %d [%.*s]\r\n", scount, strstr(sendBuffer, "\r\n") - sendBuffer, sendBuffer); |
rrom | 13:12401835892c | 138 | |
rrom | 13:12401835892c | 139 | /* Receive a simple http response and print out the response line */ |
rrom | 13:12401835892c | 140 | char respBuffer[64]; |
rrom | 13:12401835892c | 141 | int rcount = socket.recv(respBuffer, sizeof respBuffer); |
rrom | 13:12401835892c | 142 | printf("recv %d [%.*s]\r\n", rcount, strstr(respBuffer, "\r\n") - respBuffer, respBuffer); |
rrom | 13:12401835892c | 143 | |
rrom | 13:12401835892c | 144 | /* Close the socket to return its memory and bring down the network interface */ |
rrom | 13:12401835892c | 145 | pc.printf("Close Socket\r\n"); |
rrom | 13:12401835892c | 146 | socket.close(); |
rrom | 13:12401835892c | 147 | |
rrom | 13:12401835892c | 148 | /* Disconnect */ |
rrom | 13:12401835892c | 149 | pc.printf("Disconnect Wifi\r\n"); |
mridup | 2:3a87dbea07a7 | 150 | spwf.disconnect(); |
rrom | 13:12401835892c | 151 | wait(1.0); |
rrom | 13:12401835892c | 152 | pc.printf("Done\r\n"); |
mapellil | 8:74b827befe72 | 153 | |
rrom | 13:12401835892c | 154 | myLed = 0; |
rrom | 13:12401835892c | 155 | |
rrom | 13:12401835892c | 156 | while(1) |
rrom | 13:12401835892c | 157 | { |
rrom | 13:12401835892c | 158 | myLed = !myLed; |
rrom | 13:12401835892c | 159 | wait(1.0); |
mridup | 0:dee849b0e6e6 | 160 | } |
mridup | 0:dee849b0e6e6 | 161 | } |