989
Dependencies: mbed Servo X_NUCLEO_IKS01A2 X_NUCLEO_IDW01M1v2 NetworkSocketAPI NDefLib MQTT
Diff: main.cpp
- Revision:
- 2:ba0531d896f0
- Parent:
- 1:041e9f05738c
- Child:
- 3:dfb8c6c8c31b
--- a/main.cpp Tue Apr 12 09:26:56 2016 +0000 +++ b/main.cpp Wed Apr 13 10:14:35 2016 +0000 @@ -1,6 +1,22 @@ +/* NetworkSocketAPI Example Program + * Copyright (c) 2015 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "mbed.h" #include "SPWFInterface.h" -#include "TCPSocket.h" +#include "NSAPITests.h" //------------------------------------ // Hyperterminal configuration @@ -8,82 +24,32 @@ //------------------------------------ Serial pc(SERIAL_TX, SERIAL_RX); - -DigitalOut myled(LED1); SpwfSAInterface *spwf; -TCPSocket *tcp_; - -int main() { - int32_t err; + +int main() +{ + int32_t err; char * ssid = "STM"; char * seckey = "STMdemoPWD"; - char * hostname = "192.168.1.5"; - char * data = "Hello World!\r\n"; - uint32_t len = strlen(data); - - pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); - + spwf = createSPWFInstance(); - tcp_ = new TCPSocket(spwf); - err = spwf->init(); if(err!=0) { - pc.printf("\r\n[APP] error initializing.\r\n"); - return -1; - } - - pc.printf("\r\n[APP] connecting to AP\r\n"); - - err = spwf->connect(ssid, seckey, NS_SECURITY_WPA); - if(err!=0) - { - pc.printf("\r\n[APP] error connecting to AP.\r\n"); + pc.printf("\r\nerror initializing.\r\n"); return -1; } - pc.printf("\r\n[APP] now connected\r\n"); - - const char *ip = spwf->getIPAddress(); - const char *mac = spwf->getMACAddress(); - - pc.printf("\r\n[APP] IP Address is: %s\r\n", (ip) ? ip : "No IP"); - pc.printf("\r\n[APP] MAC Address is: %s\r\n", (mac) ? mac : "No MAC"); - - pc.printf("\r\n[APP] opening socket\r\n"); - - err = tcp_->open((const char *)hostname, 32000); - //err = tcp_->open("time-d.nist.gov", 37); + pc.printf("\r\nconnecting to AP\r\n"); - if(err==0) - { - pc.printf("\r\n[APP] socket open OK\r\n"); - pc.printf("\r\n[APP] hostname resolved to: %s\r\n", tcp_->getIPAddress()); - - err = tcp_->send(data, len); - - if(err == 0) - pc.printf("\r\n[APP] socket send OK\r\n"); - else - pc.printf("\r\n[APP] socket send error\r\n"); - - char received[4]; - int32_t size = 0; - pc.printf("\r\n[APP] waiting for data recv\r\n"); - size = tcp_->recv(received, sizeof(received)); - if(size==-1) - pc.printf("\r\n[APP] Receive error."); - else - pc.printf("\r\n[APP] Received: %ld bytes, 0x%02x 0x%02x 0x%02x 0x%02x\r\n", size, received[0], received[1], received[2], received[3]); - } - else - pc.printf("\r\n[APP] socket open Error\r\n"); - - - while(1) { - wait(1); - //pc.printf("This program runs since %d seconds.\n", i++); - myled = !myled; + err = spwf->connect(ssid, seckey, NS_SECURITY_WPA); + + if (err) { + printf("Interface failed to connect with code %d\r\n", err); + } else { + nsapi_tests("SPWF Tests", spwf, "192.168.1.5", 32001); + spwf->disconnect(); } -} + while(1); +} \ No newline at end of file