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()

main.cpp

Committer:
xively
Date:
2013-06-26
Revision:
3:7ad3f6543b6e
Parent:
1:0a61d7ab702c
Child:
4:e7ca62a11595

File content as of revision 3:7ad3f6543b6e:

#include "mbed.h"
#include "EthernetInterface.h"
#include "xively.h"
#include "xi_err.h"
#include "xi_printf.h"

#include <stdarg.h>
#include <stdio.h>

#include "MMA7660.h"
#include "LM75B.h"

//#define XI_FEED_ID 123 // set Xively Feed ID (numerical, no quoutes
//#define XI_API_KEY "T4KXAH_dasgw1PWBPc3fdsfsdgsdy-dUc4ND0g" // set Xively API key (double-quoted string) 
#define XI_FEED_ID 128488
#define XI_API_KEY "9YL5ZOL8pV0gLeJMKJeQPy5TaQJkz06IpgccQD2fXvjezSll"

#include "C12832_lcd.h"

C12832_LCD lcd;

MMA7660 axl(p28, p27);
LM75B tmp(p28, p27);

extern "C" {

void user_printf( const char* buffer )
{
    lcd.cls();
    lcd.locate( 0, 3 );
    lcd.printf( buffer );
    //wait( 1.0 );    
}

void mbed_printf( const char* fmt, ...  )
{
    char buffer[ 64 ];
    
    va_list ap;
    va_start( ap, fmt );
    vsnprintf( buffer, 64, fmt, ap );
    va_end( ap );
    
    user_printf( buffer );
}

}

int main() {
    // set our device specific print function
    USER_PRINT = user_printf;
    
    EthernetInterface eth;
    
    int s = eth.init(); //Use DHCP
    
    if( s != NULL )
    {
        mbed_printf( "Could not initialise. Will halt!\n" );        
        exit( 0 );
    }    
        
    s = eth.connect();
    
    if( s != NULL )
    {
        mbed_printf( "Could not connect. Will halt!\n" );
        exit( 0 );
    }
    else 
    {
        mbed_printf( "IP: %s\n", eth.getIPAddress() );    
    }
    
    xi_feed_t feed;
    memset( &feed, NULL, sizeof( xi_feed_t ) );
    
    feed.feed_id = XI_FEED_ID;
    feed.datastream_count = 3;
    
    feed.datastreams[0].datapoint_count = 1;
    xi_datastream_t* orientation_datastream = &feed.datastreams[0];
    strcpy( orientation_datastream->datastream_id, "orientation" );
    xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];

    feed.datastreams[1].datapoint_count = 1;
    xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
    strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
    xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
    
    feed.datastreams[2].datapoint_count = 1;
    xi_datastream_t* temperature_datastream = &feed.datastreams[2];
    strcpy( temperature_datastream->datastream_id, "temperature" );
    xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
    
    // create the cosm library context
    xi_context_t* xi_context
        = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );

    // check if everything works
    if( xi_context == NULL )
    {
        return -1;
    }
    
    mbed_printf("feed:%d datastreams:[%s,%s]\n", feed.feed_id,
                    orientation_datastream->datastream_id,
                    side_rotation_datastream->datastream_id);

    while(1) {
      
      switch( axl.getSide() ) {
        case MMA7660::Front:
          xi_set_value_str( current_side_rotation, "front" );
          break;
        case MMA7660::Back:
          xi_set_value_str( current_side_rotation, "back" );
          break;
        default:
          xi_set_value_str( current_side_rotation, "unknown" );
          break;
      }
      
      switch( axl.getOrientation() ) {
        case MMA7660::Down:
          mbed_printf("down %s\n",
            (axl.getSide() == MMA7660::Front ? "front" : "back"));
          xi_set_value_str( current_orientation, "down" );
          break;
        case MMA7660::Up:
           mbed_printf("up %s\n",
            (axl.getSide() == MMA7660::Front ? "front" : "back"));
           xi_set_value_str( current_orientation, "up" );
           break;
        case MMA7660::Right:
          mbed_printf("right %s\n",
            (axl.getSide() == MMA7660::Front ? "front" : "back"));
          xi_set_value_str( current_orientation, "right" );
          break;
        case MMA7660::Left:
          mbed_printf("left %s\n",
            (axl.getSide() == MMA7660::Front ? "front" : "back"));
          xi_set_value_str( current_orientation, "left" );
          break;
        default: 
          xi_set_value_str( current_orientation, "unknown" );
          break;
      }
      
      xi_set_value_f32( current_temperature, tmp.read() );
      
      mbed_printf( "update...\n" );
      xi_feed_update(xi_context, &feed);
      mbed_printf( "done...\n" );
      
      wait( 15.0 );
    }
}