INSAT_MiniPRoject

Dependencies:   MQTT NDefLib NetworkSocketAPI Servo Light_Sensor_Nucleo X_NUCLEO_IDW01M1v2 mbed

Fork of IDW01M1_Cloud_IBM by ST

Committer:
fabiombed
Date:
Wed Sep 07 14:14:27 2016 +0000
Revision:
13:0b31131bf711
Parent:
11:70df7089e2da
Child:
14:641560b57584
Added MQTT for Cloud IBM by WiFi

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
fabiombed 13:0b31131bf711 21 #include "MQTTClient.h"
fabiombed 13:0b31131bf711 22
fabiombed 13:0b31131bf711 23
mridup 0:cbf8bc43bc9e 24 //------------------------------------
mridup 0:cbf8bc43bc9e 25 // Hyperterminal configuration
mridup 0:cbf8bc43bc9e 26 // 9600 bauds, 8-bit data, no parity
mridup 0:cbf8bc43bc9e 27 //------------------------------------
mridup 0:cbf8bc43bc9e 28
mridup 8:6df01cb43137 29 Serial pc(SERIAL_TX, SERIAL_RX);
mridup 3:dfb8c6c8c31b 30 DigitalOut myled(LED1);
mridup 5:e913a401b174 31 SpwfSAInterface spwf(PA_9, PA_10, PC_12, PC_8, PA_12, true);
mridup 4:1ed7f173eec5 32
fabiombed 13:0b31131bf711 33 int main()
fabiombed 13:0b31131bf711 34 {
mridup 8:6df01cb43137 35 int err;
fabiombed 13:0b31131bf711 36 char * ssid = "crespan"; // Network must be visible otherwise it can't connect
fabiombed 13:0b31131bf711 37 char * seckey = "Elfrontal0";
mridup 4:1ed7f173eec5 38
mridup 6:0d838d564181 39 pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");
fabiombed 13:0b31131bf711 40 pc.printf("\r\nconnecting to AP\r\n");
fabiombed 13:0b31131bf711 41
fabiombed 13:0b31131bf711 42 err = spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2); //WPA2
fabiombed 13:0b31131bf711 43
mridup 4:1ed7f173eec5 44 if(err!=0)
mridup 4:1ed7f173eec5 45 {
mridup 8:6df01cb43137 46 pc.printf("\r\nerror connecting to AP.\r\n");
mridup 4:1ed7f173eec5 47 return -1;
mridup 0:cbf8bc43bc9e 48 }
mridup 4:1ed7f173eec5 49
mridup 8:6df01cb43137 50 pc.printf("\r\nnow connected\r\n");
mridup 4:1ed7f173eec5 51
mridup 8:6df01cb43137 52 const char *ip = spwf.get_ip_address();
mridup 8:6df01cb43137 53 const char *mac = spwf.get_mac_address();
mridup 4:1ed7f173eec5 54
mridup 8:6df01cb43137 55 pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "No IP");
mridup 8:6df01cb43137 56 pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "No MAC");
mridup 8:6df01cb43137 57
mridup 8:6df01cb43137 58 SocketAddress addr(&spwf, "st.com");
mridup 8:6df01cb43137 59 printf("\r\nst.com resolved to: %s\r\n", addr.get_ip_address());
mridup 8:6df01cb43137 60
mridup 10:c7b62ce013ad 61 pc.printf("\r\nconnecting to http://time-d.nist.gov\r\n");
fabiombed 13:0b31131bf711 62 //pc.printf("\r\nconnecting to https://quickstart.internetofthings.ibmcloud.com\r\n");
mridup 4:1ed7f173eec5 63
mridup 8:6df01cb43137 64 TCPSocket socket(&spwf);
mridup 8:6df01cb43137 65 err = socket.connect("time-d.nist.gov", 37);
fabiombed 13:0b31131bf711 66
mridup 8:6df01cb43137 67 if(err!=0)
mridup 8:6df01cb43137 68 {
mridup 8:6df01cb43137 69 pc.printf("\r\nCould not connect to Socket, err = %d!!\r\n", err);
mridup 8:6df01cb43137 70 return -1;
mridup 8:6df01cb43137 71 }
mridup 10:c7b62ce013ad 72 char buffer[10];
mridup 8:6df01cb43137 73 int count = 0;
mridup 8:6df01cb43137 74 pc.printf("\r\nReceiving Data\r\n");
mridup 8:6df01cb43137 75 count = socket.recv(buffer, sizeof buffer);
mridup 8:6df01cb43137 76
mridup 8:6df01cb43137 77 if(count > 0)
mridup 8:6df01cb43137 78 {
mridup 8:6df01cb43137 79 pc.printf("\r\nReceived: %ld bytes, 0x%02x 0x%02x 0x%02x 0x%02x\r\n", \
mridup 8:6df01cb43137 80 count, buffer[0], buffer[1], buffer[2], buffer[3]);
mridup 8:6df01cb43137 81 }
mridup 8:6df01cb43137 82 else pc.printf("\r\nData not received\r\n");
mridup 8:6df01cb43137 83
mridup 8:6df01cb43137 84 pc.printf("\r\nClosing Socket\r\n");
mridup 8:6df01cb43137 85 socket.close();
mridup 4:1ed7f173eec5 86
mridup 8:6df01cb43137 87 pc.printf("\r\ndisconnecting....\r\n");
mridup 8:6df01cb43137 88 spwf.disconnect();
mridup 8:6df01cb43137 89 pc.printf("\r\nTest complete.\r\n");
mridup 4:1ed7f173eec5 90
mridup 4:1ed7f173eec5 91 while(1) {
mridup 4:1ed7f173eec5 92 wait(1);
mridup 4:1ed7f173eec5 93 myled = !myled;
mridup 3:dfb8c6c8c31b 94 }
mridup 4:1ed7f173eec5 95 }