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:
Wed Jun 11 05:28:21 2014 +0000
Revision:
17:0bf3c49a83d5
Parent:
16:ed9b9c28f860
Child:
18:62daece493a9
Update SNIC xively jumpstart-demo

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
kishino 16:ed9b9c28f860 16 #include "PowerControl/EthernetPowerControl.h"
kishino 16:ed9b9c28f860 17
kishino 17:0bf3c49a83d5 18 #include "UDPSocket.h"
kishino 17:0bf3c49a83d5 19
xively 0:efdea27c3b81 20 MMA7660 axl(p28, p27);
xively 3:7ad3f6543b6e 21 LM75B tmp(p28, p27);
errordeveloper 11:bdf601a405fc 22 C12832_LCD lcd;
errordeveloper 11:bdf601a405fc 23
errordeveloper 11:bdf601a405fc 24 #include "logo.h"
xively 0:efdea27c3b81 25
kishino 17:0bf3c49a83d5 26 #if 0
kishino 15:abc12b228291 27 #define DEMO_AP_SSID "muRata12345"
kishino 15:abc12b228291 28 #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES
kishino 15:abc12b228291 29 #define DEMO_AP_SECUTIRY_KEY "12345678"
kishino 16:ed9b9c28f860 30 #define DEMO_AP_SECUTIRY_KEY_LEN 8
kishino 14:6d58d3855feb 31 #else
kishino 15:abc12b228291 32 #define DEMO_AP_SSID "E2N1-Lab-Buffalo-D302"
kishino 17:0bf3c49a83d5 33 //#define DEMO_AP_SECURITY_TYPE e_SEC_OPEN
kishino 17:0bf3c49a83d5 34 //#define DEMO_AP_SECUTIRY_KEY ""
kishino 17:0bf3c49a83d5 35 #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES
kishino 17:0bf3c49a83d5 36 #define DEMO_AP_SECUTIRY_KEY "12345678"
kishino 17:0bf3c49a83d5 37 #define DEMO_AP_SECUTIRY_KEY_LEN 8
kishino 14:6d58d3855feb 38 #endif
kishino 14:6d58d3855feb 39 /** Wi-Fi SNIC UART Interface*/
kishino 14:6d58d3855feb 40 C_SNIC_WifiInterface mSNICwifi( p9, p10, NC, NC, p30 );
kishino 14:6d58d3855feb 41 Serial pc(USBTX, USBRX);
kishino 17:0bf3c49a83d5 42
kishino 17:0bf3c49a83d5 43 char buf[2048];
xively 0:efdea27c3b81 44 int main() {
kishino 16:ed9b9c28f860 45
kishino 16:ed9b9c28f860 46 PHY_PowerDown();
kishino 16:ed9b9c28f860 47
kishino 14:6d58d3855feb 48 pc.baud( 115200 );
kishino 14:6d58d3855feb 49 printf("main\r\n");
errordeveloper 11:bdf601a405fc 50 lcd_print_xively_logo();
xively 0:efdea27c3b81 51
kishino 14:6d58d3855feb 52 // Initialize Wi-Fi interface
kishino 14:6d58d3855feb 53 int s = mSNICwifi.init();
xively 0:efdea27c3b81 54
kishino 14:6d58d3855feb 55 lcd_printf("init();\r\n");
kishino 14:6d58d3855feb 56
kishino 14:6d58d3855feb 57 if( s != 0 )
xively 0:efdea27c3b81 58 {
xively 7:0eff5db44b8b 59 lcd_printf( "Could not initialise. Will halt!\n" );
kishino 14:6d58d3855feb 60 return -1;
xively 0:efdea27c3b81 61 }
xively 0:efdea27c3b81 62
kishino 14:6d58d3855feb 63 wait(0.5);
kishino 14:6d58d3855feb 64 mSNICwifi.disconnect();
kishino 14:6d58d3855feb 65 lcd_printf("disconnect();\r\n");
xively 0:efdea27c3b81 66
kishino 14:6d58d3855feb 67 wait(0.5);
kishino 14:6d58d3855feb 68 // Connect AP
kishino 14:6d58d3855feb 69 s = mSNICwifi.connect( DEMO_AP_SSID
kishino 14:6d58d3855feb 70 , strlen(DEMO_AP_SSID)
kishino 14:6d58d3855feb 71 , DEMO_AP_SECURITY_TYPE
kishino 14:6d58d3855feb 72 , DEMO_AP_SECUTIRY_KEY
kishino 14:6d58d3855feb 73 , DEMO_AP_SECUTIRY_KEY_LEN );
kishino 14:6d58d3855feb 74 lcd_printf("connect();\r\n");
kishino 16:ed9b9c28f860 75 if( s != 0 )
xively 0:efdea27c3b81 76 {
xively 7:0eff5db44b8b 77 lcd_printf( "Could not connect. Will halt!\n" );
kishino 14:6d58d3855feb 78 return -1;
xively 0:efdea27c3b81 79 }
kishino 14:6d58d3855feb 80 wait(0.5);
kishino 14:6d58d3855feb 81
kishino 17:0bf3c49a83d5 82 UDPSocket udp;
kishino 17:0bf3c49a83d5 83 Endpoint ep;
kishino 17:0bf3c49a83d5 84
kishino 17:0bf3c49a83d5 85 /*
kishino 17:0bf3c49a83d5 86 // UDP Client
kishino 17:0bf3c49a83d5 87 char udp_data_p[] = {"This is UDP data."};
kishino 17:0bf3c49a83d5 88 int len = strlen(udp_data_p);
kishino 17:0bf3c49a83d5 89
kishino 17:0bf3c49a83d5 90 ep.set_address("192.168.10.101", 30000);
kishino 17:0bf3c49a83d5 91
kishino 17:0bf3c49a83d5 92 while(1)
kishino 17:0bf3c49a83d5 93 {
kishino 17:0bf3c49a83d5 94 udp.sendTo( ep, udp_data_p, len );
kishino 17:0bf3c49a83d5 95 wait(1);
kishino 17:0bf3c49a83d5 96 }
kishino 17:0bf3c49a83d5 97 printf("UDP bind\r\n");
kishino 17:0bf3c49a83d5 98 udp.bind(40000);
kishino 17:0bf3c49a83d5 99
kishino 17:0bf3c49a83d5 100 int len, i;
kishino 17:0bf3c49a83d5 101 while(1)
kishino 17:0bf3c49a83d5 102 {
kishino 17:0bf3c49a83d5 103 len = udp.receiveFrom( ep, buf, 2048 );
kishino 17:0bf3c49a83d5 104 if( len < 1 )
kishino 17:0bf3c49a83d5 105 {
kishino 17:0bf3c49a83d5 106 Thread::yield();
kishino 17:0bf3c49a83d5 107 continue;
kishino 17:0bf3c49a83d5 108 }
kishino 17:0bf3c49a83d5 109 printf("recv from[%s:%d]\r\n", ep.get_address(), ep.get_port());
kishino 17:0bf3c49a83d5 110 for(i=0;i<len;i++)
kishino 17:0bf3c49a83d5 111 {
kishino 17:0bf3c49a83d5 112 printf("%02x", buf[i]);
kishino 17:0bf3c49a83d5 113 }
kishino 17:0bf3c49a83d5 114 printf("\r\n");
kishino 17:0bf3c49a83d5 115 }
kishino 17:0bf3c49a83d5 116 */
kishino 17:0bf3c49a83d5 117
kishino 17:0bf3c49a83d5 118 #if 1
xively 0:efdea27c3b81 119 xi_feed_t feed;
xively 0:efdea27c3b81 120 memset( &feed, NULL, sizeof( xi_feed_t ) );
xively 0:efdea27c3b81 121
xively 0:efdea27c3b81 122 feed.feed_id = XI_FEED_ID;
xively 3:7ad3f6543b6e 123 feed.datastream_count = 3;
xively 0:efdea27c3b81 124
xively 0:efdea27c3b81 125 feed.datastreams[0].datapoint_count = 1;
xively 0:efdea27c3b81 126 xi_datastream_t* orientation_datastream = &feed.datastreams[0];
xively 0:efdea27c3b81 127 strcpy( orientation_datastream->datastream_id, "orientation" );
xively 0:efdea27c3b81 128 xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
xively 0:efdea27c3b81 129
xively 0:efdea27c3b81 130 feed.datastreams[1].datapoint_count = 1;
xively 0:efdea27c3b81 131 xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
xively 0:efdea27c3b81 132 strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
xively 0:efdea27c3b81 133 xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
xively 0:efdea27c3b81 134
xively 3:7ad3f6543b6e 135 feed.datastreams[2].datapoint_count = 1;
xively 3:7ad3f6543b6e 136 xi_datastream_t* temperature_datastream = &feed.datastreams[2];
xively 3:7ad3f6543b6e 137 strcpy( temperature_datastream->datastream_id, "temperature" );
xively 3:7ad3f6543b6e 138 xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
xively 3:7ad3f6543b6e 139
xively 0:efdea27c3b81 140 // create the cosm library context
xively 0:efdea27c3b81 141 xi_context_t* xi_context
xively 0:efdea27c3b81 142 = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
xively 0:efdea27c3b81 143
xively 0:efdea27c3b81 144 // check if everything works
xively 0:efdea27c3b81 145 if( xi_context == NULL )
xively 0:efdea27c3b81 146 {
xively 0:efdea27c3b81 147 return -1;
xively 0:efdea27c3b81 148 }
xively 0:efdea27c3b81 149
xively 0:efdea27c3b81 150 while(1) {
xively 0:efdea27c3b81 151
xively 0:efdea27c3b81 152 switch( axl.getSide() ) {
xively 0:efdea27c3b81 153 case MMA7660::Front:
xively 0:efdea27c3b81 154 xi_set_value_str( current_side_rotation, "front" );
xively 0:efdea27c3b81 155 break;
xively 0:efdea27c3b81 156 case MMA7660::Back:
xively 0:efdea27c3b81 157 xi_set_value_str( current_side_rotation, "back" );
xively 0:efdea27c3b81 158 break;
xively 0:efdea27c3b81 159 default:
xively 0:efdea27c3b81 160 xi_set_value_str( current_side_rotation, "unknown" );
xively 0:efdea27c3b81 161 break;
xively 0:efdea27c3b81 162 }
xively 0:efdea27c3b81 163
xively 0:efdea27c3b81 164 switch( axl.getOrientation() ) {
xively 0:efdea27c3b81 165 case MMA7660::Down:
xively 0:efdea27c3b81 166 xi_set_value_str( current_orientation, "down" );
xively 0:efdea27c3b81 167 break;
xively 0:efdea27c3b81 168 case MMA7660::Up:
xively 0:efdea27c3b81 169 xi_set_value_str( current_orientation, "up" );
xively 0:efdea27c3b81 170 break;
xively 0:efdea27c3b81 171 case MMA7660::Right:
xively 0:efdea27c3b81 172 xi_set_value_str( current_orientation, "right" );
xively 0:efdea27c3b81 173 break;
xively 0:efdea27c3b81 174 case MMA7660::Left:
xively 0:efdea27c3b81 175 xi_set_value_str( current_orientation, "left" );
xively 0:efdea27c3b81 176 break;
xively 0:efdea27c3b81 177 default:
xively 0:efdea27c3b81 178 xi_set_value_str( current_orientation, "unknown" );
xively 0:efdea27c3b81 179 break;
xively 0:efdea27c3b81 180 }
xively 3:7ad3f6543b6e 181
xively 3:7ad3f6543b6e 182 xi_set_value_f32( current_temperature, tmp.read() );
xively 4:e7ca62a11595 183
xively 7:0eff5db44b8b 184 lcd_printf( "update...\n" );
errordeveloper 11:bdf601a405fc 185 xi_feed_update( xi_context, &feed );
xively 7:0eff5db44b8b 186 lcd_printf( "done...\n" );
xively 3:7ad3f6543b6e 187
kishino 14:6d58d3855feb 188 wait( 1.0 );
xively 0:efdea27c3b81 189 }
kishino 17:0bf3c49a83d5 190 #endif
Ilya Dmitrichenko 6:9e4f4a8c1829 191 }