Controlling Arduino Motors via Raspberry-Hosted Website (Part 2)

We want to send commands to our remote-control Arduino car. One way to communicate directly with Arduino from Python is through Serial interface. For this we need a Python library called pySerial, which allows direct signal manipulation via serial ports.

Installation

PySerial is available as a standard Debian package.

sudo apt-get install python-serial

Sending messages

Open python in command line

python

>>> import serial
>>> ser = serial.Serial(0)  # open first serial port
>>> print ser.portstr       # check which port was really used
>>> ser.write("hello")      # write a string
>>> ser.close()             # close port

Receiving messages

>>> ser = serial.Serial('/dev/ttyACM0', 19200, timeout=1)
>>> x = ser.read()          # read one byte
>>> s = ser.read(10)        # read up to ten bytes (timeout)
>>> while 1:
...  line = ser.readline()   # read a '\n' terminated line
...  print line
>>> ser.close()

Arduino side

Make sure your Arduino board is pre-uploaded with codes to listen and print what it received on the Serial status panel.

int incomingByte = 0;   // for incoming serial data
void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}
void loop() {
    // send data only when you receive data:

    if (Serial.available() > 0) {

        // read the incoming byte:

        incomingByte = Serial.read();
         // say what you got:

        Serial.print("I received: ");

        Serial.println(incomingByte, DEC);

    }

}

 


Once you understand the basic concept of sending commands via Serial port, please follow the instruction on how to put them all together:

Raspberry Internet Rover Part 1 – Robotics Basics
Or

Download the complete Arduino Sketch, and work out ways to talk to it.

[post_view]

(Visited 1,097 times, 1 visits today)

8 thoughts on “Controlling Arduino Motors via Raspberry-Hosted Website (Part 2)”

  1. The websocket server gives me “segmentation fault”. It runs fine on Ubuntu, but crash when started on Raspbian.

    I think the best alternative is to create a UDP Packet listener in Python, and then send messages via socket.io in NodeJS program. Call the NodeJS functions from within a HTML file, and transmit the messages to Python which then relay it to Arduino board via PySerial.

    It sounds too overwhealmhing, here is the summary:

    web client (html)
    |
    V
    Raspberry Pi (nodeJS server->Python UDP server->Serial Class)
    |
    V
    Arduino Board like Romeo (arduino)

    NodeJS with CoffeeScript servers are lightweight and can be hosted via Lighttpd on Raspbian without abusing the limited ARM processor too much.

  2. johnny says:

    didn’t work for me 🙁

    1. John Dowe says:

      which part doesn’t work? Any error?

      1. johnny says:

        ser =serial.serial(0) and i get module object has no attribute serial

  3. johnny says:

    >>> import serial
    >>> ser = serial.Serial(0)
    doesn’t recongnise serial..i don;t know why…

    another question…does it work with an arduino uno and a ardumoto shield?

  4. Al B says:

    Nice post! We built a similar project. However, we installed an Android device onto a RC car and used an IOIO board instead to control it autonomously. Here is the preliminary result:

    http://youtu.be/vkvkfcqEUkk

  5. Dipto Pratyaksa Dipto Pratyaksa says:

    After you have pyserial module installed, open the right USB / serial port like ser=serial.Serial(“/dev/ttyACM0”). It is case senstive too, make sure you have S with caps on.

  6. Dipto Pratyaksa Dipto Pratyaksa says:

    If you want pure Python, another method would be to use Tornado websocket server as discussed in this post: https://www.linuxcircle.com/2015/04/13/raspberry-pi-pololu-maestro-python3-tornado-servo-control-part-2/

Comments are closed.