INSAT_MiniPRoject

Dependencies:   MQTT NDefLib NetworkSocketAPI Servo Light_Sensor_Nucleo X_NUCLEO_IDW01M1v2 mbed

Fork of IDW01M1_Cloud_IBM by ST

Committer:
mridup
Date:
Wed May 25 06:23:43 2016 +0000
Revision:
11:70df7089e2da
Parent:
10:c7b62ce013ad
Child:
13:0b31131bf711
updating x_nucleo_idw01m1 and networksocketAPI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mridup 10:c7b62ce013ad 1 /* SpwfInterface NetworkSocketAPI Example Program
mridup 10:c7b62ce013ad 2 * Copyright (c) 2015 ARM Limited
mridup 10:c7b62ce013ad 3 *
mridup 10:c7b62ce013ad 4 * Licensed under the Apache License, Version 2.0 (the "License");
mridup 10:c7b62ce013ad 5 * you may not use this file except in compliance with the License.
mridup 10:c7b62ce013ad 6 * You may obtain a copy of the License at
mridup 10:c7b62ce013ad 7 *
mridup 10:c7b62ce013ad 8 * http://www.apache.org/licenses/LICENSE-2.0
mridup 10:c7b62ce013ad 9 *
mridup 10:c7b62ce013ad 10 * Unless required by applicable law or agreed to in writing, software
mridup 10:c7b62ce013ad 11 * distributed under the License is distributed on an "AS IS" BASIS,
mridup 10:c7b62ce013ad 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mridup 10:c7b62ce013ad 13 * See the License for the specific language governing permissions and
mridup 10:c7b62ce013ad 14 * limitations under the License.
mridup 10:c7b62ce013ad 15 */
mridup 10:c7b62ce013ad 16
mridup 0:cbf8bc43bc9e 17 #include "mbed.h"
mridup 0:cbf8bc43bc9e 18 #include "SPWFInterface.h"
mridup 4:1ed7f173eec5 19 #include "TCPSocket.h"
mridup 0:cbf8bc43bc9e 20
mridup 0:cbf8bc43bc9e 21 //------------------------------------
mridup 0:cbf8bc43bc9e 22 // Hyperterminal configuration
mridup 0:cbf8bc43bc9e 23 // 9600 bauds, 8-bit data, no parity
mridup 0:cbf8bc43bc9e 24 //------------------------------------
mridup 0:cbf8bc43bc9e 25
mridup 8:6df01cb43137 26 Serial pc(SERIAL_TX, SERIAL_RX);
mridup 3:dfb8c6c8c31b 27 DigitalOut myled(LED1);
mridup 5:e913a401b174 28 SpwfSAInterface spwf(PA_9, PA_10, PC_12, PC_8, PA_12, true);
mridup 4:1ed7f173eec5 29
mridup 4:1ed7f173eec5 30 int main() {
mridup 8:6df01cb43137 31 int err;
mridup 0:cbf8bc43bc9e 32 char * ssid = "STM";
mridup 0:cbf8bc43bc9e 33 char * seckey = "STMdemoPWD";
mridup 4:1ed7f173eec5 34
mridup 6:0d838d564181 35 pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
mridup 8:6df01cb43137 36 pc.printf("\r\nconnecting to AP\r\n");
mridup 0:cbf8bc43bc9e 37
mridup 11:70df7089e2da 38 err = spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2);//WPA
mridup 4:1ed7f173eec5 39 if(err!=0)
mridup 4:1ed7f173eec5 40 {
mridup 8:6df01cb43137 41 pc.printf("\r\nerror connecting to AP.\r\n");
mridup 4:1ed7f173eec5 42 return -1;
mridup 0:cbf8bc43bc9e 43 }
mridup 4:1ed7f173eec5 44
mridup 8:6df01cb43137 45 pc.printf("\r\nnow connected\r\n");
mridup 4:1ed7f173eec5 46
mridup 8:6df01cb43137 47 const char *ip = spwf.get_ip_address();
mridup 8:6df01cb43137 48 const char *mac = spwf.get_mac_address();
mridup 4:1ed7f173eec5 49
mridup 8:6df01cb43137 50 pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP");
mridup 8:6df01cb43137 51 pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC");
mridup 8:6df01cb43137 52
mridup 8:6df01cb43137 53 SocketAddress addr(&spwf, "st.com");
mridup 8:6df01cb43137 54 printf("\r\nst.com resolved to: %s\r\n", addr.get_ip_address());
mridup 8:6df01cb43137 55
mridup 10:c7b62ce013ad 56 pc.printf("\r\nconnecting to http://time-d.nist.gov\r\n");
mridup 4:1ed7f173eec5 57
mridup 8:6df01cb43137 58 TCPSocket socket(&spwf);
mridup 8:6df01cb43137 59 err = socket.connect("time-d.nist.gov", 37);
mridup 8:6df01cb43137 60 if(err!=0)
mridup 8:6df01cb43137 61 {
mridup 8:6df01cb43137 62 pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err);
mridup 8:6df01cb43137 63 return -1;
mridup 8:6df01cb43137 64 }
mridup 10:c7b62ce013ad 65 char buffer[10];
mridup 8:6df01cb43137 66 int count = 0;
mridup 8:6df01cb43137 67 pc.printf("\r\nReceiving Data\r\n");
mridup 8:6df01cb43137 68 count = socket.recv(buffer, sizeof buffer);
mridup 8:6df01cb43137 69
mridup 8:6df01cb43137 70 if(count > 0)
mridup 8:6df01cb43137 71 {
mridup 8:6df01cb43137 72 pc.printf("\r\nReceived: %ld bytes, 0x%02x 0x%02x 0x%02x 0x%02x\r\n", \
mridup 8:6df01cb43137 73 count, buffer[0], buffer[1], buffer[2], buffer[3]);
mridup 8:6df01cb43137 74 }
mridup 8:6df01cb43137 75 else pc.printf("\r\nData not received\r\n");
mridup 8:6df01cb43137 76
mridup 8:6df01cb43137 77 pc.printf("\r\nClosing Socket\r\n");
mridup 8:6df01cb43137 78 socket.close();
mridup 4:1ed7f173eec5 79
mridup 8:6df01cb43137 80 pc.printf("\r\ndisconnecting....\r\n");
mridup 8:6df01cb43137 81 spwf.disconnect();
mridup 8:6df01cb43137 82 pc.printf("\r\nTest complete.\r\n");
mridup 4:1ed7f173eec5 83
mridup 4:1ed7f173eec5 84 while(1) {
mridup 4:1ed7f173eec5 85 wait(1);
mridup 4:1ed7f173eec5 86 myled = !myled;
mridup 3:dfb8c6c8c31b 87 }
mridup 4:1ed7f173eec5 88 }