demo of Murata wifi chip as TCP client.

Dependencies:   SNICInterface mbed-rtos mbed

Fork of murataDemo by Austin Blackstone

Intro

this program demonstrates how to use TCP on the Murata Wifi chip. It will connect to a server and send a message, the server will then send a reply. The reply will be printed out to the terminal on the microcontroller.

Instructions

  1. Make sure you have both the wifi device and the computer running the server on the same network / wifi router.
  2. Change the hard coded IP in the microcontroller code to match that of the laptop running the python server.
  3. Run the python2 script below on the computer
  4. Have a console hooked up to the microcontroller and watch as messages are sent back and forth between the server (python) and the client (murata).
  5. Run the microcontroller code on the device.

For ease of use numbers have been appended to the end of the messages being sent back and forth.

Python Server

Please run this python2.7 code on your computer. Make sure to change the IP Address in the microcontroller code to match the IP of your computer.

import socket
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 7))
s.listen(1)
 
x = 0
while True:
    conn, addr = s.accept()
    print 'Connected b'TCP data from server: 'y', addr
    while True:
        # receive data from board
        data = conn.recv(1024)
        
        # check received data
        if not data: 
            break
        
        # print received data 
        print("TCP data from microcontroller: '"+data+"'")
        
        # send data to board with counter to differentiate messages
        conn.sendall("HelloFromPython!: "+str(x)+"\n\r")
        x+=1

    # close the port
    conn.close()

Committer:
mbedAustin
Date:
Thu Apr 02 06:03:05 2015 +0000
Revision:
30:de5a32932408
Parent:
29:9f08c7152c7a
Child:
31:c42d189364b4
got all API's working, need to cleanup

Who changed what in which revision?

UserRevisionLine numberNew 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"
xively 7:0eff5db44b8b 21
mbedAustin 28:174412ff9671 22 Serial pc(USBTX, USBRX);
xively 7:0eff5db44b8b 23
errordeveloper 10:86ffba646df1 24
mbedAustin 29:9f08c7152c7a 25 #define WIFI_SSID "demossid"
mbedAustin 29:9f08c7152c7a 26 #define WIFI_SECURITY_TYPE e_SEC_OPEN
mbedAustin 28:174412ff9671 27 #define WIFI_SECUTIRY_KEY "WPA2_PASSPHRASE"
mbedAustin 28:174412ff9671 28 #define WIFI_SECUTIRY_KEY_LEN 15
errordeveloper 11:bdf601a405fc 29
mbedAustin 29:9f08c7152c7a 30 //#define WIFI_SSID "AP_SSID"
mbedAustin 29:9f08c7152c7a 31 //#define WIFI_SECURITY_TYPE e_SEC_WPA2_AES
mbedAustin 29:9f08c7152c7a 32 //#define WIFI_SECUTIRY_KEY "WPA2_PASSPHRASE"
mbedAustin 29:9f08c7152c7a 33 //#define WIFI_SECUTIRY_KEY_LEN 15
mbedAustin 29:9f08c7152c7a 34
mbedAustin 29:9f08c7152c7a 35
mbedAustin 28:174412ff9671 36 C_SNIC_WifiInterface wifi( D1, D0, NC, NC, D3 );
kishino 17:0bf3c49a83d5 37
mbedAustin 30:de5a32932408 38
mbedAustin 30:de5a32932408 39 void scanCallbackFn(tagSCAN_RESULT_T *scan_result)
mbedAustin 30:de5a32932408 40 {
mbedAustin 30:de5a32932408 41 printf("\r\n");
mbedAustin 30:de5a32932408 42 printf("channel = %d \r\n" ,scan_result->channel);
mbedAustin 30:de5a32932408 43 printf("rssi = %d \r\n" ,scan_result->rssi);
mbedAustin 30:de5a32932408 44 printf("security = %d \r\n" ,scan_result->security);
mbedAustin 30:de5a32932408 45 printf("bssid = %x%x%x%x%x%x\r\n",scan_result->bssid[0],scan_result->bssid[1],scan_result->bssid[2],scan_result->bssid[3],scan_result->bssid[4],scan_result->bssid[5]);
mbedAustin 30:de5a32932408 46 printf("network_type = %d \r\n" ,scan_result->network_type);
mbedAustin 30:de5a32932408 47 printf("max_rate = %d \r\n" ,scan_result->max_rate);
mbedAustin 30:de5a32932408 48 printf("ssid = %s \r\n" ,scan_result->ssid);
mbedAustin 30:de5a32932408 49 }
mbedAustin 30:de5a32932408 50
mbedAustin 30:de5a32932408 51
mbedAustin 28:174412ff9671 52 int main()
mbedAustin 28:174412ff9671 53 {
mbedAustin 28:174412ff9671 54 // for built in debug printouts
mbedAustin 29:9f08c7152c7a 55 // pc.baud( 115200 );
mbedAustin 29:9f08c7152c7a 56
mbedAustin 29:9f08c7152c7a 57 int check = 0;
mbedAustin 28:174412ff9671 58
kishino 14:6d58d3855feb 59 // Initialize Wi-Fi interface
mbedAustin 29:9f08c7152c7a 60 check = wifi.init();
mbedAustin 29:9f08c7152c7a 61 if( check != 0 ) {
mbedAustin 28:174412ff9671 62 printf( "Wifi could not be initialized, halting.\r\n" );
kishino 14:6d58d3855feb 63 return -1;
mbedAustin 29:9f08c7152c7a 64 } else {
mbedAustin 28:174412ff9671 65 printf("wifi initialized successfully!\r\n");
mbedAustin 28:174412ff9671 66 }
kishino 19:4e2900daad59 67 wait(0.5);
mbedAustin 29:9f08c7152c7a 68
mbedAustin 28:174412ff9671 69 // good form to make sure you are disconnected from all AP's
mbedAustin 29:9f08c7152c7a 70 check = wifi.disconnect();
mbedAustin 29:9f08c7152c7a 71 if( check != 0 ) {
mbedAustin 28:174412ff9671 72 printf( "disconnect failed\r\n" );
mbedAustin 28:174412ff9671 73 return -1;
mbedAustin 29:9f08c7152c7a 74 } else {
mbedAustin 28:174412ff9671 75 printf("disconnection successful!\r\n");
mbedAustin 29:9f08c7152c7a 76 }
mbedAustin 28:174412ff9671 77 wait(0.3);
mbedAustin 29:9f08c7152c7a 78
mbedAustin 28:174412ff9671 79 // Connect AP
mbedAustin 29:9f08c7152c7a 80 check = wifi.connect( WIFI_SSID
mbedAustin 29:9f08c7152c7a 81 , strlen(WIFI_SSID)
mbedAustin 29:9f08c7152c7a 82 , WIFI_SECURITY_TYPE
mbedAustin 29:9f08c7152c7a 83 , WIFI_SECUTIRY_KEY
mbedAustin 29:9f08c7152c7a 84 , WIFI_SECUTIRY_KEY_LEN );
mbedAustin 29:9f08c7152c7a 85 if( check != 0) {
mbedAustin 29:9f08c7152c7a 86 printf("Connect Failed\r\n");
mbedAustin 29:9f08c7152c7a 87 } else {
mbedAustin 29:9f08c7152c7a 88 printf("connected successfully!\r\n");
mbedAustin 29:9f08c7152c7a 89 }
kishino 14:6d58d3855feb 90 wait(0.5);
kishino 14:6d58d3855feb 91
mbedAustin 29:9f08c7152c7a 92 // Get DHCP IP
mbedAustin 29:9f08c7152c7a 93 check = wifi.setIPConfig( true ); // get IP as DHCP IP
mbedAustin 29:9f08c7152c7a 94 if(check != 0) {
mbedAustin 29:9f08c7152c7a 95 printf("SetIPConfig failed \r\n");
mbedAustin 29:9f08c7152c7a 96 } else {
mbedAustin 29:9f08c7152c7a 97 printf("SetIPConfig successful \r\n");
mbedAustin 29:9f08c7152c7a 98 }
mbedAustin 30:de5a32932408 99
mbedAustin 29:9f08c7152c7a 100 // Get RSSI
mbedAustin 29:9f08c7152c7a 101 signed char temp = 0;
mbedAustin 29:9f08c7152c7a 102 check = wifi.getRssi(&temp);
mbedAustin 30:de5a32932408 103 if(check != 0) {
mbedAustin 29:9f08c7152c7a 104 printf("getRssi failed. \r\n");
mbedAustin 30:de5a32932408 105 } else {
mbedAustin 29:9f08c7152c7a 106 printf("getRssi success: %d \r\n",temp);
mbedAustin 29:9f08c7152c7a 107 }
mbedAustin 30:de5a32932408 108
mbedAustin 30:de5a32932408 109 // check IP Address
mbedAustin 30:de5a32932408 110 char * ip ;
mbedAustin 30:de5a32932408 111 ip = wifi.getIPAddress();
mbedAustin 30:de5a32932408 112 if(ip == 0) {
mbedAustin 30:de5a32932408 113 printf("getIP failed. \r\n");
mbedAustin 30:de5a32932408 114 } else {
mbedAustin 30:de5a32932408 115 printf("getIP success: %s \r\n",ip);
mbedAustin 30:de5a32932408 116 }
mbedAustin 30:de5a32932408 117
mbedAustin 30:de5a32932408 118 // get wifi status
mbedAustin 30:de5a32932408 119 tagWIFI_STATUS_T status;
mbedAustin 30:de5a32932408 120 check = wifi.getWifiStatus(&status);
mbedAustin 30:de5a32932408 121 if(check != 0) {
mbedAustin 30:de5a32932408 122 printf("getWifiStatus failed \r\n");
mbedAustin 30:de5a32932408 123 } else {
mbedAustin 30:de5a32932408 124 // Status 0=WifiOff, 1=No Network, 2=Connected to AP, 3=Started AP mode
mbedAustin 30:de5a32932408 125 printf("getWifiStatus success: status =%d, MAC = %x%x%x%x%x%x, SSID = %s \r\n",
mbedAustin 30:de5a32932408 126 status.status,
mbedAustin 30:de5a32932408 127 status.mac_address[0], status.mac_address[1],
mbedAustin 30:de5a32932408 128 status.mac_address[2], status.mac_address[3],
mbedAustin 30:de5a32932408 129 status.mac_address[4], status.mac_address[5],
mbedAustin 30:de5a32932408 130 status.ssid );
mbedAustin 30:de5a32932408 131 }
mbedAustin 30:de5a32932408 132
mbedAustin 30:de5a32932408 133 // scan for wifi
mbedAustin 30:de5a32932408 134 check = wifi.scan(NULL,NULL,scanCallbackFn);
mbedAustin 30:de5a32932408 135 if(check != 0) {
mbedAustin 30:de5a32932408 136 printf("scan failed! \r\n");
mbedAustin 30:de5a32932408 137 } else {
mbedAustin 30:de5a32932408 138 printf("Scan Success! \r\n");
mbedAustin 30:de5a32932408 139
mbedAustin 30:de5a32932408 140 }
mbedAustin 30:de5a32932408 141
mbedAustin 29:9f08c7152c7a 142 // if(check != 0){
mbedAustin 29:9f08c7152c7a 143 // printf(" \r\n");
mbedAustin 29:9f08c7152c7a 144 // }else{
mbedAustin 29:9f08c7152c7a 145 // printf(" \r\n");
mbedAustin 29:9f08c7152c7a 146 // }
mbedAustin 28:174412ff9671 147
mbedAustin 28:174412ff9671 148 wait( 1.0 );
Ilya Dmitrichenko 6:9e4f4a8c1829 149 }