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:
kishino
Date:
Tue Jul 15 02:08:48 2014 +0000
Revision:
21:25b85cbbdd82
Parent:
20:f0c7f5ca7e8a
Child:
22:e567f0d4b05d
The pointing out of in-house reviews was modified.

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 21:25b85cbbdd82 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
kishino 14:6d58d3855feb 22 #define XI_FEED_ID 1056160623 // set Xively Feed ID (numerical, no quoutes)
kishino 14:6d58d3855feb 23 #define XI_API_KEY "Wg7CfZDrj7VjIIpiYzdDrMow6wdENAOGjkIfQ0fUjJh6DAw2" // set Xively API key (double-quoted string)
errordeveloper 10:86ffba646df1 24
errordeveloper 10:86ffba646df1 25 #include "app_board_io.h"
xively 7:0eff5db44b8b 26
xively 0:efdea27c3b81 27 #include "xively.h"
xively 0:efdea27c3b81 28 #include "xi_err.h"
xively 0:efdea27c3b81 29
errordeveloper 10:86ffba646df1 30 #include "MMA7660.h"
errordeveloper 10:86ffba646df1 31 #include "LM75B.h"
errordeveloper 11:bdf601a405fc 32 #include "C12832_lcd.h"
errordeveloper 10:86ffba646df1 33
kishino 16:ed9b9c28f860 34 #include "PowerControl/EthernetPowerControl.h"
kishino 16:ed9b9c28f860 35
xively 0:efdea27c3b81 36 MMA7660 axl(p28, p27);
xively 3:7ad3f6543b6e 37 LM75B tmp(p28, p27);
errordeveloper 11:bdf601a405fc 38 C12832_LCD lcd;
errordeveloper 11:bdf601a405fc 39
errordeveloper 11:bdf601a405fc 40 #include "logo.h"
xively 0:efdea27c3b81 41
kishino 15:abc12b228291 42 #define DEMO_AP_SSID "E2N1-Lab-Buffalo-D302"
kishino 17:0bf3c49a83d5 43 #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES
kishino 17:0bf3c49a83d5 44 #define DEMO_AP_SECUTIRY_KEY "12345678"
kishino 17:0bf3c49a83d5 45 #define DEMO_AP_SECUTIRY_KEY_LEN 8
kishino 19:4e2900daad59 46
kishino 14:6d58d3855feb 47 /** Wi-Fi SNIC UART Interface*/
kishino 14:6d58d3855feb 48 C_SNIC_WifiInterface mSNICwifi( p9, p10, NC, NC, p30 );
kishino 14:6d58d3855feb 49 Serial pc(USBTX, USBRX);
kishino 17:0bf3c49a83d5 50
xively 0:efdea27c3b81 51 int main() {
kishino 16:ed9b9c28f860 52
kishino 16:ed9b9c28f860 53 PHY_PowerDown();
kishino 16:ed9b9c28f860 54
kishino 14:6d58d3855feb 55 pc.baud( 115200 );
kishino 14:6d58d3855feb 56 printf("main\r\n");
errordeveloper 11:bdf601a405fc 57 lcd_print_xively_logo();
xively 0:efdea27c3b81 58
kishino 14:6d58d3855feb 59 // Initialize Wi-Fi interface
kishino 14:6d58d3855feb 60 int s = mSNICwifi.init();
xively 0:efdea27c3b81 61
kishino 14:6d58d3855feb 62 lcd_printf("init();\r\n");
kishino 14:6d58d3855feb 63
kishino 14:6d58d3855feb 64 if( s != 0 )
xively 0:efdea27c3b81 65 {
xively 7:0eff5db44b8b 66 lcd_printf( "Could not initialise. Will halt!\n" );
kishino 14:6d58d3855feb 67 return -1;
xively 0:efdea27c3b81 68 }
kishino 20:f0c7f5ca7e8a 69 wait(0.5);
kishino 20:f0c7f5ca7e8a 70 s = mSNICwifi.disconnect();
kishino 14:6d58d3855feb 71 lcd_printf("disconnect();\r\n");
kishino 20:f0c7f5ca7e8a 72 if( s != 0 )
kishino 20:f0c7f5ca7e8a 73 {
kishino 20:f0c7f5ca7e8a 74 printf( "disconnect failed\r\n" );
kishino 20:f0c7f5ca7e8a 75 return -1;
kishino 20:f0c7f5ca7e8a 76 }
xively 0:efdea27c3b81 77
kishino 19:4e2900daad59 78 wait(0.3);
kishino 14:6d58d3855feb 79 // Connect AP
kishino 19:4e2900daad59 80 mSNICwifi.connect( DEMO_AP_SSID
kishino 14:6d58d3855feb 81 , strlen(DEMO_AP_SSID)
kishino 14:6d58d3855feb 82 , DEMO_AP_SECURITY_TYPE
kishino 14:6d58d3855feb 83 , DEMO_AP_SECUTIRY_KEY
kishino 14:6d58d3855feb 84 , DEMO_AP_SECUTIRY_KEY_LEN );
kishino 14:6d58d3855feb 85 lcd_printf("connect();\r\n");
kishino 19:4e2900daad59 86 wait(0.5);
kishino 19:4e2900daad59 87
kishino 19:4e2900daad59 88 lcd_printf("IP Config();\r\n");
kishino 19:4e2900daad59 89 mSNICwifi.setIPConfig( true );
kishino 19:4e2900daad59 90
kishino 14:6d58d3855feb 91 wait(0.5);
kishino 14:6d58d3855feb 92
xively 0:efdea27c3b81 93 xi_feed_t feed;
xively 0:efdea27c3b81 94 memset( &feed, NULL, sizeof( xi_feed_t ) );
xively 0:efdea27c3b81 95
xively 0:efdea27c3b81 96 feed.feed_id = XI_FEED_ID;
xively 3:7ad3f6543b6e 97 feed.datastream_count = 3;
xively 0:efdea27c3b81 98
xively 0:efdea27c3b81 99 feed.datastreams[0].datapoint_count = 1;
xively 0:efdea27c3b81 100 xi_datastream_t* orientation_datastream = &feed.datastreams[0];
xively 0:efdea27c3b81 101 strcpy( orientation_datastream->datastream_id, "orientation" );
xively 0:efdea27c3b81 102 xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
xively 0:efdea27c3b81 103
xively 0:efdea27c3b81 104 feed.datastreams[1].datapoint_count = 1;
xively 0:efdea27c3b81 105 xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
xively 0:efdea27c3b81 106 strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
xively 0:efdea27c3b81 107 xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
xively 0:efdea27c3b81 108
xively 3:7ad3f6543b6e 109 feed.datastreams[2].datapoint_count = 1;
xively 3:7ad3f6543b6e 110 xi_datastream_t* temperature_datastream = &feed.datastreams[2];
xively 3:7ad3f6543b6e 111 strcpy( temperature_datastream->datastream_id, "temperature" );
xively 3:7ad3f6543b6e 112 xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
xively 3:7ad3f6543b6e 113
xively 0:efdea27c3b81 114 // create the cosm library context
xively 0:efdea27c3b81 115 xi_context_t* xi_context
xively 0:efdea27c3b81 116 = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
xively 0:efdea27c3b81 117
xively 0:efdea27c3b81 118 // check if everything works
xively 0:efdea27c3b81 119 if( xi_context == NULL )
xively 0:efdea27c3b81 120 {
xively 0:efdea27c3b81 121 return -1;
xively 0:efdea27c3b81 122 }
xively 0:efdea27c3b81 123
xively 0:efdea27c3b81 124 while(1) {
xively 0:efdea27c3b81 125 switch( axl.getSide() ) {
xively 0:efdea27c3b81 126 case MMA7660::Front:
xively 0:efdea27c3b81 127 xi_set_value_str( current_side_rotation, "front" );
xively 0:efdea27c3b81 128 break;
xively 0:efdea27c3b81 129 case MMA7660::Back:
xively 0:efdea27c3b81 130 xi_set_value_str( current_side_rotation, "back" );
xively 0:efdea27c3b81 131 break;
xively 0:efdea27c3b81 132 default:
xively 0:efdea27c3b81 133 xi_set_value_str( current_side_rotation, "unknown" );
xively 0:efdea27c3b81 134 break;
xively 0:efdea27c3b81 135 }
xively 0:efdea27c3b81 136
xively 0:efdea27c3b81 137 switch( axl.getOrientation() ) {
xively 0:efdea27c3b81 138 case MMA7660::Down:
xively 0:efdea27c3b81 139 xi_set_value_str( current_orientation, "down" );
xively 0:efdea27c3b81 140 break;
xively 0:efdea27c3b81 141 case MMA7660::Up:
xively 0:efdea27c3b81 142 xi_set_value_str( current_orientation, "up" );
xively 0:efdea27c3b81 143 break;
xively 0:efdea27c3b81 144 case MMA7660::Right:
xively 0:efdea27c3b81 145 xi_set_value_str( current_orientation, "right" );
xively 0:efdea27c3b81 146 break;
xively 0:efdea27c3b81 147 case MMA7660::Left:
xively 0:efdea27c3b81 148 xi_set_value_str( current_orientation, "left" );
xively 0:efdea27c3b81 149 break;
xively 0:efdea27c3b81 150 default:
xively 0:efdea27c3b81 151 xi_set_value_str( current_orientation, "unknown" );
xively 0:efdea27c3b81 152 break;
xively 0:efdea27c3b81 153 }
xively 3:7ad3f6543b6e 154
xively 3:7ad3f6543b6e 155 xi_set_value_f32( current_temperature, tmp.read() );
xively 4:e7ca62a11595 156
xively 7:0eff5db44b8b 157 lcd_printf( "update...\n" );
errordeveloper 11:bdf601a405fc 158 xi_feed_update( xi_context, &feed );
xively 7:0eff5db44b8b 159 lcd_printf( "done...\n" );
xively 3:7ad3f6543b6e 160
kishino 14:6d58d3855feb 161 wait( 1.0 );
xively 0:efdea27c3b81 162 }
Ilya Dmitrichenko 6:9e4f4a8c1829 163 }