demo of Murata wifi chip as TCP client.
Dependencies: SNICInterface mbed-rtos mbed
Fork of murataDemo by
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
- Make sure you have both the wifi device and the computer running the server on the same network / wifi router.
- Change the hard coded IP in the microcontroller code to match that of the laptop running the python server.
- Run the python2 script below on the computer
- 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).
- 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@12:27471bb09274, 2013-11-08 (annotated)
- Committer:
- errordeveloper
- Date:
- Fri Nov 08 16:44:46 2013 +0000
- Revision:
- 12:27471bb09274
- Parent:
- 11:bdf601a405fc
First prototype
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xively | 0:efdea27c3b81 | 1 | #include "mbed.h" |
xively | 7:0eff5db44b8b | 2 | |
errordeveloper | 12:27471bb09274 | 3 | #include "UbloxUSBCDMAModem.h" |
errordeveloper | 12:27471bb09274 | 4 | #include "UbloxUSBGSMModem.h" |
errordeveloper | 12:27471bb09274 | 5 | #define MODEM_UBLOX_CDMA // remove this if you are using GSM |
errordeveloper | 10:86ffba646df1 | 6 | |
errordeveloper | 12:27471bb09274 | 7 | #define XI_FEED_ID 248 // set Xively Feed ID (numerical, no quoutes) |
errordeveloper | 12:27471bb09274 | 8 | #define XI_API_KEY "T4KXAH_dasgw1PWBsfsdgy-dUc4ND0g" // set Xively API key (double-quoted string) |
errordeveloper | 12:27471bb09274 | 9 | |
errordeveloper | 12:27471bb09274 | 10 | #include "cellular_board_io.h" |
xively | 7:0eff5db44b8b | 11 | |
xively | 0:efdea27c3b81 | 12 | #include "xively.h" |
xively | 0:efdea27c3b81 | 13 | #include "xi_err.h" |
xively | 0:efdea27c3b81 | 14 | |
errordeveloper | 12:27471bb09274 | 15 | int main() { |
errordeveloper | 11:bdf601a405fc | 16 | |
errordeveloper | 12:27471bb09274 | 17 | //TODO: maybe move these into board_io files and do it for app board ethernet version too... |
errordeveloper | 12:27471bb09274 | 18 | #ifdef MODEM_UBLOX_CDMA |
errordeveloper | 12:27471bb09274 | 19 | UbloxUSBCDMAModem modem(p18, true, 1); |
errordeveloper | 12:27471bb09274 | 20 | #else |
errordeveloper | 12:27471bb09274 | 21 | UbloxUSBGSMModem modem; |
errordeveloper | 12:27471bb09274 | 22 | #endif |
xively | 0:efdea27c3b81 | 23 | |
errordeveloper | 12:27471bb09274 | 24 | modem.power(true); |
errordeveloper | 12:27471bb09274 | 25 | Thread::wait(1000); |
errordeveloper | 12:27471bb09274 | 26 | |
errordeveloper | 12:27471bb09274 | 27 | int s = modem.connect(NULL, NULL, "internet"); |
xively | 0:efdea27c3b81 | 28 | |
errordeveloper | 12:27471bb09274 | 29 | if( s ) |
xively | 0:efdea27c3b81 | 30 | { |
errordeveloper | 12:27471bb09274 | 31 | printf( "Could not connect to the Internet. Will halt!\r\n" ); |
xively | 0:efdea27c3b81 | 32 | exit( 0 ); |
xively | 0:efdea27c3b81 | 33 | } |
xively | 0:efdea27c3b81 | 34 | |
xively | 0:efdea27c3b81 | 35 | xi_feed_t feed; |
xively | 0:efdea27c3b81 | 36 | memset( &feed, NULL, sizeof( xi_feed_t ) ); |
xively | 0:efdea27c3b81 | 37 | |
xively | 0:efdea27c3b81 | 38 | feed.feed_id = XI_FEED_ID; |
errordeveloper | 12:27471bb09274 | 39 | feed.datastream_count = 1; |
xively | 0:efdea27c3b81 | 40 | |
xively | 0:efdea27c3b81 | 41 | feed.datastreams[0].datapoint_count = 1; |
errordeveloper | 12:27471bb09274 | 42 | xi_datastream_t* counter_datastream = &feed.datastreams[0]; |
errordeveloper | 12:27471bb09274 | 43 | strcpy( counter_datastream->datastream_id, "counter" ); |
errordeveloper | 12:27471bb09274 | 44 | xi_datapoint_t* counter = &counter_datastream->datapoints[0]; |
xively | 0:efdea27c3b81 | 45 | |
errordeveloper | 12:27471bb09274 | 46 | //TODO: |
errordeveloper | 12:27471bb09274 | 47 | //- figure out how got send `AT$DEBUG?` command and parse it's ouput which looks like: |
errordeveloper | 12:27471bb09274 | 48 | // $DEBUG: 4183,233,100,1,99,-9,-105,2,2,2,93,261,429,186,249,438,21,63,63,63,63,63 |
errordeveloper | 12:27471bb09274 | 49 | // For details see docs: http://www.u-blox.com/images/downloads/Product_Docs/LISAC200-FW75C200_ATCommandsManual_%28CDMA-2X-11002%29.pdf) |
errordeveloper | 12:27471bb09274 | 50 | // Some of these values represent varios interesting things, e.g. RSSI. |
errordeveloper | 12:27471bb09274 | 51 | // We should put some of those into separate Xively datastreams... |
xively | 3:7ad3f6543b6e | 52 | |
xively | 0:efdea27c3b81 | 53 | // create the cosm library context |
xively | 0:efdea27c3b81 | 54 | xi_context_t* xi_context |
xively | 0:efdea27c3b81 | 55 | = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); |
xively | 0:efdea27c3b81 | 56 | |
xively | 0:efdea27c3b81 | 57 | // check if everything works |
xively | 0:efdea27c3b81 | 58 | if( xi_context == NULL ) |
xively | 0:efdea27c3b81 | 59 | { |
xively | 0:efdea27c3b81 | 60 | return -1; |
xively | 0:efdea27c3b81 | 61 | } |
errordeveloper | 12:27471bb09274 | 62 | long unsigned int c = 0; |
xively | 0:efdea27c3b81 | 63 | while(1) { |
xively | 0:efdea27c3b81 | 64 | |
errordeveloper | 12:27471bb09274 | 65 | xi_set_value_i32( counter, ++c ); |
errordeveloper | 12:27471bb09274 | 66 | |
errordeveloper | 12:27471bb09274 | 67 | printf( "Updating Xively feed %d...\r\n", XI_FEED_ID ); |
errordeveloper | 12:27471bb09274 | 68 | xi_feed_update( xi_context, &feed ); |
errordeveloper | 12:27471bb09274 | 69 | printf( " done!\r\n" ); //TODO: add error check, as cellular can be flaky. |
xively | 0:efdea27c3b81 | 70 | |
errordeveloper | 12:27471bb09274 | 71 | wait( 50.0 ); |
xively | 0:efdea27c3b81 | 72 | } |
Ilya Dmitrichenko |
6:9e4f4a8c1829 | 73 | } |