| « node.js and Fedora | Source code und geshi » |
Arduino controlled by a GTK interface
For my little Arduino and relay project I started with the usage of the Serial monitor to switch the relay and the LED.
What's about a nice GUI for doing this? Using Python to grab data from Arduino is easy. The only missing part is the GUI and some code to send information to the Arduino.
As the existing part was written in Python, I'm going on with GTK3. I just want a simple windows with a toggle button. That's all. If you are not familiar with the successor of pygtk, reading this tutorial is a good starting point.
This blog post will be some kind of a tutorial/example for developing an user interface for Arduino platform. For simplicity only a LED can be controlled but the UI is easy expandable if you want. So the tools is called "Arduino LED commander" ;-).
The hardware setup is simple. You need an Arduino board, a resistor, and a LED. Connect the LED to the digital pin 13 and GRD. Check out the Blink tutorial for the layout.
On the software side the following tools are needed: Python (2.7.x is just fine), pySerial, Glade 3, and their dependencies.
The design of the UI in Glade is very simple. Drag-and-drop the component you want to the window. In Glade my UI looks like this:
If you want to skip this step, just download my Glade file.
The code can be divided in several parts. I will not go to much into details because everything is more or less standard stuff which was glued together. In the import section you see "serial", this is the lib from pySerial.
import os import sys import serial import time import signal from gi.repository import Gtk
I declared to constants for the serial connection. The port aka interface and the communication speed.
PORT = '/dev/ttyACM0' SPEED = 9600
The largest part is needed for setting up the UI. The glade file is loaded, objects are created, and signals are connected.
class UI: def _ _init_ _(self): self.builder = Gtk.Builder() self.builder.add_from_file(os.path.join(os.getcwd(), 'data/arduino-led.ui')) self.window = self.builder.get_object('dialog1') self.aboutdialog = self.builder.get_object('aboutdialog1') self.bt_exit = self.builder.get_object('bt_exit') self.tbt_state = self.builder.get_object('tbt_state') self.imagemenuitem5 = self.builder.get_object('imagemenuitem5') self.imagemenuitem10 = self.builder.get_object('imagemenuitem10') self.window.connect('delete-event', self.quit) self.tbt_state.connect('toggled', self.on_button_toggled) self.bt_exit.connect('clicked', self.quit) self.imagemenuitem5.connect('activate', self.quit) self.imagemenuitem10.connect('activate', self.show_aboutdialog) self.window.show()
The toggle button is the headliner. This button knows two states, pressed and released. Pressed is On, released is Off. When the button is clicked something needs to be sent to the Arduino. The self.send_command() is responsible for that.
def on_button_toggled(self, button): if button.get_active(): state = ['1', 'on'] button.set_label(state[1].upper()) self.send_command(state[0]) else: state = ['0', 'off'] button.set_label(state[1].upper()) self.send_command(state[0])
The send_command() is invoked by the toggle button. Depending on the value of state, a O or a 1 is sent through the serial connection to the Arduino.
def send_command(self, val): connection = serial.Serial( PORT, SPEED, timeout=0, stopbits=serial.STOPBITS_TWO ) connection.write(val) connection.close()
To lauch the Arduino LED commander, open a terminal and use a command like the one below depending on your naming schema.
python arduino-led.py
If everything is ok, the UI shows up.
And the UI in action:
Video: http://youtu.be/WZ9FKHfVKRY
Website: http://www.affolter-engineering.ch/index.php?page=led-commander
Source: http://www.gitorious.org/arduino-led
1 comment
Thanks for the tutorial.
Just a small point, a run time warning says thatb this is GTK2 code and that it should be ported to GTK3. What needs to be changed?



