![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
INSAT_MiniPRoject
Dependencies: MQTT NDefLib NetworkSocketAPI Servo Light_Sensor_Nucleo X_NUCLEO_IDW01M1v2 mbed
Fork of IDW01M1_Cloud_IBM by
main.cpp@1:041e9f05738c, 2016-04-12 (annotated)
- Committer:
- mridup
- Date:
- Tue Apr 12 09:26:56 2016 +0000
- Revision:
- 1:041e9f05738c
- Parent:
- 0:cbf8bc43bc9e
- Child:
- 2:ba0531d896f0
Optimization changes and other misc.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:cbf8bc43bc9e | 1 | #include "mbed.h" |
mridup | 0:cbf8bc43bc9e | 2 | #include "SPWFInterface.h" |
mridup | 0:cbf8bc43bc9e | 3 | #include "TCPSocket.h" |
mridup | 0:cbf8bc43bc9e | 4 | |
mridup | 0:cbf8bc43bc9e | 5 | //------------------------------------ |
mridup | 0:cbf8bc43bc9e | 6 | // Hyperterminal configuration |
mridup | 0:cbf8bc43bc9e | 7 | // 9600 bauds, 8-bit data, no parity |
mridup | 0:cbf8bc43bc9e | 8 | //------------------------------------ |
mridup | 0:cbf8bc43bc9e | 9 | |
mridup | 0:cbf8bc43bc9e | 10 | Serial pc(SERIAL_TX, SERIAL_RX); |
mridup | 0:cbf8bc43bc9e | 11 | |
mridup | 0:cbf8bc43bc9e | 12 | DigitalOut myled(LED1); |
mridup | 0:cbf8bc43bc9e | 13 | |
mridup | 0:cbf8bc43bc9e | 14 | SpwfSAInterface *spwf; |
mridup | 1:041e9f05738c | 15 | TCPSocket *tcp_; |
mridup | 1:041e9f05738c | 16 | |
mridup | 0:cbf8bc43bc9e | 17 | int main() { |
mridup | 0:cbf8bc43bc9e | 18 | int32_t err; |
mridup | 0:cbf8bc43bc9e | 19 | char * ssid = "STM"; |
mridup | 0:cbf8bc43bc9e | 20 | char * seckey = "STMdemoPWD"; |
mridup | 0:cbf8bc43bc9e | 21 | char * hostname = "192.168.1.5"; |
mridup | 0:cbf8bc43bc9e | 22 | char * data = "Hello World!\r\n"; |
mridup | 0:cbf8bc43bc9e | 23 | uint32_t len = strlen(data); |
mridup | 0:cbf8bc43bc9e | 24 | |
mridup | 0:cbf8bc43bc9e | 25 | pc.printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n"); |
mridup | 0:cbf8bc43bc9e | 26 | |
mridup | 0:cbf8bc43bc9e | 27 | spwf = createSPWFInstance(); |
mridup | 0:cbf8bc43bc9e | 28 | tcp_ = new TCPSocket(spwf); |
mridup | 1:041e9f05738c | 29 | |
mridup | 0:cbf8bc43bc9e | 30 | err = spwf->init(); |
mridup | 0:cbf8bc43bc9e | 31 | if(err!=0) |
mridup | 0:cbf8bc43bc9e | 32 | { |
mridup | 0:cbf8bc43bc9e | 33 | pc.printf("\r\n[APP] error initializing.\r\n"); |
mridup | 0:cbf8bc43bc9e | 34 | return -1; |
mridup | 0:cbf8bc43bc9e | 35 | } |
mridup | 1:041e9f05738c | 36 | |
mridup | 0:cbf8bc43bc9e | 37 | pc.printf("\r\n[APP] connecting to AP\r\n"); |
mridup | 0:cbf8bc43bc9e | 38 | |
mridup | 0:cbf8bc43bc9e | 39 | err = spwf->connect(ssid, seckey, NS_SECURITY_WPA); |
mridup | 0:cbf8bc43bc9e | 40 | if(err!=0) |
mridup | 0:cbf8bc43bc9e | 41 | { |
mridup | 0:cbf8bc43bc9e | 42 | pc.printf("\r\n[APP] error connecting to AP.\r\n"); |
mridup | 0:cbf8bc43bc9e | 43 | return -1; |
mridup | 0:cbf8bc43bc9e | 44 | } |
mridup | 0:cbf8bc43bc9e | 45 | |
mridup | 0:cbf8bc43bc9e | 46 | pc.printf("\r\n[APP] now connected\r\n"); |
mridup | 0:cbf8bc43bc9e | 47 | |
mridup | 0:cbf8bc43bc9e | 48 | const char *ip = spwf->getIPAddress(); |
mridup | 0:cbf8bc43bc9e | 49 | const char *mac = spwf->getMACAddress(); |
mridup | 0:cbf8bc43bc9e | 50 | |
mridup | 0:cbf8bc43bc9e | 51 | pc.printf("\r\n[APP] IP Address is: %s\r\n", (ip) ? ip : "No IP"); |
mridup | 0:cbf8bc43bc9e | 52 | pc.printf("\r\n[APP] MAC Address is: %s\r\n", (mac) ? mac : "No MAC"); |
mridup | 0:cbf8bc43bc9e | 53 | |
mridup | 0:cbf8bc43bc9e | 54 | pc.printf("\r\n[APP] opening socket\r\n"); |
mridup | 0:cbf8bc43bc9e | 55 | |
mridup | 0:cbf8bc43bc9e | 56 | err = tcp_->open((const char *)hostname, 32000); |
mridup | 0:cbf8bc43bc9e | 57 | //err = tcp_->open("time-d.nist.gov", 37); |
mridup | 0:cbf8bc43bc9e | 58 | |
mridup | 0:cbf8bc43bc9e | 59 | if(err==0) |
mridup | 0:cbf8bc43bc9e | 60 | { |
mridup | 0:cbf8bc43bc9e | 61 | pc.printf("\r\n[APP] socket open OK\r\n"); |
mridup | 0:cbf8bc43bc9e | 62 | pc.printf("\r\n[APP] hostname resolved to: %s\r\n", tcp_->getIPAddress()); |
mridup | 0:cbf8bc43bc9e | 63 | |
mridup | 0:cbf8bc43bc9e | 64 | err = tcp_->send(data, len); |
mridup | 0:cbf8bc43bc9e | 65 | |
mridup | 0:cbf8bc43bc9e | 66 | if(err == 0) |
mridup | 0:cbf8bc43bc9e | 67 | pc.printf("\r\n[APP] socket send OK\r\n"); |
mridup | 0:cbf8bc43bc9e | 68 | else |
mridup | 0:cbf8bc43bc9e | 69 | pc.printf("\r\n[APP] socket send error\r\n"); |
mridup | 0:cbf8bc43bc9e | 70 | |
mridup | 0:cbf8bc43bc9e | 71 | char received[4]; |
mridup | 0:cbf8bc43bc9e | 72 | int32_t size = 0; |
mridup | 0:cbf8bc43bc9e | 73 | pc.printf("\r\n[APP] waiting for data recv\r\n"); |
mridup | 0:cbf8bc43bc9e | 74 | size = tcp_->recv(received, sizeof(received)); |
mridup | 0:cbf8bc43bc9e | 75 | if(size==-1) |
mridup | 0:cbf8bc43bc9e | 76 | pc.printf("\r\n[APP] Receive error."); |
mridup | 0:cbf8bc43bc9e | 77 | else |
mridup | 0:cbf8bc43bc9e | 78 | 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]); |
mridup | 0:cbf8bc43bc9e | 79 | } |
mridup | 0:cbf8bc43bc9e | 80 | else |
mridup | 0:cbf8bc43bc9e | 81 | pc.printf("\r\n[APP] socket open Error\r\n"); |
mridup | 1:041e9f05738c | 82 | |
mridup | 0:cbf8bc43bc9e | 83 | |
mridup | 0:cbf8bc43bc9e | 84 | while(1) { |
mridup | 0:cbf8bc43bc9e | 85 | wait(1); |
mridup | 0:cbf8bc43bc9e | 86 | //pc.printf("This program runs since %d seconds.\n", i++); |
mridup | 0:cbf8bc43bc9e | 87 | myled = !myled; |
mridup | 0:cbf8bc43bc9e | 88 | } |
mridup | 0:cbf8bc43bc9e | 89 | } |