Basic example using a TCP protocol. Client version. It connects to a Raspberry Pi server using a Python Script.

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoClient by Gerardo CR

Basic program.

It connects to a remote server on a LAN running a Python script. Server send data to a spreadsheet in my google docs account.

PYTHON CODE

PYTHON CODE

#!/usr/bin/python
import time
import datetime
import sys
import gspread
import socket

# Socket
port = 50006
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', port))
s.listen(1)

# Google Acoount Details
email = 'your-email@gmail.com'
password = 'your-password'
spreadsheet = 'ldr'

# Login with your Google account
try:
  gc = gspread.login(email, password)
except:
  print "unable to login in. Check your email address/password"
  sys.exit()
  
# Open a worksheet for your spreadsheet using the filename
try:
  wks = gc.open(spreadsheet).sheet1
except:
  print ("unable to open the spredsheet.  Check your filename: %s") % spreadsheet
  sys.exit()

time.sleep(1)

input = 0

conn, addr = s.accept()
print 'Connected by', addr

while True:  
    input = conn.recv(1024)
    print(input)
    print(datetime.datetime.now(), input)
    print('write angle to gdocs')
    try:
      values = [datetime.datetime.now(), input]
      wks.append_row(values)
    except:
      print ("Unable to append data.  Check your connection?")
      sys.exit()

Files at this revision

API Documentation at this revision

Comitter:
emilmont
Date:
Wed Aug 01 13:03:13 2012 +0000
Parent:
2:563aa530f0dd
Child:
4:4aa1faf9363e
Commit message:
Update socket library

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.lib	Thu Jul 26 16:45:55 2012 +0000
+++ b/EthernetInterface.lib	Wed Aug 01 13:03:13 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/EthernetInterface/#b7a3766f6253
+http://mbed.org/users/mbed_official/code/EthernetInterface/#0d9ae7845bfe
--- a/main.cpp	Thu Jul 26 16:45:55 2012 +0000
+++ b/main.cpp	Wed Aug 01 13:03:13 2012 +0000
@@ -1,8 +1,8 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 
-const char* ECHO_SERVER_ADDRESS = "10.2.131.73";
-const int ECHO_PORT = 7;
+const char* ECHO_SERVER_ADDRESS = "192.168.0.51";
+const int ECHO_SERVER_PORT = 7;
 
 int main() {
     EthernetInterface eth;
@@ -10,21 +10,21 @@
     eth.connect();
     printf("IP Address is %s\n", eth.getIPAddress());
     
-    TCPSocketConnection sock;
-    while (sock.connect(ECHO_SERVER_ADDRESS, ECHO_PORT) < 0) {
-        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_PORT);
+    TCPSocketConnection socket;
+    while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) {
+        printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
         wait(1);
     }
     
     char hello[] = "Hello World\n";
-    sock.send_all(hello, sizeof(hello) - 1);
+    socket.send_all(hello, sizeof(hello) - 1);
     
     char buf[256];
-    int n = sock.receive(buf, 256);
+    int n = socket.receive(buf, 256);
     buf[n] = '\0';
     printf("%s", buf);
     
-    sock.close();
+    socket.close();
     eth.disconnect();
     
     while(true) {}
--- a/mbed-rtos.lib	Thu Jul 26 16:45:55 2012 +0000
+++ b/mbed-rtos.lib	Wed Aug 01 13:03:13 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#4238c328365e
+http://mbed.org/users/mbed_official/code/mbed-rtos/#70bd33086f56