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 09 01:28:01 2015 +0000
Revision:
31:c42d189364b4
Parent:
30:de5a32932408
Child:
32:67402fe56150
Initial rev of murata demo, all API's are used and broken out.

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