ST / Mbed 2 deprecated HelloWorld_IDW01M1

Dependencies:   NetworkSocketAPI X_NUCLEO_IDW01M1 mbed

Fork of HelloWorld_IDW01M1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* SpwfInterface NetworkSocketAPI Example Program
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "SPWFInterface.h"
00019 #include "TCPSocket.h"
00020 
00021 //------------------------------------
00022 // Hyperterminal configuration
00023 // 9600 bauds, 8-bit data, no parity
00024 //------------------------------------
00025 
00026 Serial pc(SERIAL_TX, SERIAL_RX); 
00027 DigitalOut myled(LED1);
00028 SpwfSAInterface spwf(PA_9, PA_10, PC_12, PC_8, PA_12, true);
00029     
00030 int main() {
00031     int err;    
00032     char * ssid = "STM";
00033     char * seckey = "STMdemoPWD";
00034     
00035     pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
00036     pc.printf("\r\nconnecting to AP\r\n");
00037             
00038     err = spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2);//WPA
00039     if(err!=0)
00040     {
00041         pc.printf("\r\nerror connecting to AP.\r\n");
00042         return -1;
00043     }
00044     
00045     pc.printf("\r\nnow connected\r\n");
00046             
00047     const char *ip = spwf.get_ip_address();
00048     const char *mac = spwf.get_mac_address();
00049     
00050     pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP");
00051     pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC");    
00052     
00053     SocketAddress addr(&spwf, "st.com");
00054     printf("\r\nst.com resolved to: %s\r\n", addr.get_ip_address());
00055 
00056     pc.printf("\r\nconnecting to http://time-d.nist.gov\r\n");
00057     
00058     TCPSocket socket(&spwf);
00059     err = socket.connect("time-d.nist.gov", 37);
00060     if(err!=0) 
00061     {
00062       pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err); 
00063       return -1;
00064     }
00065     char buffer[10];
00066     int count = 0;
00067     pc.printf("\r\nReceiving Data\r\n"); 
00068     count = socket.recv(buffer, sizeof buffer);
00069     
00070     if(count > 0)
00071     {
00072         pc.printf("\r\nReceived: %ld bytes, 0x%02x 0x%02x 0x%02x 0x%02x\r\n", \
00073                             count, buffer[0], buffer[1], buffer[2], buffer[3]);
00074     }
00075     else pc.printf("\r\nData not received\r\n");
00076 
00077     pc.printf("\r\nClosing Socket\r\n");
00078     socket.close();
00079         
00080     pc.printf("\r\ndisconnecting....\r\n");
00081     spwf.disconnect();
00082     pc.printf("\r\nTest complete.\r\n");
00083                 
00084     while(1) { 
00085       wait(1);
00086       myled = !myled;
00087     }
00088 }