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:
xively
Date:
Mon May 13 19:30:39 2013 +0000
Revision:
0:efdea27c3b81
Child:
1:0a61d7ab702c
Xively Jumpstart Demo (v1)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xively 0:efdea27c3b81 1 #include "mbed.h"
xively 0:efdea27c3b81 2 #include "EthernetInterface.h"
xively 0:efdea27c3b81 3 #include "xively.h"
xively 0:efdea27c3b81 4 #include "xi_err.h"
xively 0:efdea27c3b81 5 #include "xi_printf.h"
xively 0:efdea27c3b81 6
xively 0:efdea27c3b81 7 #include <stdarg.h>
xively 0:efdea27c3b81 8 #include <stdio.h>
xively 0:efdea27c3b81 9
xively 0:efdea27c3b81 10 #include "MMA7660.h"
xively 0:efdea27c3b81 11
xively 0:efdea27c3b81 12 #define XI_FEED_ID 128488
xively 0:efdea27c3b81 13 #define XI_API_KEY "T4KXAH_gw1PW42RjRdhU-lgmCC6SAKw3NnBPc3dydUc4ND0g"
xively 0:efdea27c3b81 14
xively 0:efdea27c3b81 15 #include "C12832_lcd.h"
xively 0:efdea27c3b81 16
xively 0:efdea27c3b81 17 C12832_LCD lcd;
xively 0:efdea27c3b81 18
xively 0:efdea27c3b81 19 MMA7660 axl(p28, p27);
xively 0:efdea27c3b81 20
xively 0:efdea27c3b81 21 extern "C" {
xively 0:efdea27c3b81 22
xively 0:efdea27c3b81 23 void user_printf( const char* buffer )
xively 0:efdea27c3b81 24 {
xively 0:efdea27c3b81 25 lcd.cls();
xively 0:efdea27c3b81 26 lcd.locate( 0, 3 );
xively 0:efdea27c3b81 27 lcd.printf( buffer );
xively 0:efdea27c3b81 28 //wait( 1.0 );
xively 0:efdea27c3b81 29 }
xively 0:efdea27c3b81 30
xively 0:efdea27c3b81 31 void mbed_printf( const char* fmt, ... )
xively 0:efdea27c3b81 32 {
xively 0:efdea27c3b81 33 char buffer[ 64 ];
xively 0:efdea27c3b81 34
xively 0:efdea27c3b81 35 va_list ap;
xively 0:efdea27c3b81 36 va_start( ap, fmt );
xively 0:efdea27c3b81 37 vsnprintf( buffer, 64, fmt, ap );
xively 0:efdea27c3b81 38 va_end( ap );
xively 0:efdea27c3b81 39
xively 0:efdea27c3b81 40 user_printf( buffer );
xively 0:efdea27c3b81 41 }
xively 0:efdea27c3b81 42
xively 0:efdea27c3b81 43 }
xively 0:efdea27c3b81 44
xively 0:efdea27c3b81 45 int main() {
xively 0:efdea27c3b81 46 // set our device specific print function
xively 0:efdea27c3b81 47 USER_PRINT = user_printf;
xively 0:efdea27c3b81 48
xively 0:efdea27c3b81 49 EthernetInterface eth;
xively 0:efdea27c3b81 50
xively 0:efdea27c3b81 51 int s = eth.init(); //Use DHCP
xively 0:efdea27c3b81 52
xively 0:efdea27c3b81 53 if( s != NULL )
xively 0:efdea27c3b81 54 {
xively 0:efdea27c3b81 55 mbed_printf( "Could not initialise. Will halt!\n" );
xively 0:efdea27c3b81 56 exit( 0 );
xively 0:efdea27c3b81 57 }
xively 0:efdea27c3b81 58
xively 0:efdea27c3b81 59 s = eth.connect();
xively 0:efdea27c3b81 60
xively 0:efdea27c3b81 61 if( s != NULL )
xively 0:efdea27c3b81 62 {
xively 0:efdea27c3b81 63 mbed_printf( "Could not connect. Will halt!\n" );
xively 0:efdea27c3b81 64 exit( 0 );
xively 0:efdea27c3b81 65 }
xively 0:efdea27c3b81 66 else
xively 0:efdea27c3b81 67 {
xively 0:efdea27c3b81 68 mbed_printf( "IP: %s\n", eth.getIPAddress() );
xively 0:efdea27c3b81 69 }
xively 0:efdea27c3b81 70
xively 0:efdea27c3b81 71 xi_feed_t feed;
xively 0:efdea27c3b81 72 memset( &feed, NULL, sizeof( xi_feed_t ) );
xively 0:efdea27c3b81 73
xively 0:efdea27c3b81 74 feed.feed_id = XI_FEED_ID;
xively 0:efdea27c3b81 75 feed.datastream_count = 2;
xively 0:efdea27c3b81 76
xively 0:efdea27c3b81 77 feed.datastreams[0].datapoint_count = 1;
xively 0:efdea27c3b81 78 xi_datastream_t* orientation_datastream = &feed.datastreams[0];
xively 0:efdea27c3b81 79 strcpy( orientation_datastream->datastream_id, "orientation" );
xively 0:efdea27c3b81 80 xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
xively 0:efdea27c3b81 81
xively 0:efdea27c3b81 82 feed.datastreams[1].datapoint_count = 1;
xively 0:efdea27c3b81 83 xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
xively 0:efdea27c3b81 84 strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
xively 0:efdea27c3b81 85 xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
xively 0:efdea27c3b81 86
xively 0:efdea27c3b81 87 // create the cosm library context
xively 0:efdea27c3b81 88 xi_context_t* xi_context
xively 0:efdea27c3b81 89 = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
xively 0:efdea27c3b81 90
xively 0:efdea27c3b81 91 // check if everything works
xively 0:efdea27c3b81 92 if( xi_context == NULL )
xively 0:efdea27c3b81 93 {
xively 0:efdea27c3b81 94 return -1;
xively 0:efdea27c3b81 95 }
xively 0:efdea27c3b81 96
xively 0:efdea27c3b81 97 mbed_printf("feed:%d datastreams:[%s,%s]\n", feed.feed_id,
xively 0:efdea27c3b81 98 orientation_datastream->datastream_id,
xively 0:efdea27c3b81 99 side_rotation_datastream->datastream_id);
xively 0:efdea27c3b81 100
xively 0:efdea27c3b81 101 while(1) {
xively 0:efdea27c3b81 102
xively 0:efdea27c3b81 103 switch( axl.getSide() ) {
xively 0:efdea27c3b81 104 case MMA7660::Front:
xively 0:efdea27c3b81 105 xi_set_value_str( current_side_rotation, "front" );
xively 0:efdea27c3b81 106 break;
xively 0:efdea27c3b81 107 case MMA7660::Back:
xively 0:efdea27c3b81 108 xi_set_value_str( current_side_rotation, "back" );
xively 0:efdea27c3b81 109 break;
xively 0:efdea27c3b81 110 default:
xively 0:efdea27c3b81 111 xi_set_value_str( current_side_rotation, "unknown" );
xively 0:efdea27c3b81 112 break;
xively 0:efdea27c3b81 113 }
xively 0:efdea27c3b81 114
xively 0:efdea27c3b81 115 switch( axl.getOrientation() ) {
xively 0:efdea27c3b81 116 case MMA7660::Down:
xively 0:efdea27c3b81 117 mbed_printf("down %s\n",
xively 0:efdea27c3b81 118 (axl.getSide() == MMA7660::Front ? "front" : "back"));
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 mbed_printf("up %s\n",
xively 0:efdea27c3b81 123 (axl.getSide() == MMA7660::Front ? "front" : "back"));
xively 0:efdea27c3b81 124 xi_set_value_str( current_orientation, "up" );
xively 0:efdea27c3b81 125 break;
xively 0:efdea27c3b81 126 case MMA7660::Right:
xively 0:efdea27c3b81 127 mbed_printf("right %s\n",
xively 0:efdea27c3b81 128 (axl.getSide() == MMA7660::Front ? "front" : "back"));
xively 0:efdea27c3b81 129 xi_set_value_str( current_orientation, "right" );
xively 0:efdea27c3b81 130 break;
xively 0:efdea27c3b81 131 case MMA7660::Left:
xively 0:efdea27c3b81 132 mbed_printf("left %s\n",
xively 0:efdea27c3b81 133 (axl.getSide() == MMA7660::Front ? "front" : "back"));
xively 0:efdea27c3b81 134 xi_set_value_str( current_orientation, "left" );
xively 0:efdea27c3b81 135 break;
xively 0:efdea27c3b81 136 default:
xively 0:efdea27c3b81 137 xi_set_value_str( current_orientation, "unknown" );
xively 0:efdea27c3b81 138 break;
xively 0:efdea27c3b81 139 }
xively 0:efdea27c3b81 140
xively 0:efdea27c3b81 141 mbed_printf( "update...\n" );
xively 0:efdea27c3b81 142 xi_feed_update(xi_context, &feed);
xively 0:efdea27c3b81 143 mbed_printf( "done...\n" );
xively 0:efdea27c3b81 144 }
xively 0:efdea27c3b81 145 }