0
\$\begingroup\$

I have a arduino running with bitlash running on it connected to a shiftbrite.

I can use a terminal program to send it commands such as rgb(1023,0,1023) and it will work but when I do it with python it doesn't.

Here is my python code. I don't know why it doesn't work. I can even print the string on an LCD connected to it and it comes out correctly

#Python Menu
#File: PythonMenu.py
#Basic menu system for use with Arduino and Shiftbrites
#over a wireless xbee link

import os #For clear
import time #For delay
import serial #For serial comms to the device

def printMenu():
    """Prints the menu ready with all options"""
    print "/--------------------------------------------\\"
    print "|      |  | |  | /--- |  |  __  ---|         |"
    print "|      |--| |  | |  - |--| |__ |___          |"
    print "|      |  | |__| |__| |  | |__  ___|         |"
    print "|--------------------------------------------|"
    print "| Please select from the following options   |"
    print "|                                            |"
    print "| (1) White                                  |"
    print "| (2) Red                                    |"
    print "| (3) Green                                  |"
    print "| (4) Blue                                   |"
    print "| (5) Fade                                   |"
    print "| (6) Off                                    |"
    print "|--------------------------------------------|"
    print "| (0) Exit                                   |"
    print "\\--------------------------------------------/"


def clear():
    """Clears the console screen"""
    os.system('clear')

def sendColour( r, g, b ):
    """Sends a RGB value to the shiftbrite"""
    #build up string
    tmpData = "rgb("
    tmpData += str(r)
    tmpData += ","
    tmpData += str(g)
    tmpData += ","
    tmpData += str(b)
    tmpData += ")"
    ser.write(tmpData)
    print(tmpData)

    return

###
#
###

#Setup serial
ser = serial.Serial('/dev/tty.usbserial-A600dJm8',9600)
#Start main code LOOP
while True:
    clear()
    printMenu()
    userInput = raw_input('Selection:')
    if userInput == '1':
        sendColour(1023,1023,1023)
    elif userInput == '2':
        sendColour(1023,0,0)
    elif userInput == '3':
        sendColour(0,1023,0)
    elif userInput == '4':
        sendColour(0,0,1023)
    elif userInput == '5':
        sendColour(0,0,0) #To be implemented
    elif userInput == '6':
        sendColour(0,0,0)
    elif userInput == '0':
        print "Quitting"
        ser.close()
        exit()
    time.sleep(2)

Anyone got any ideas?

\$\endgroup\$
1
  • \$\begingroup\$ perhaps you need to call some kind of flush method on the ser object? \$\endgroup\$ Commented Oct 21, 2012 at 4:54

1 Answer 1

1
\$\begingroup\$

I think you are forgetting a line-feeds / carriage-return character to finish off the command. If you wrote a command in the terminal and hit enter they would be sent.

Change code to:

def sendColour( r, g, b ):
    """Sends a RGB value to the shiftbrite"""
    #build up string
    tmpData = "rgb("
    tmpData += str(r)
    tmpData += ","
    tmpData += str(g)
    tmpData += ","
    tmpData += str(b)
    tmpData += ")\n"
    ser.write(tmpData)
    print(tmpData)
    return

Note the \n.

The bit lash-cmdline.c file has this in it:

void doCharacter(char c) {
if ((c == '\r') || (c == '\n') || (c == '`')) {
    speol();
    *lbufptr = 0;
    doCommand(lbuf);
    initlbuf();
}

(rest of function not included)

This suggests a '\r', '\n' or a '`' character can be used to terminate the command.

\$\endgroup\$
5
  • \$\begingroup\$ Arrrah something so simple works perfectly now \$\endgroup\$ Commented Oct 21, 2012 at 5:18
  • \$\begingroup\$ Oh man I know the feeling! Glad it works now. \$\endgroup\$ Commented Oct 21, 2012 at 5:24
  • \$\begingroup\$ Thanks heaps it is now fully working from my phone controlling lights over an xBee network \$\endgroup\$ Commented Oct 21, 2012 at 8:03
  • \$\begingroup\$ Just curious, how are you interfacing the phone to the xBee network? \$\endgroup\$ Commented Oct 21, 2012 at 8:46
  • \$\begingroup\$ Oh its a bit crazy. but here is goes Phone -> Internet -> Flask(Python server on computer) -> Flask server sends serial to my xbee -> xbee to arduino \$\endgroup\$ Commented Oct 21, 2012 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.