Daniel 'MaTachi' Jonsson

Category Archives: Arduino

Read Serial Data Transmission in Python

One year ago I wrote a couple of posts about or related to my Arduino board:

I picked it up again from my closet and since I wanted a remote control again. :)

Python script

Since I never stored the Python script that reads the data that the Arduino board outputs from its serial port to the computer’s USB port I had to rewrite it:

#!/usr/bin/python3

from serial import Serial
import subprocess

ser = Serial('/dev/ttyUSB0', 9600, timeout=1)
print("connected to: " + ser.portstr)

while True:
    # Read a line and convert it from b'xxx\r\n' to xxx
    line = ser.readline().decode('utf-8')[:-2]

    if line:  # If it isn't a blank line
        print(line)
        if line == '520':
            subprocess.call(["xte", "key Up"])
        elif line == '620':
            subprocess.call(["xte", "key Down"])
        elif line == '110':
            break

ser.close()

About the script

To install the serial module in Ubuntu for Python 3, run the following line in the terminal:

sudo apt-get install python3-serial

Documentation and installation instructions for other platforms can be found on the module’s site:

The subprocess module and the lines about xte (xautomation) aren’t needed, but read my post Control Keystrokes and Mouse from the Command-Line with Xautomation and you will understand why those lines of code are included. To install Xautomation in Ubuntu, run:

sudo apt-get install xautomation

You can find out which port you should listen to by checking the Arduino IDE’s menu Tools > Serial Port. Or you can check in the /dev folder for something fitting.

Other stuff

How to add a library to Arduino:

If you get an error regarding tying to include WProgram.h if you try to use the infrared remote library, checkout this fix:

Infrared Remote Library For Arduino & My IR Receiver Program

In the Arduino kit that I bought not long ago were an IR remote control and an IR receiver included. And I have found a really good library for Arduino that makes decoding the IR signals from the remote control really easy. So I have used this library and coded a short program to my Arduino card that reads the IR signals and then sends more logical signals to the computer through the USB cable.

Here is a link to the page where I found the library: http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html

Here is code that I have written for my Arduino board:

#include <IRremote.h>

int RECV_PIN = 11;
String in;
String cur = "";
String cur2 = "";

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {

    in = String(int(results.value), HEX);
    irrecv.resume(); // Receive the next value
    cur2 = "";

    if (in != "ffff") {
      if (in == "a25d") {      // ON/OFF
        cur = "11";
      }
      else if (in == "629d") { // Mode
        cur = "12";
      }
      else if (in == "e21d") { // Sound ON/OFF
        cur = "13";
      }
      else if (in == "22dd") { // Play/Pause
        cur = "21";
      }
      else if (in == "2fd") {  // Back
        cur = "22";
      }
      else if (in == "c23d") { // Forward
        cur = "23";
      }
      else if (in == "e01f") { // EQ
        cur = "31";
      }
      else if (in == "a857") { // VOL-
        cur = "32";
      }
      else if (in == "906f") { // VOL+
        cur = "33";
      }
      else if (in == "6897") { // 0
        cur = "41";
      }
      else if (in == "9867") { // RPT
        cur = "42";
      }
      else if (in == "b04f") { // U/SD
        cur = "43";
      }
      else if (in == "30cf") { // 1
        cur = "51";
      }
      else if (in == "18e7") { // 2
        cur = "52";
      }
      else if (in == "7a85") { // 3
        cur = "53";
      }
      else if (in == "10ef") { // 4
        cur = "61";
      }
      else if (in == "38c7") { // 5
        cur = "62";
      }
      else if (in == "5aa5") { // 6
        cur = "63";
      }
      else if (in == "42bd") { // 7
        cur = "71";
      }
      else if (in == "4ab5") { // 8
        cur = "72";
      }
      else if (in == "52ad") { // 9
        cur = "73";
      }
    }

    if (in == "ffff") { // Presses down a button
      Serial.println(cur + "d");
    } else if (cur.length() == 2) {
      Serial.println(cur + "0");
    }

  }

  delay(100);
}

I’m not sure if this program is so easy to understand, but I hope that it’s at least little help if you are setting up your own program for reading IR signals (and later controlling your computer).

Basically will the loop decode a signal, convert it to HEX and then check what button was pressed down. If I press a button on my remote control it will send a normal, unique signal (which in hex is something like a25d or 629d). But if I keep it pressed down it will keep on sending ffff signals, regardless of what button is kept pressed down. Therefore I have to save what button that was initially pressed down, which I do in cur.

The normal signals will result in numbers like 11, 12, 33 and 72 are saved as cur, and behind that is a really simple system. The first digit is the button row of the remote control, and the second digit is the the column. So as you may understand, I have 3 x 7 buttons on my remote control.

Before sending the number 22, 33, or something similar, to the computer, is 0 added to the end if the button was just pressed down or d if it already was pressed down in the previous loop.

So if press the button on my remote control that says 8 (row 7, column 2) the program will in the first run through send the message 720 to the computer. If I keep it pressed down will the program keep on sending 72d to the computer. If I bring up the Serial Monitor in the Arduino software will the output be:

720
72d
72d
72d
72d
72d
72d
72d

I apologize for my crappy English. ^.^