Newbie trying to understand Firmada and Pyduino

Hi all, I’m trying to find a Solution to send a few simple commands to the Arduino using python. So far I’ve played around with some code that uses pySerial to send ascii messages via USB.

Here is an example python command:

sdata.set(4,1)
This will set pin4 to HIGH. It works beautifully except…

Values over 255 don’t work as the ascii table wont go any higher(or maybe I’m missing someting here). I’m planning on driving a stepper motor with this so I will need a lot more than 255(steps)

Basically I’m looking for something that will take any string of characters or numbers from python and convert it so that when I do a Serial.read in the Arduino sketch, it gets the exact same values I typed in the python shell.

I’ve tried looking at SimpleMessageSystem, Messenger, and Firmata. SimpleMessageSystem and Firmata claim to work with Python. But there is no official Documentation for this and the forums have me jumping through lots of hoops. To be honest the discussions are way over my head.

I’m hoping there is a simple answer or approach to this. Can anyone shed some light on this topic?

Here’s the Arduino code I’ve been playing with that I mentioned above:

//////////////////////////////////////
// simpleSerial
//////////////////////////////////////

/*
//---------------
// Description
//---------------
The simpleSerial sketch is designed to be used in conjunction with the sdata python script.
The sdata script allows python to send ascii data to the Arduino.
The commands from the python shell are then converted on the fly to ascii code for you.

Important Note:
This code would need to be modified if values higher 255 need to be sent in one go.

//---------------
// Requirements.
//---------------
1. Python must be installed.
2. The pySerial package for Python must be installed.
3. The sdata.py script must be placed here: C:\Python25\Lib\sdata.py

//------------------------
// Instructions for use.
//------------------------
1. Upload this sketch to the Arduino.
2. Open a Python Shell
3. Type "import serial"
4. Type "import sdata"
5. Use the sdata script as instructed:

Sets the specified Pin and it's Value on the Arduino Board.

//----------------------
// sdata.py Arguments.
//----------------------
(pin) The output pin, an integer from 2-13
(value) Sends the HIGH or LOW command for the specified pin. Type "1" to send HIGH and "0" to send LOW.

//---------------------
// Python Examples
//---------------------

>>> sdata.set(2, 1)
Sets pin #2 to a value of HIGH

>>> sdata.set(4, 0)
Sets pin #4 to a value of LOW

*/

int pins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // an array of pin numbers
int num_pins = 12; // the number of pins (i.e. the length of the array)
int startbyte; //start byte variable
int i; // iterator variable
int userInput[3]; // user input variable


void setup() { //sets up the output pins and serial connection.
int i;
for (i = 0; i < num_pins; i++) // the array elements are numbered from 0 to num_pins - 1
pinMode(pins[i], OUTPUT); // set each pin as an output
Serial.begin(115200); // open serial connection
}

void loop() { // The main loop
if (Serial.available() > 2) { // wait for serial input (min 3 bytes in buffer)
startbyte = Serial.read(); //read the first byte
if (startbyte == 255) { // if it's really the startbyte (255)
for (i=0;i<2;i++) { // then get the next two bytes
userInput[i] = Serial.read();
}
digitalWrite(userInput[0],userInput[1]); // write to the respective pin(userInput[0])
// with the repsective value (userInput[1])
}
}
}

And here’s the Python script to go with it.

################################################
# Module: sdata.py
# by Daniel Thompson
# http://danthompsonsblog.blogspot.com/
# Created: 27 September 2008
# inspired by the Brian D. Wendt's servo.py script
# found at http://principialabs.com/

'''
Provides a serial connection abstraction layer
for use with Arduino "simpleSerial" sketch.

Note: This script requires the pySerial package to function properly.
you can download it here. http://pyserial.wiki.sourceforge.net/pySerial
'''
################################################

import serial
# This variable could require a different value depending
# on you Operating system. COM4 usually works for windows.
usbport = 'COM4'
ser = serial.Serial(usbport, 115200, timeout=1)

def set(pin, value):
'''Sets the specified Pin and it's Value on the Arduino Board.

Arguments:
pin
the output pin, an integer from 2-13
value
sends the HIGH or LOW command for the specified pin.
Type 1 to send HIGH and 0 to send LOW.

(e.g.) >>> sdata.set(2, 1)
..."sets pin #2 to a value of HIGH"'''

ser.write(chr(255))
ser.write(chr(pin))
ser.write(chr(value))

The python code that you posted sends the character representation down the serial line to the Arduino board. What you want to do is send the raw binary data down the line as 2 bytes. That will give you a range o 0 to 65535. Of course this is easier to represent in Hex.

I think this may work in Python:

Serial.write(0xFFFF)

When you receive it with the Arduino take the high byte and shift it 8 bits left then && it with the low byte. That will give you the total you send using only two bytes.

Hey thanks for the reply,

So I found this bit of code which converts from an integer to binary in python.

def int2bin(n):
    '''convert denary integer n to binary string bStr'''
    bStr = ''
    if n < 0:  raise ValueError, "must be positive"
    if n == 0: return '0'
    while n > 0:
        bStr = str(n % 2) + bStr
        n = n >> 1
    return bStr
 
print int2bin(255)   # 11111111
print int2bin(5000)  # 1001110001000

Forgive the stupid questions, but cant I just send what I’ve converted straight over the serial port in one hit if it’s binary?

So… something like 1001110001000 which is (5000). How do I actually make use of this binary data?
What do you mean send it as 2 bytes? My head hurts. I think I need to go learn a little “bit” more before I go any further ::slight_smile:

Also, values will need go much higher than 65535 it’s a micro stepper driver (1600 steps per rev) and it’s for a motion controlled camera (on tracks).

Will I always need to rebuild the string of numbers I send over serial back into a full number before I can use it in the Arduino code?