Thingspeak IoT using Nucleo-F411RE and IDW01m1
Dependencies: mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI
Diff: main.cpp
- Revision:
- 8:74b827befe72
- Parent:
- 7:92b5480bb3e4
- Child:
- 9:13bd9c989e6c
--- a/main.cpp Mon Oct 17 15:26:16 2016 +0000 +++ b/main.cpp Thu Jan 05 09:39:03 2017 +0000 @@ -18,6 +18,37 @@ #include "SpwfInterface.h" #include "TCPSocket.h" + +//www.amazon.it CA certificate in PEM format +char CA_cert []="-----BEGIN CERTIFICATE-----\r\n" +"MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB\r\n" +"yjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQL\r\n" +"ExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJp\r\n" +"U2lnbiwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxW\r\n" +"ZXJpU2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0\r\n" +"aG9yaXR5IC0gRzUwHhcNMDYxMTA4MDAwMDAwWhcNMzYwNzE2MjM1OTU5WjCByjEL\r\n" +"MAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZW\r\n" +"ZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTowOAYDVQQLEzEoYykgMjAwNiBWZXJpU2ln\r\n" +"biwgSW5jLiAtIEZvciBhdXRob3JpemVkIHVzZSBvbmx5MUUwQwYDVQQDEzxWZXJp\r\n" +"U2lnbiBDbGFzcyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9y\r\n" +"aXR5IC0gRzUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAo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sEdao7WNq\r\n" +"-----END CERTIFICATE-----\r\n"; + //------------------------------------ // Hyperterminal configuration // 9600 bauds, 8-bit data, no parity @@ -68,7 +99,7 @@ pc.printf("\r\nerror connecting to AP.\r\n"); return -1; } - + const char *ip = spwf.get_ip_address(); const char *mac = spwf.get_mac_address(); @@ -88,24 +119,54 @@ return -1; } else pc.printf("\r\nconnected to host server\r\n"); - char buffer[64]; + char buffer[100]; int count = 0; pc.printf("\r\nReceiving Data\r\n"); count = socket.recv(buffer, sizeof buffer); if(count > 0) { - printf("public IP address is: %s\r\n", &buffer[15]); + buffer [count]='\0'; + printf("%s\r\n", buffer); } else pc.printf("\r\nData not received\r\n"); pc.printf("\r\nClosing Socket\r\n"); socket.close(); - - pc.printf("\r\ndisconnecting....\r\n"); + pc.printf("\r\nUnsecure Socket Test complete.\r\n"); + +// Start Secure Socket connection test (one way server autentication only) + time_t ctTime; + ctTime = time(NULL); + printf ("Start Secure Socket connection with one way server autentication test\n\r"); + printf("Initial System Time is: %s\r\n", ctime(&ctTime)); + printf("Need to adjust time? if yes enter time in seconds elapsed since Epoch (cmd: date +'%%s'), otherwise enter 0 "); + int t=0; + scanf("%d",&t); + printf ("entered time is: %d \n\r", t); + if (t != 0) { time_t txTm = t; set_time(txTm); } + printf ("The current system time is: %s", ctime (&ctTime)); + if (!spwf.clean_TLS_certificate(ALL)) printf ("ERROR clean_TLS_certificate\n\r"); + if (!spwf.set_TLS_certificate(CA_cert, sizeof(CA_cert), FLASH_CA_ROOT_CERT)) printf ("ERROR set_TLS_certificate\n\r"); + if (!spwf.set_TLS_SRV_domain("www.amazon.it",FLASH_DOMAIN)) printf ("ERROR set_TLS_CA_domain\n\r"); +// NOTE: DNS requires UDP socket. DNS is invoked while connecting a secure socket to resolve URL to IP, +// so DNS doesnt answer as it requires unsecure UDP socket connection. +// workaroud: convert URL to IP quering DNS before secure socket creation, then connect +// the secure socket created to the retrieved IP. + spwf.gethostbyname(&addr,"www.amazon.it"); // ask DNS + addr.set_port(443); + spwf.set_secure_sockets(); + socket.open(&spwf); + printf ("Connecting to www.amazon.it IP: %s ...\n\r", addr.get_ip_address()); + err = socket.connect(addr); + if (err != 0 )printf ("ERROR secure socket connection failed: %d\n\r", err); + else printf ("--->>> Secure socket CONNECTED to: %s\n\r", addr.get_ip_address()); + spwf.set_unsecure_sockets(); + socket.close(); + printf ("Socket closed\n\r"); spwf.disconnect(); - pc.printf("\r\nTest complete.\r\n"); - + printf ("WIFI disconnected, exiting ...\n\r"); + while(1) { wait(1); myled = !myled;