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:
Tue Nov 25 00:33:30 2014 +0000
Revision:
27:6949291ca38d
Parent:
25:39099b3b173f
Child:
28:174412ff9671
Add the definition of serial for debugging output.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kishino 21:25b85cbbdd82 1 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
kishino 23:39cf9f03b076 2 * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
kishino 21:25b85cbbdd82 3 *
kishino 21:25b85cbbdd82 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kishino 21:25b85cbbdd82 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kishino 21:25b85cbbdd82 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kishino 21:25b85cbbdd82 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kishino 21:25b85cbbdd82 8 * furnished to do so, subject to the following conditions:
kishino 21:25b85cbbdd82 9 *
kishino 21:25b85cbbdd82 10 * The above copyright notice and this permission notice shall be included in all copies or
kishino 21:25b85cbbdd82 11 * substantial portions of the Software.
kishino 21:25b85cbbdd82 12 *
kishino 21:25b85cbbdd82 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kishino 21:25b85cbbdd82 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kishino 21:25b85cbbdd82 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kishino 21:25b85cbbdd82 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kishino 21:25b85cbbdd82 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kishino 21:25b85cbbdd82 18 */
xively 0:efdea27c3b81 19 #include "mbed.h"
kishino 14:6d58d3855feb 20 #include "SNIC_WifiInterface.h"
xively 7:0eff5db44b8b 21
kishino 14:6d58d3855feb 22 #define XI_FEED_ID 1056160623 // set Xively Feed ID (numerical, no quoutes)
kishino 14:6d58d3855feb 23 #define XI_API_KEY "Wg7CfZDrj7VjIIpiYzdDrMow6wdENAOGjkIfQ0fUjJh6DAw2" // set Xively API key (double-quoted string)
errordeveloper 10:86ffba646df1 24
errordeveloper 10:86ffba646df1 25 #include "app_board_io.h"
xively 7:0eff5db44b8b 26
xively 0:efdea27c3b81 27 #include "xively.h"
xively 0:efdea27c3b81 28 #include "xi_err.h"
xively 0:efdea27c3b81 29
errordeveloper 10:86ffba646df1 30 #include "MMA7660.h"
errordeveloper 10:86ffba646df1 31 #include "LM75B.h"
kishino 22:e567f0d4b05d 32 #include "C12832.h"
errordeveloper 10:86ffba646df1 33
kishino 22:e567f0d4b05d 34 #if defined(TARGET_LPC1768)
kishino 16:ed9b9c28f860 35 #include "PowerControl/EthernetPowerControl.h"
kishino 22:e567f0d4b05d 36 C12832 lcd(p5, p7, p6, p8, p11);
xively 0:efdea27c3b81 37 MMA7660 axl(p28, p27);
xively 3:7ad3f6543b6e 38 LM75B tmp(p28, p27);
kishino 27:6949291ca38d 39 Serial pc(USBTX, USBRX); /* for DEBUG_PRINT */
kishino 25:39099b3b173f 40 #elif defined(TARGET_KL46Z)
kishino 22:e567f0d4b05d 41 C12832 lcd(D11, D13, D12, D7, D10);
kishino 25:39099b3b173f 42 MMA7660 axl(I2C_SDA, I2C_SCL);
kishino 25:39099b3b173f 43 LM75B tmp(I2C_SDA, I2C_SCL);
kishino 27:6949291ca38d 44 Serial pc(P2_12, P2_11); /* for DEBUG_PRINT */
kishino 22:e567f0d4b05d 45 #endif
errordeveloper 11:bdf601a405fc 46
errordeveloper 11:bdf601a405fc 47 #include "logo.h"
xively 0:efdea27c3b81 48
kishino 24:3d150ff59276 49 #define DEMO_AP_SSID "AP_SSID"
kishino 17:0bf3c49a83d5 50 #define DEMO_AP_SECURITY_TYPE e_SEC_WPA2_AES
kishino 24:3d150ff59276 51 #define DEMO_AP_SECUTIRY_KEY "WPA2_PASSPHRASE"
kishino 24:3d150ff59276 52 #define DEMO_AP_SECUTIRY_KEY_LEN 15
kishino 19:4e2900daad59 53
kishino 14:6d58d3855feb 54 /** Wi-Fi SNIC UART Interface*/
kishino 22:e567f0d4b05d 55 #if defined(TARGET_LPC1768)
kishino 14:6d58d3855feb 56 C_SNIC_WifiInterface mSNICwifi( p9, p10, NC, NC, p30 );
kishino 25:39099b3b173f 57 #elif defined(TARGET_KL46Z)
kishino 25:39099b3b173f 58 C_SNIC_WifiInterface mSNICwifi( D1, D0, NC, NC, D3 );
kishino 22:e567f0d4b05d 59 #endif
kishino 17:0bf3c49a83d5 60
xively 0:efdea27c3b81 61 int main() {
kishino 16:ed9b9c28f860 62
kishino 22:e567f0d4b05d 63 #if defined(TARGET_LPC1768)
kishino 16:ed9b9c28f860 64 PHY_PowerDown();
kishino 22:e567f0d4b05d 65 #endif
kishino 25:39099b3b173f 66 #ifdef _DEBUG
kishino 14:6d58d3855feb 67 pc.baud( 115200 );
kishino 25:39099b3b173f 68 #endif
kishino 22:e567f0d4b05d 69 DEBUG_PRINT("main\r\n");
errordeveloper 11:bdf601a405fc 70 lcd_print_xively_logo();
xively 0:efdea27c3b81 71
kishino 14:6d58d3855feb 72 // Initialize Wi-Fi interface
kishino 14:6d58d3855feb 73 int s = mSNICwifi.init();
xively 0:efdea27c3b81 74
kishino 14:6d58d3855feb 75 lcd_printf("init();\r\n");
kishino 14:6d58d3855feb 76
kishino 14:6d58d3855feb 77 if( s != 0 )
xively 0:efdea27c3b81 78 {
xively 7:0eff5db44b8b 79 lcd_printf( "Could not initialise. Will halt!\n" );
kishino 14:6d58d3855feb 80 return -1;
xively 0:efdea27c3b81 81 }
kishino 20:f0c7f5ca7e8a 82 wait(0.5);
kishino 20:f0c7f5ca7e8a 83 s = mSNICwifi.disconnect();
kishino 14:6d58d3855feb 84 lcd_printf("disconnect();\r\n");
kishino 20:f0c7f5ca7e8a 85 if( s != 0 )
kishino 20:f0c7f5ca7e8a 86 {
kishino 20:f0c7f5ca7e8a 87 printf( "disconnect failed\r\n" );
kishino 20:f0c7f5ca7e8a 88 return -1;
kishino 20:f0c7f5ca7e8a 89 }
xively 0:efdea27c3b81 90
kishino 19:4e2900daad59 91 wait(0.3);
kishino 14:6d58d3855feb 92 // Connect AP
kishino 19:4e2900daad59 93 mSNICwifi.connect( DEMO_AP_SSID
kishino 14:6d58d3855feb 94 , strlen(DEMO_AP_SSID)
kishino 14:6d58d3855feb 95 , DEMO_AP_SECURITY_TYPE
kishino 14:6d58d3855feb 96 , DEMO_AP_SECUTIRY_KEY
kishino 14:6d58d3855feb 97 , DEMO_AP_SECUTIRY_KEY_LEN );
kishino 14:6d58d3855feb 98 lcd_printf("connect();\r\n");
kishino 19:4e2900daad59 99 wait(0.5);
kishino 19:4e2900daad59 100
kishino 19:4e2900daad59 101 lcd_printf("IP Config();\r\n");
kishino 19:4e2900daad59 102 mSNICwifi.setIPConfig( true );
kishino 19:4e2900daad59 103
kishino 14:6d58d3855feb 104 wait(0.5);
kishino 14:6d58d3855feb 105
xively 0:efdea27c3b81 106 xi_feed_t feed;
xively 0:efdea27c3b81 107 memset( &feed, NULL, sizeof( xi_feed_t ) );
xively 0:efdea27c3b81 108
xively 0:efdea27c3b81 109 feed.feed_id = XI_FEED_ID;
xively 3:7ad3f6543b6e 110 feed.datastream_count = 3;
xively 0:efdea27c3b81 111
xively 0:efdea27c3b81 112 feed.datastreams[0].datapoint_count = 1;
xively 0:efdea27c3b81 113 xi_datastream_t* orientation_datastream = &feed.datastreams[0];
xively 0:efdea27c3b81 114 strcpy( orientation_datastream->datastream_id, "orientation" );
xively 0:efdea27c3b81 115 xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
xively 0:efdea27c3b81 116
xively 0:efdea27c3b81 117 feed.datastreams[1].datapoint_count = 1;
xively 0:efdea27c3b81 118 xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
xively 0:efdea27c3b81 119 strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
xively 0:efdea27c3b81 120 xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
xively 0:efdea27c3b81 121
xively 3:7ad3f6543b6e 122 feed.datastreams[2].datapoint_count = 1;
xively 3:7ad3f6543b6e 123 xi_datastream_t* temperature_datastream = &feed.datastreams[2];
xively 3:7ad3f6543b6e 124 strcpy( temperature_datastream->datastream_id, "temperature" );
xively 3:7ad3f6543b6e 125 xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
xively 3:7ad3f6543b6e 126
xively 0:efdea27c3b81 127 // create the cosm library context
xively 0:efdea27c3b81 128 xi_context_t* xi_context
xively 0:efdea27c3b81 129 = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
xively 0:efdea27c3b81 130
xively 0:efdea27c3b81 131 // check if everything works
xively 0:efdea27c3b81 132 if( xi_context == NULL )
xively 0:efdea27c3b81 133 {
xively 0:efdea27c3b81 134 return -1;
xively 0:efdea27c3b81 135 }
xively 0:efdea27c3b81 136
xively 0:efdea27c3b81 137 while(1) {
xively 0:efdea27c3b81 138 switch( axl.getSide() ) {
xively 0:efdea27c3b81 139 case MMA7660::Front:
xively 0:efdea27c3b81 140 xi_set_value_str( current_side_rotation, "front" );
xively 0:efdea27c3b81 141 break;
xively 0:efdea27c3b81 142 case MMA7660::Back:
xively 0:efdea27c3b81 143 xi_set_value_str( current_side_rotation, "back" );
xively 0:efdea27c3b81 144 break;
xively 0:efdea27c3b81 145 default:
xively 0:efdea27c3b81 146 xi_set_value_str( current_side_rotation, "unknown" );
xively 0:efdea27c3b81 147 break;
xively 0:efdea27c3b81 148 }
xively 0:efdea27c3b81 149
xively 0:efdea27c3b81 150 switch( axl.getOrientation() ) {
xively 0:efdea27c3b81 151 case MMA7660::Down:
xively 0:efdea27c3b81 152 xi_set_value_str( current_orientation, "down" );
xively 0:efdea27c3b81 153 break;
xively 0:efdea27c3b81 154 case MMA7660::Up:
xively 0:efdea27c3b81 155 xi_set_value_str( current_orientation, "up" );
xively 0:efdea27c3b81 156 break;
xively 0:efdea27c3b81 157 case MMA7660::Right:
xively 0:efdea27c3b81 158 xi_set_value_str( current_orientation, "right" );
xively 0:efdea27c3b81 159 break;
xively 0:efdea27c3b81 160 case MMA7660::Left:
xively 0:efdea27c3b81 161 xi_set_value_str( current_orientation, "left" );
xively 0:efdea27c3b81 162 break;
xively 0:efdea27c3b81 163 default:
xively 0:efdea27c3b81 164 xi_set_value_str( current_orientation, "unknown" );
xively 0:efdea27c3b81 165 break;
xively 0:efdea27c3b81 166 }
xively 3:7ad3f6543b6e 167
xively 3:7ad3f6543b6e 168 xi_set_value_f32( current_temperature, tmp.read() );
xively 4:e7ca62a11595 169
xively 7:0eff5db44b8b 170 lcd_printf( "update...\n" );
errordeveloper 11:bdf601a405fc 171 xi_feed_update( xi_context, &feed );
xively 7:0eff5db44b8b 172 lcd_printf( "done...\n" );
xively 3:7ad3f6543b6e 173
kishino 14:6d58d3855feb 174 wait( 1.0 );
xively 0:efdea27c3b81 175 }
Ilya Dmitrichenko 6:9e4f4a8c1829 176 }