Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with my Raspberry Pi talking to my Arduino through XBee modules.

I have Python script running a websocket, from where i wish to control the pins of my Arduino and Raspberry Pi through a website.

Whenever i call the commands for controlling the Raspberry Pi, everything is fint. But when i call the commands that should be send through Serial, the script breaks, and gives me this error:

ERROR:tornado.application:Uncaught exception in /ws
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/websocket.py", line 303, in wrapper
    return callback(*args, **kwargs)
  File "websocket.py", line 101, in on_message
    xbee.write("%s" % message)
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 491, in write
    d = to_bytes(data)
  File "/usr/local/lib/python2.7/dist-packages/serial/serialutil.py", line 76, in to_bytes
    b.append(item)  # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer or string of size 1 is required
Connection was closed...

Here is my Python script:

    import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import RPi.GPIO as GPIO
import serial
import time

xbee = serial.Serial("/dev/ttyAMA0", 9600)
#xbee.open()

time.sleep(2)

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN) 

pin18 = GPIO.input(18)
pin23 = GPIO.input(23)
pin24 = GPIO.input(24)
pin25 = GPIO.input(25)

GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)

class WSHandler(tornado.websocket.WebSocketHandler):

    def open(self):
        print "New connection was opened"
        self.write_message("Connected!")
        self.write_message("pin18 = %s \n pin23 = %s \n pin24 = %s \n pin25 = %s" % (pin18, pin23, pin24, pin25))


    def on_message(self, message):
        global pin18
        global pin23
        global pin24
        global pin25

        if(message == "pin18"):
            print "%s" % message
            if(pin18 == 1):
                GPIO.output(18, GPIO.LOW)
                pin18 = 0
            else:
                GPIO.output(18, GPIO.HIGH)
                pin18 = 1

        elif(message == "pin23"):
            if(pin23 == 1):
                GPIO.output(23, GPIO.LOW)
                pin23 = 0
            else:
                GPIO.output(23, GPIO.HIGH)
                pin23 = 1       

        elif(message == "pin24"):
            if(pin24 == 1):
                GPIO.output(24, GPIO.LOW)
                pin24 = 0
            else: 
                GPIO.output(24, GPIO.HIGH)
                pin24 = 1

        elif(message == "pin25"):
            if(pin25 == 1):
                GPIO.output(25, GPIO.LOW)
                pin25 = 0
            else:
                GPIO.output(25, GPIO.HIGH)
                pin25 = 1

        elif(message == "alloff"):
            GPIO.output(18, GPIO.HIGH)
            pin18 = 1
            GPIO.output(23, GPIO.HIGH)
            pin23 = 1
            GPIO.output(24, GPIO.HIGH)
            pin24 = 1
            GPIO.output(25, GPIO.HIGH)
            pin25 = 1

        elif(message == "allon"):
            GPIO.output(18, GPIO.LOW)
            pin18 = 0
            GPIO.output(23, GPIO.LOW)
            pin23 = 0
            GPIO.output(24, GPIO.LOW)
            pin24 = 0
            GPIO.output(25, GPIO.LOW)
            pin25 = 0

        else:
            self.write_message("%s\n" % message)
            print "%s" % message
            xbee.write("%s" % message)

    def on_close(self):
        print "Connection was closed..."

application = tornado.web.Application([
    (r'/ws', WSHandler),
])

if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(9090)
    tornado.ioloop.IOLoop.instance().start()

Can anyone tell me what the problem is? :)

share|improve this question
add comment

1 Answer

I had a similar problem, it was related to encoding issues. If I had appended a unicode character like so, I would get this info:

serial.write(command + u'\r\n')

once I removed the u, it would work fine:

serial.write(command + '\r\n')

So you might want to try, something like

xbee.write(message.encode('iso-8859-1'))

This might not be exactly an answer, just a hint.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.