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