Thingspeak IoT using Nucleo-F411RE and IDW01m1
Dependencies: mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI
main.cpp@9:13bd9c989e6c, 2017-01-10 (annotated)
- Committer:
- mapellil
- Date:
- Tue Jan 10 09:20:31 2017 +0000
- Revision:
- 9:13bd9c989e6c
- Parent:
- 8:74b827befe72
- Child:
- 10:cc61a766cd1f
Added set time on Wifi module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:dee849b0e6e6 | 1 | /* SpwfInterface NetworkSocketAPI Example Program |
mridup | 0:dee849b0e6e6 | 2 | * Copyright (c) 2015 ARM Limited |
mridup | 0:dee849b0e6e6 | 3 | * |
mridup | 0:dee849b0e6e6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mridup | 0:dee849b0e6e6 | 5 | * you may not use this file except in compliance with the License. |
mridup | 0:dee849b0e6e6 | 6 | * You may obtain a copy of the License at |
mridup | 0:dee849b0e6e6 | 7 | * |
mridup | 0:dee849b0e6e6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mridup | 0:dee849b0e6e6 | 9 | * |
mridup | 0:dee849b0e6e6 | 10 | * Unless required by applicable law or agreed to in writing, software |
mridup | 0:dee849b0e6e6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mridup | 0:dee849b0e6e6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mridup | 0:dee849b0e6e6 | 13 | * See the License for the specific language governing permissions and |
mridup | 0:dee849b0e6e6 | 14 | * limitations under the License. |
mridup | 0:dee849b0e6e6 | 15 | */ |
mridup | 0:dee849b0e6e6 | 16 | |
mridup | 0:dee849b0e6e6 | 17 | #include "mbed.h" |
mridup | 0:dee849b0e6e6 | 18 | #include "SpwfInterface.h" |
mridup | 0:dee849b0e6e6 | 19 | #include "TCPSocket.h" |
mridup | 0:dee849b0e6e6 | 20 | |
mapellil | 8:74b827befe72 | 21 | |
mapellil | 8:74b827befe72 | 22 | //www.amazon.it CA certificate in PEM format |
mapellil | 8:74b827befe72 | 23 | char CA_cert []="-----BEGIN CERTIFICATE-----\r\n" |
mapellil | 8:74b827befe72 | 24 | "MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB\r\n" |
mapellil | 8:74b827befe72 | 25 | "yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL\r\n" |
mapellil | 8:74b827befe72 | 26 | "ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp\r\n" |
mapellil | 8:74b827befe72 | 27 | "U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW\r\n" |
mapellil | 8:74b827befe72 | 28 | "ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0\r\n" |
mapellil | 8:74b827befe72 | 29 | "aG9yaXR5IC0gRzUwHhcNMDYxMTA4MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCByjEL\r\n" |
mapellil | 8:74b827befe72 | 30 | "MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZW\r\n" |
mapellil | 8:74b827befe72 | 31 | "ZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJpU2ln\r\n" |
mapellil | 8:74b827befe72 | 32 | "biwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxWZXJp\r\n" |
mapellil | 8:74b827befe72 | 33 | "U2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9y\r\n" |
mapellil | 8:74b827befe72 | 34 | "aXR5IC0gRzUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCvJAgIKXo1\r\n" |
mapellil | 8:74b827befe72 | 35 | "nmAMqudLO07cfLw8RRy7K+D+KQL5VwijZIUVJ/XxrcgxiV0i6CqqpkKzj/i5Vbex\r\n" |
mapellil | 8:74b827befe72 | 36 | "t0uz/o9+B1fs70PbZmIVYc9gDaTY3vjgw2IIPVQT60nKWVSFJuUrjxuf6/WhkcIz\r\n" |
mapellil | 8:74b827befe72 | 37 | "SdhDY2pSS9KP6HBRTdGJaXvHcPaz3BJ023tdS1bTlr8Vd6Gw9KIl8q8ckmcY5fQG\r\n" |
mapellil | 8:74b827befe72 | 38 | "BO+QueQA5N06tRn/Arr0PO7gi+s3i+z016zy9vA9r911kTMZHRxAy3QkGSGT2RT+\r\n" |
mapellil | 8:74b827befe72 | 39 | "rCpSx4/VBEnkjWNHiDxpg8v+R70rfk/Fla4OndTRQ8Bnc+MUCH7lP59zuDMKz10/\r\n" |
mapellil | 8:74b827befe72 | 40 | "NIeWiu5T6CUVAgMBAAGjgbIwga8wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8E\r\n" |
mapellil | 8:74b827befe72 | 41 | "BAMCAQYwbQYIKwYBBQUHAQwEYTBfoV2gWzBZMFcwVRYJaW1hZ2UvZ2lmMCEwHzAH\r\n" |
mapellil | 8:74b827befe72 | 42 | "BgUrDgMCGgQUj+XTGoasjY5rw8+AatRIGCx7GS4wJRYjaHR0cDovL2xvZ28udmVy\r\n" |
mapellil | 8:74b827befe72 | 43 | "aXNpZ24uY29tL3ZzbG9nby5naWYwHQYDVR0OBBYEFH/TZafC3ey78DAJ80M5+gKv\r\n" |
mapellil | 8:74b827befe72 | 44 | "MzEzMA0GCSqGSIb3DQEBBQUAA4IBAQCTJEowX2LP2BqYLz3q3JktvXf2pXkiOOzE\r\n" |
mapellil | 8:74b827befe72 | 45 | "p6B4Eq1iDkVwZMXnl2YtmAl+X6/WzChl8gGqCBpH3vn5fJJaCGkgDdk+bW48DW7Y\r\n" |
mapellil | 8:74b827befe72 | 46 | "5gaRQBi5+MHt39tBquCWIMnNZBU4gcmU7qKEKQsTb47bDN0lAtukixlE0kF6BWlK\r\n" |
mapellil | 8:74b827befe72 | 47 | "WE9gyn6CagsCqiUXObXbf+eEZSqVir2G3l6BFoMtEMze/aiCKm0oHw0LxOXnGiYZ\r\n" |
mapellil | 8:74b827befe72 | 48 | "4fQRbxC1lfznQgUy286dUV4otp6F01vvpX1FQHKOtw5rDgb7MzVIcbidJ4vEZV8N\r\n" |
mapellil | 8:74b827befe72 | 49 | "hnacRHr2lVz2XTIIM6RUthg/aFzyQkqFOFSDX9HoLPKsEdao7WNq\r\n" |
mapellil | 8:74b827befe72 | 50 | "-----END CERTIFICATE-----\r\n"; |
mapellil | 8:74b827befe72 | 51 | |
mridup | 0:dee849b0e6e6 | 52 | //------------------------------------ |
mridup | 0:dee849b0e6e6 | 53 | // Hyperterminal configuration |
mridup | 0:dee849b0e6e6 | 54 | // 9600 bauds, 8-bit data, no parity |
mridup | 0:dee849b0e6e6 | 55 | //------------------------------------ |
mridup | 0:dee849b0e6e6 | 56 | |
mridup | 2:3a87dbea07a7 | 57 | Serial pc(USBTX, USBRX); |
mridup | 0:dee849b0e6e6 | 58 | DigitalOut myled(LED1); |
mridup | 1:daf71fa3674c | 59 | |
mridup | 1:daf71fa3674c | 60 | /************************************* |
mridup | 1:daf71fa3674c | 61 | //FRDM-K64: D9->UART1_TX, D7->UART1_RX |
mridup | 1:daf71fa3674c | 62 | Pin connections: |
mridup | 1:daf71fa3674c | 63 | FRDM IDW01M1 |
mridup | 1:daf71fa3674c | 64 | ------ --------- |
mridup | 1:daf71fa3674c | 65 | +3v3 <--> +3v3 |
mridup | 1:daf71fa3674c | 66 | GND <--> GND |
mridup | 1:daf71fa3674c | 67 | D9 <--> D8 |
mridup | 1:daf71fa3674c | 68 | D7 <--> D2 |
mridup | 1:daf71fa3674c | 69 | |
mridup | 1:daf71fa3674c | 70 | SpwfSAInterface spwf(D9, D7, false); |
mridup | 1:daf71fa3674c | 71 | *************************************/ |
mridup | 2:3a87dbea07a7 | 72 | /************************************* |
mridup | 2:3a87dbea07a7 | 73 | //LPCXpresso11U68: D9->UART1_TX, D7->UART1_RX |
mridup | 2:3a87dbea07a7 | 74 | Pin connections: |
mridup | 2:3a87dbea07a7 | 75 | LPC IDW01M1 |
mridup | 2:3a87dbea07a7 | 76 | ------ --------- |
mridup | 2:3a87dbea07a7 | 77 | +3v3 <--> +3v3 |
mridup | 2:3a87dbea07a7 | 78 | GND <--> GND |
mridup | 2:3a87dbea07a7 | 79 | A1 <--> D8 |
mridup | 2:3a87dbea07a7 | 80 | A2 <--> D2 |
mridup | 2:3a87dbea07a7 | 81 | |
mridup | 2:3a87dbea07a7 | 82 | SpwfSAInterface spwf(A1, A2, false); |
mridup | 2:3a87dbea07a7 | 83 | *************************************/ |
mridup | 1:daf71fa3674c | 84 | |
mridup | 1:daf71fa3674c | 85 | //NUCLEO: D8->UART1_TX (PA_9), D2->UART1_RX (PA_10) |
mridup | 1:daf71fa3674c | 86 | SpwfSAInterface spwf(D8, D2, false); |
mridup | 2:3a87dbea07a7 | 87 | |
mridup | 0:dee849b0e6e6 | 88 | int main() { |
mridup | 0:dee849b0e6e6 | 89 | int err; |
nikapov | 7:92b5480bb3e4 | 90 | char * ssid = "STM"; |
nikapov | 7:92b5480bb3e4 | 91 | char * seckey = "STMDemo"; |
mridup | 0:dee849b0e6e6 | 92 | |
mridup | 0:dee849b0e6e6 | 93 | pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); |
mridup | 0:dee849b0e6e6 | 94 | pc.printf("\r\nconnecting to AP\r\n"); |
mridup | 0:dee849b0e6e6 | 95 | |
mridup | 0:dee849b0e6e6 | 96 | if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) { |
mridup | 0:dee849b0e6e6 | 97 | pc.printf("\r\nnow connected\r\n"); |
mridup | 0:dee849b0e6e6 | 98 | } else { |
mridup | 0:dee849b0e6e6 | 99 | pc.printf("\r\nerror connecting to AP.\r\n"); |
mridup | 0:dee849b0e6e6 | 100 | return -1; |
mridup | 0:dee849b0e6e6 | 101 | } |
mapellil | 8:74b827befe72 | 102 | |
mridup | 0:dee849b0e6e6 | 103 | const char *ip = spwf.get_ip_address(); |
mridup | 0:dee849b0e6e6 | 104 | const char *mac = spwf.get_mac_address(); |
mridup | 0:dee849b0e6e6 | 105 | |
mridup | 0:dee849b0e6e6 | 106 | pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP"); |
mridup | 0:dee849b0e6e6 | 107 | pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC"); |
mridup | 0:dee849b0e6e6 | 108 | |
mridup | 0:dee849b0e6e6 | 109 | SocketAddress addr(&spwf, "st.com"); |
mridup | 0:dee849b0e6e6 | 110 | pc.printf("\r\nst.com resolved to: %s\r\n", addr.get_ip_address()); |
mridup | 0:dee849b0e6e6 | 111 | |
mridup | 4:40cb51547b9f | 112 | pc.printf("\r\nconnecting to http://4.ifcfg.me\r\n"); |
mridup | 0:dee849b0e6e6 | 113 | |
mridup | 0:dee849b0e6e6 | 114 | TCPSocket socket(&spwf); |
mridup | 2:3a87dbea07a7 | 115 | err = socket.connect("4.ifcfg.me", 23); |
mridup | 0:dee849b0e6e6 | 116 | if(err!=0) |
mridup | 0:dee849b0e6e6 | 117 | { |
mridup | 0:dee849b0e6e6 | 118 | pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); |
mridup | 0:dee849b0e6e6 | 119 | return -1; |
mridup | 0:dee849b0e6e6 | 120 | } else pc.printf("\r\nconnected to host server\r\n"); |
mridup | 0:dee849b0e6e6 | 121 | |
mapellil | 8:74b827befe72 | 122 | char buffer[100]; |
mridup | 0:dee849b0e6e6 | 123 | int count = 0; |
mridup | 0:dee849b0e6e6 | 124 | pc.printf("\r\nReceiving Data\r\n"); |
mridup | 0:dee849b0e6e6 | 125 | count = socket.recv(buffer, sizeof buffer); |
mridup | 0:dee849b0e6e6 | 126 | |
mridup | 0:dee849b0e6e6 | 127 | if(count > 0) |
mridup | 0:dee849b0e6e6 | 128 | { |
mapellil | 8:74b827befe72 | 129 | buffer [count]='\0'; |
mapellil | 8:74b827befe72 | 130 | printf("%s\r\n", buffer); |
mridup | 0:dee849b0e6e6 | 131 | } |
mridup | 0:dee849b0e6e6 | 132 | else pc.printf("\r\nData not received\r\n"); |
mridup | 0:dee849b0e6e6 | 133 | |
mridup | 2:3a87dbea07a7 | 134 | pc.printf("\r\nClosing Socket\r\n"); |
mridup | 2:3a87dbea07a7 | 135 | socket.close(); |
mapellil | 8:74b827befe72 | 136 | pc.printf("\r\nUnsecure Socket Test complete.\r\n"); |
mapellil | 8:74b827befe72 | 137 | |
mapellil | 8:74b827befe72 | 138 | // Start Secure Socket connection test (one way server autentication only) |
mapellil | 8:74b827befe72 | 139 | time_t ctTime; |
mapellil | 8:74b827befe72 | 140 | ctTime = time(NULL); |
mapellil | 8:74b827befe72 | 141 | printf ("Start Secure Socket connection with one way server autentication test\n\r"); |
mapellil | 8:74b827befe72 | 142 | printf("Initial System Time is: %s\r\n", ctime(&ctTime)); |
mapellil | 8:74b827befe72 | 143 | printf("Need to adjust time? if yes enter time in seconds elapsed since Epoch (cmd: date +'%%s'), otherwise enter 0 "); |
mapellil | 8:74b827befe72 | 144 | int t=0; |
mapellil | 8:74b827befe72 | 145 | scanf("%d",&t); |
mapellil | 8:74b827befe72 | 146 | printf ("entered time is: %d \n\r", t); |
mapellil | 9:13bd9c989e6c | 147 | if (t != 0) { time_t txTm = t; set_time(txTm); } // set Nucleo system time |
mapellil | 9:13bd9c989e6c | 148 | ctTime = time(NULL); |
mapellil | 9:13bd9c989e6c | 149 | printf ("The current system time is: %s", ctime (&ctTime)); // set WiFi module systm time |
mapellil | 9:13bd9c989e6c | 150 | if (!spwf.set_time(ctTime)) printf ("ERROR set_time\n\r"); |
mapellil | 8:74b827befe72 | 151 | if (!spwf.clean_TLS_certificate(ALL)) printf ("ERROR clean_TLS_certificate\n\r"); |
mapellil | 8:74b827befe72 | 152 | if (!spwf.set_TLS_certificate(CA_cert, sizeof(CA_cert), FLASH_CA_ROOT_CERT)) printf ("ERROR set_TLS_certificate\n\r"); |
mapellil | 8:74b827befe72 | 153 | if (!spwf.set_TLS_SRV_domain("www.amazon.it",FLASH_DOMAIN)) printf ("ERROR set_TLS_CA_domain\n\r"); |
mapellil | 8:74b827befe72 | 154 | // NOTE: DNS requires UDP socket. DNS is invoked while connecting a secure socket to resolve URL to IP, |
mapellil | 8:74b827befe72 | 155 | // so DNS doesnt answer as it requires unsecure UDP socket connection. |
mapellil | 8:74b827befe72 | 156 | // workaroud: convert URL to IP quering DNS before secure socket creation, then connect |
mapellil | 8:74b827befe72 | 157 | // the secure socket created to the retrieved IP. |
mapellil | 8:74b827befe72 | 158 | spwf.gethostbyname(&addr,"www.amazon.it"); // ask DNS |
mapellil | 8:74b827befe72 | 159 | addr.set_port(443); |
mapellil | 8:74b827befe72 | 160 | spwf.set_secure_sockets(); |
mapellil | 8:74b827befe72 | 161 | socket.open(&spwf); |
mapellil | 8:74b827befe72 | 162 | printf ("Connecting to www.amazon.it IP: %s ...\n\r", addr.get_ip_address()); |
mapellil | 8:74b827befe72 | 163 | err = socket.connect(addr); |
mapellil | 8:74b827befe72 | 164 | if (err != 0 )printf ("ERROR secure socket connection failed: %d\n\r", err); |
mapellil | 8:74b827befe72 | 165 | else printf ("--->>> Secure socket CONNECTED to: %s\n\r", addr.get_ip_address()); |
mapellil | 8:74b827befe72 | 166 | spwf.set_unsecure_sockets(); |
mapellil | 8:74b827befe72 | 167 | socket.close(); |
mapellil | 8:74b827befe72 | 168 | printf ("Socket closed\n\r"); |
mridup | 2:3a87dbea07a7 | 169 | spwf.disconnect(); |
mapellil | 8:74b827befe72 | 170 | printf ("WIFI disconnected, exiting ...\n\r"); |
mapellil | 8:74b827befe72 | 171 | |
mridup | 0:dee849b0e6e6 | 172 | while(1) { |
mridup | 0:dee849b0e6e6 | 173 | wait(1); |
mridup | 0:dee849b0e6e6 | 174 | myled = !myled; |
mridup | 0:dee849b0e6e6 | 175 | } |
mridup | 0:dee849b0e6e6 | 176 | } |