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 10:13:57 2014 +0000
Revision:
23:39cf9f03b076
Parent:
22:e567f0d4b05d
Child:
24:3d150ff59276
Modified the module name of Copyright.

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