Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SNICInterface mbed-rtos mbed
Fork of murataDemo by
main.cpp@32:67402fe56150, 2015-04-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Apr 09 22:03:34 2015 +0000
- Revision:
- 32:67402fe56150
- Parent:
- 31:c42d189364b4
Basic TCP Client working with murata wifi chip
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kishino | 21:25b85cbbdd82 | 1 | /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License |
| kishino | 23:39cf9f03b076 | 2 | * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. |
| kishino | 21:25b85cbbdd82 | 3 | * |
| kishino | 21:25b85cbbdd82 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| kishino | 21:25b85cbbdd82 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| kishino | 21:25b85cbbdd82 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| kishino | 21:25b85cbbdd82 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| kishino | 21:25b85cbbdd82 | 8 | * furnished to do so, subject to the following conditions: |
| kishino | 21:25b85cbbdd82 | 9 | * |
| kishino | 21:25b85cbbdd82 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
| kishino | 21:25b85cbbdd82 | 11 | * substantial portions of the Software. |
| kishino | 21:25b85cbbdd82 | 12 | * |
| kishino | 21:25b85cbbdd82 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| kishino | 21:25b85cbbdd82 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| kishino | 21:25b85cbbdd82 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| kishino | 21:25b85cbbdd82 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| kishino | 21:25b85cbbdd82 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| kishino | 21:25b85cbbdd82 | 18 | */ |
| xively | 0:efdea27c3b81 | 19 | #include "mbed.h" |
| kishino | 14:6d58d3855feb | 20 | #include "SNIC_WifiInterface.h" |
| mbedAustin | 32:67402fe56150 | 21 | #include "TCPSocketServer.h" |
| xively | 7:0eff5db44b8b | 22 | |
| mbedAustin | 28:174412ff9671 | 23 | Serial pc(USBTX, USBRX); |
| xively | 7:0eff5db44b8b | 24 | |
| errordeveloper | 10:86ffba646df1 | 25 | |
| mbedAustin | 29:9f08c7152c7a | 26 | #define WIFI_SSID "demossid" |
| mbedAustin | 29:9f08c7152c7a | 27 | #define WIFI_SECURITY_TYPE e_SEC_OPEN |
| mbedAustin | 28:174412ff9671 | 28 | #define WIFI_SECUTIRY_KEY "WPA2_PASSPHRASE" |
| mbedAustin | 28:174412ff9671 | 29 | #define WIFI_SECUTIRY_KEY_LEN 15 |
| errordeveloper | 11:bdf601a405fc | 30 | |
| mbedAustin | 29:9f08c7152c7a | 31 | //#define WIFI_SSID "AP_SSID" |
| mbedAustin | 29:9f08c7152c7a | 32 | //#define WIFI_SECURITY_TYPE e_SEC_WPA2_AES |
| mbedAustin | 29:9f08c7152c7a | 33 | //#define WIFI_SECUTIRY_KEY "WPA2_PASSPHRASE" |
| mbedAustin | 29:9f08c7152c7a | 34 | //#define WIFI_SECUTIRY_KEY_LEN 15 |
| mbedAustin | 29:9f08c7152c7a | 35 | |
| mbedAustin | 29:9f08c7152c7a | 36 | |
| mbedAustin | 28:174412ff9671 | 37 | C_SNIC_WifiInterface wifi( D1, D0, NC, NC, D3 ); |
| kishino | 17:0bf3c49a83d5 | 38 | |
| mbedAustin | 32:67402fe56150 | 39 | int wifiInit() |
| mbedAustin | 30:de5a32932408 | 40 | { |
| mbedAustin | 29:9f08c7152c7a | 41 | int check = 0; |
| mbedAustin | 28:174412ff9671 | 42 | |
| kishino | 14:6d58d3855feb | 43 | // Initialize Wi-Fi interface |
| mbedAustin | 29:9f08c7152c7a | 44 | check = wifi.init(); |
| mbedAustin | 29:9f08c7152c7a | 45 | if( check != 0 ) { |
| mbedAustin | 28:174412ff9671 | 46 | printf( "Wifi could not be initialized, halting.\r\n" ); |
| kishino | 14:6d58d3855feb | 47 | return -1; |
| mbedAustin | 29:9f08c7152c7a | 48 | } else { |
| mbedAustin | 28:174412ff9671 | 49 | printf("wifi initialized successfully!\r\n"); |
| mbedAustin | 28:174412ff9671 | 50 | } |
| kishino | 19:4e2900daad59 | 51 | wait(0.5); |
| mbedAustin | 29:9f08c7152c7a | 52 | |
| mbedAustin | 28:174412ff9671 | 53 | // good form to make sure you are disconnected from all AP's |
| mbedAustin | 29:9f08c7152c7a | 54 | check = wifi.disconnect(); |
| mbedAustin | 29:9f08c7152c7a | 55 | if( check != 0 ) { |
| mbedAustin | 28:174412ff9671 | 56 | printf( "disconnect failed\r\n" ); |
| mbedAustin | 28:174412ff9671 | 57 | return -1; |
| mbedAustin | 29:9f08c7152c7a | 58 | } else { |
| mbedAustin | 28:174412ff9671 | 59 | printf("disconnection successful!\r\n"); |
| mbedAustin | 29:9f08c7152c7a | 60 | } |
| mbedAustin | 28:174412ff9671 | 61 | wait(0.3); |
| mbedAustin | 29:9f08c7152c7a | 62 | |
| mbedAustin | 28:174412ff9671 | 63 | // Connect AP |
| mbedAustin | 29:9f08c7152c7a | 64 | check = wifi.connect( WIFI_SSID |
| mbedAustin | 29:9f08c7152c7a | 65 | , strlen(WIFI_SSID) |
| mbedAustin | 29:9f08c7152c7a | 66 | , WIFI_SECURITY_TYPE |
| mbedAustin | 29:9f08c7152c7a | 67 | , WIFI_SECUTIRY_KEY |
| mbedAustin | 29:9f08c7152c7a | 68 | , WIFI_SECUTIRY_KEY_LEN ); |
| mbedAustin | 29:9f08c7152c7a | 69 | if( check != 0) { |
| mbedAustin | 29:9f08c7152c7a | 70 | printf("Connect Failed\r\n"); |
| mbedAustin | 29:9f08c7152c7a | 71 | } else { |
| mbedAustin | 29:9f08c7152c7a | 72 | printf("connected successfully!\r\n"); |
| mbedAustin | 29:9f08c7152c7a | 73 | } |
| kishino | 14:6d58d3855feb | 74 | wait(0.5); |
| kishino | 14:6d58d3855feb | 75 | |
| mbedAustin | 29:9f08c7152c7a | 76 | // Get DHCP IP |
| mbedAustin | 29:9f08c7152c7a | 77 | check = wifi.setIPConfig( true ); // get IP as DHCP IP |
| mbedAustin | 29:9f08c7152c7a | 78 | if(check != 0) { |
| mbedAustin | 29:9f08c7152c7a | 79 | printf("SetIPConfig failed \r\n"); |
| mbedAustin | 29:9f08c7152c7a | 80 | } else { |
| mbedAustin | 29:9f08c7152c7a | 81 | printf("SetIPConfig successful \r\n"); |
| mbedAustin | 29:9f08c7152c7a | 82 | } |
| mbedAustin | 30:de5a32932408 | 83 | |
| mbedAustin | 30:de5a32932408 | 84 | // check IP Address |
| mbedAustin | 30:de5a32932408 | 85 | char * ip ; |
| mbedAustin | 30:de5a32932408 | 86 | ip = wifi.getIPAddress(); |
| mbedAustin | 30:de5a32932408 | 87 | if(ip == 0) { |
| mbedAustin | 30:de5a32932408 | 88 | printf("getIP failed. \r\n"); |
| mbedAustin | 30:de5a32932408 | 89 | } else { |
| mbedAustin | 30:de5a32932408 | 90 | printf("getIP success: %s \r\n",ip); |
| mbedAustin | 30:de5a32932408 | 91 | } |
| mbedAustin | 32:67402fe56150 | 92 | } |
| mbedAustin | 30:de5a32932408 | 93 | |
| mbedAustin | 32:67402fe56150 | 94 | // |
| mbedAustin | 32:67402fe56150 | 95 | // main loop |
| mbedAustin | 32:67402fe56150 | 96 | // |
| mbedAustin | 32:67402fe56150 | 97 | int main() |
| mbedAustin | 32:67402fe56150 | 98 | { |
| mbedAustin | 32:67402fe56150 | 99 | |
| mbedAustin | 32:67402fe56150 | 100 | // Initialize Wifi |
| mbedAustin | 32:67402fe56150 | 101 | wifiInit(); |
| mbedAustin | 32:67402fe56150 | 102 | |
| mbedAustin | 32:67402fe56150 | 103 | // TCP Schenanigans! |
| mbedAustin | 32:67402fe56150 | 104 | const char* ECHO_SERVER_ADDRESS = "192.168.11.3"; |
| mbedAustin | 32:67402fe56150 | 105 | const int ECHO_SERVER_PORT = 7; |
| mbedAustin | 32:67402fe56150 | 106 | |
| mbedAustin | 32:67402fe56150 | 107 | // connect to server |
| mbedAustin | 32:67402fe56150 | 108 | TCPSocketConnection socket; |
| mbedAustin | 32:67402fe56150 | 109 | while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { |
| mbedAustin | 32:67402fe56150 | 110 | printf("Unable to connect to (%s) on port (%d)\n\r", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
| mbedAustin | 32:67402fe56150 | 111 | wait(1); |
| mbedAustin | 30:de5a32932408 | 112 | } |
| mbedAustin | 30:de5a32932408 | 113 | |
| mbedAustin | 32:67402fe56150 | 114 | // send messages back and forth as long as we are connected |
| mbedAustin | 32:67402fe56150 | 115 | int x = 0; |
| mbedAustin | 32:67402fe56150 | 116 | while(socket.is_connected()) { |
| mbedAustin | 32:67402fe56150 | 117 | // Setup & send message to server |
| mbedAustin | 32:67402fe56150 | 118 | char message[64] = {}; |
| mbedAustin | 32:67402fe56150 | 119 | sprintf(message,"%s%d","HelloFromMicrocontroller: ",x++); |
| mbedAustin | 32:67402fe56150 | 120 | socket.send_all(message, sizeof(message) - 1); |
| mbedAustin | 30:de5a32932408 | 121 | |
| mbedAustin | 32:67402fe56150 | 122 | // Receive & print message from server |
| mbedAustin | 32:67402fe56150 | 123 | char buf[256]; |
| mbedAustin | 32:67402fe56150 | 124 | int n = socket.receive_all(buf, 256); |
| mbedAustin | 32:67402fe56150 | 125 | buf[n] = '\0'; |
| mbedAustin | 32:67402fe56150 | 126 | printf("TCP data from server: '%s'", buf); |
| mbedAustin | 32:67402fe56150 | 127 | |
| mbedAustin | 32:67402fe56150 | 128 | // not needed, here to slow the data down so you can see it whip past. |
| mbedAustin | 32:67402fe56150 | 129 | wait(0.5); |
| mbedAustin | 30:de5a32932408 | 130 | } |
| mbedAustin | 30:de5a32932408 | 131 | |
| mbedAustin | 32:67402fe56150 | 132 | socket.close(); |
| mbedAustin | 32:67402fe56150 | 133 | wifi.disconnect(); |
| mbedAustin | 28:174412ff9671 | 134 | |
| mbedAustin | 32:67402fe56150 | 135 | while(true) {} |
| mbedAustin | 32:67402fe56150 | 136 | |
| Ilya Dmitrichenko |
6:9e4f4a8c1829 | 137 | } |
