|« node.js and Fedora||Source code und geshi »|
Arduino controlled by a GTK interface
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.
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.upper()) self.send_command(state) else: state = ['0', 'off'] button.set_label(state.upper()) self.send_command(state)
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.
If everything is ok, the UI shows up.
And the UI in action:
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?