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 May 27 01:36:56 2014 +0000
Revision:
15:abc12b228291
Parent:
14:6d58d3855feb
Child:
16:ed9b9c28f860
Adjusted of parameters for Xively demo for using SNIC UART.

Who changed what in which revision?

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