Raspberry Internet Rover Part 2 – Python Datagram Server Codes

Some python code in the Raspberry Pi is needed to send commands.

Install socketIO_Client for your python from : http://www.gelens.org/code/gevent-websocket/

Then please save this file in your Raspberry and run it.

#
# Datagram socket server class – derives from sserver
#

import socket
import serial
import sserver
from socketIO_client import SocketIO

ser = serial.Serial()
ser.port = “/dev/ttyACM0”
ser.baudrate =9600

try:
ser.open()
except Exception, e:
print “ACM0 not available, try ACM1”
ser.port = “/dev/ttyACM1”
ser.baudrate=9600
ser.open()

buf = 1024

class dgramserver (sserver.sserver):
# use inherited constructor

def handleconnection (self):    # Handle one incoming connection, from birth to death – not very long
stat = self.ssock.recvfrom(buf)
data, self.raddr = stat
if not data:
pass
else:
self.handlemsg (data)           # our last words

def handlemsg (self, data):         # s/b overriden in subclass
pass

def sendmsg (self, data):
self.ssock.sendto (data, self.raddr)

def serve (self):
while 1:
self.ssock = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
self.ssock.bind (self.addr)
#self.ssock.connect((‘10.0.0.28’,9091))
self.handleconnection ()
self.ssock.close()

if __name__ == ‘__main__’:        # self test code
class echoserver (dgramserver):    # echo server
def handlemsg (self, data):    # simply send back the message
#data = “sending back ” + data
print data
self.sendmsg (“5:1::{‘name’:’message’,’args’:'” + data + “‘}”)
ser.write(data)

#connect back to NodeJS server as the client to be able to send back the data
client = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
client.connect((‘10.0.0.28’,1234))
client.sendall (‘robot:’+data)

#self.mainSocket = SocketIO(‘localhost’,9092)
#self.gazeSocket = self.mainSocket.connect()
#self.gazeSocket.emit(‘message’, data)

echo = echoserver (”, 9090)
print ‘Serving….’
echo.serve ()

This thing basically listens to commands from another server and relay it to Arduino board via serial port. This also sends back signals to its sender.

Ideally we want to use a responsive web server, so we use NodeJS although other options are available, NodeJS seems to be the easiest to run out-of-the-box.

Please find the NodeJS server code on the next page.

[post_view]

(Visited 1,855 times, 1 visits today)

14 thoughts on “Raspberry Internet Rover Part 2 – Python Datagram Server Codes”

  1. Henry Sachs says:

    Hey i got some questions on the code would you mind writing some mails or something like that would be really helpful 🙂

    1. Dipto Pratyaksa Dipto Pratyaksa says:

      Sure, what do you want to ask. You can ask here to get feedback from others too

      1. Henry Sachs says:

        The link for the websocket library is outdated so could you provide a new one.

        Anyway i got the node.js Server running but dont know how to reach the html Website through my Browser or my node.js doesnt present it i’m not sure.

      2. Dipto Pratyaksa Dipto Pratyaksa says:

        These days are a lot easier to setup communications between sockets
        https://gist.github.com/drewww/1402238
        http://socket.io/

        have you run it with $python3 node mynodeserver.js and open it on the web browser specifying the IP and port number, like http://192.168.1.111:8080 ?

      3. Henry Sachs says:

        Yeah but i still need this Script for the Connection to the arduino.

        I run it with node and pointed at 9090 just like in your example on the next page

      4. Dipto Pratyaksa Dipto Pratyaksa says:

        You dont need datagram server if you are only wanting connection between the web and Arduino.

        You only need to make sure you can transmit bytes from nodejs through serial port.

        You could either use python web server like Tornado or Flask or nodejs.

      5. Henry Sachs says:

        So i can merge the python Script with my node js server?

        And where do i declare the node js which Website to Show?

      6. Henry Sachs says:

        Will have a look in the afternoon into that. Would really appreciate if someone could help me write this Python Script which sends the data that i send from my Website to the arduino.

      7. Dipto Pratyaksa Dipto Pratyaksa says:

        You dont need python if you use nodejs and you dont need nodejs if you use python.

        I will write another tutorial on how you can setup serial communication between Tornado and Arduino.

        Basically you only need:
        Html with jquery for button action, web server using Tornado or nodejs, then the Arduino script.

        Once you know how to serve html using python tornado.. the rest is not that hard

      8. Henry Sachs says:

        How fast will you be able to deliver that? 🙂

        And why didnt you use this right here in this example?

      9. Dipto Pratyaksa Dipto Pratyaksa says:

        I’ve written the html version here
        https://www.linuxcircle.com/2013/04/14/html-script-to-listen-and-send-commands-to-arduino/

        Just have to dig my blor a little deeper

      10. Henry Sachs says:

        So will try your newer version in the afternoon and comment my results there.

        See you soon and thanks m8 🙂

      11. Dipto Pratyaksa Dipto Pratyaksa says:

        What do you want to achieve in the end?

      12. Henry Sachs says:

        So in the end i want an App which sends Data to Control a rc car

        But First i want to send these commands from a website which will send them to the arduino which interprets them

        E.g i will send move and the car will Drive

Comments are closed.