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:
errordeveloper
Date:
Mon Oct 14 13:33:04 2013 +0000
Revision:
11:bdf601a405fc
Parent:
10:86ffba646df1
Child:
12:27471bb09274
Child:
14:6d58d3855feb
Improve a few things about the LCD output:; ; * don't output debug logs; * show Xively logo on boot;

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 7:0eff5db44b8b 3
errordeveloper 11:bdf601a405fc 4 #define XI_FEED_ID 128488 // set Xively Feed ID (numerical, no quoutes)
errordeveloper 10:86ffba646df1 5 #define XI_API_KEY "T4KXAH_dasgw1PWBPc3fdsfsdgsdy-dUc4ND0g" // 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
xively 0:efdea27c3b81 22 int main() {
errordeveloper 11:bdf601a405fc 23 lcd_print_xively_logo();
xively 0:efdea27c3b81 24 EthernetInterface eth;
xively 0:efdea27c3b81 25
xively 0:efdea27c3b81 26 int s = eth.init(); //Use DHCP
xively 0:efdea27c3b81 27
xively 0:efdea27c3b81 28 if( s != NULL )
xively 0:efdea27c3b81 29 {
xively 7:0eff5db44b8b 30 lcd_printf( "Could not initialise. Will halt!\n" );
xively 0:efdea27c3b81 31 exit( 0 );
xively 0:efdea27c3b81 32 }
xively 0:efdea27c3b81 33
xively 0:efdea27c3b81 34 s = eth.connect();
xively 0:efdea27c3b81 35
xively 0:efdea27c3b81 36 if( s != NULL )
xively 0:efdea27c3b81 37 {
xively 7:0eff5db44b8b 38 lcd_printf( "Could not connect. Will halt!\n" );
xively 0:efdea27c3b81 39 exit( 0 );
xively 0:efdea27c3b81 40 }
xively 0:efdea27c3b81 41 else
xively 0:efdea27c3b81 42 {
xively 7:0eff5db44b8b 43 lcd_printf( "IP: %s\n", eth.getIPAddress() );
xively 0:efdea27c3b81 44 }
xively 0:efdea27c3b81 45
xively 0:efdea27c3b81 46 xi_feed_t feed;
xively 0:efdea27c3b81 47 memset( &feed, NULL, sizeof( xi_feed_t ) );
xively 0:efdea27c3b81 48
xively 0:efdea27c3b81 49 feed.feed_id = XI_FEED_ID;
xively 3:7ad3f6543b6e 50 feed.datastream_count = 3;
xively 0:efdea27c3b81 51
xively 0:efdea27c3b81 52 feed.datastreams[0].datapoint_count = 1;
xively 0:efdea27c3b81 53 xi_datastream_t* orientation_datastream = &feed.datastreams[0];
xively 0:efdea27c3b81 54 strcpy( orientation_datastream->datastream_id, "orientation" );
xively 0:efdea27c3b81 55 xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
xively 0:efdea27c3b81 56
xively 0:efdea27c3b81 57 feed.datastreams[1].datapoint_count = 1;
xively 0:efdea27c3b81 58 xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
xively 0:efdea27c3b81 59 strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
xively 0:efdea27c3b81 60 xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
xively 0:efdea27c3b81 61
xively 3:7ad3f6543b6e 62 feed.datastreams[2].datapoint_count = 1;
xively 3:7ad3f6543b6e 63 xi_datastream_t* temperature_datastream = &feed.datastreams[2];
xively 3:7ad3f6543b6e 64 strcpy( temperature_datastream->datastream_id, "temperature" );
xively 3:7ad3f6543b6e 65 xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
xively 3:7ad3f6543b6e 66
xively 0:efdea27c3b81 67 // create the cosm library context
xively 0:efdea27c3b81 68 xi_context_t* xi_context
xively 0:efdea27c3b81 69 = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
xively 0:efdea27c3b81 70
xively 0:efdea27c3b81 71 // check if everything works
xively 0:efdea27c3b81 72 if( xi_context == NULL )
xively 0:efdea27c3b81 73 {
xively 0:efdea27c3b81 74 return -1;
xively 0:efdea27c3b81 75 }
xively 0:efdea27c3b81 76
xively 0:efdea27c3b81 77 while(1) {
xively 0:efdea27c3b81 78
xively 0:efdea27c3b81 79 switch( axl.getSide() ) {
xively 0:efdea27c3b81 80 case MMA7660::Front:
xively 0:efdea27c3b81 81 xi_set_value_str( current_side_rotation, "front" );
xively 0:efdea27c3b81 82 break;
xively 0:efdea27c3b81 83 case MMA7660::Back:
xively 0:efdea27c3b81 84 xi_set_value_str( current_side_rotation, "back" );
xively 0:efdea27c3b81 85 break;
xively 0:efdea27c3b81 86 default:
xively 0:efdea27c3b81 87 xi_set_value_str( current_side_rotation, "unknown" );
xively 0:efdea27c3b81 88 break;
xively 0:efdea27c3b81 89 }
xively 0:efdea27c3b81 90
xively 0:efdea27c3b81 91 switch( axl.getOrientation() ) {
xively 0:efdea27c3b81 92 case MMA7660::Down:
xively 0:efdea27c3b81 93 xi_set_value_str( current_orientation, "down" );
xively 0:efdea27c3b81 94 break;
xively 0:efdea27c3b81 95 case MMA7660::Up:
xively 0:efdea27c3b81 96 xi_set_value_str( current_orientation, "up" );
xively 0:efdea27c3b81 97 break;
xively 0:efdea27c3b81 98 case MMA7660::Right:
xively 0:efdea27c3b81 99 xi_set_value_str( current_orientation, "right" );
xively 0:efdea27c3b81 100 break;
xively 0:efdea27c3b81 101 case MMA7660::Left:
xively 0:efdea27c3b81 102 xi_set_value_str( current_orientation, "left" );
xively 0:efdea27c3b81 103 break;
xively 0:efdea27c3b81 104 default:
xively 0:efdea27c3b81 105 xi_set_value_str( current_orientation, "unknown" );
xively 0:efdea27c3b81 106 break;
xively 0:efdea27c3b81 107 }
xively 3:7ad3f6543b6e 108
xively 3:7ad3f6543b6e 109 xi_set_value_f32( current_temperature, tmp.read() );
xively 4:e7ca62a11595 110
xively 7:0eff5db44b8b 111 lcd_printf( "update...\n" );
errordeveloper 11:bdf601a405fc 112 xi_feed_update( xi_context, &feed );
xively 7:0eff5db44b8b 113 lcd_printf( "done...\n" );
xively 3:7ad3f6543b6e 114
xively 3:7ad3f6543b6e 115 wait( 15.0 );
xively 0:efdea27c3b81 116 }
Ilya Dmitrichenko 6:9e4f4a8c1829 117 }