Joining Raspberry Pi and Arduino applications with SIMPL
eightLeds.py Sender
The other half of this sample application is a SIMPL sender written in Python called eightLeds.py
(Listing 2).
eightLeds.py Annotation
Lines 2-9 import the necessary modules.
Line 3 imports the SIMPL module, which is necessary to send messages to the Arduino.
Line 11 defines a variable called BRINKY_RITE
, which will be used as the message token.
Lines 16-23 define a callback function that handles the button icons and states.
Lines 26-44 define another callback invoked when the submit button is pressed.
Lines 28-32 organize message variables according to the message structure definition at the top of the Arduino sketch eightLeds.ino
.
Line 35 loads the message into the internal outgoing message buffer.
Line 38 sends the message to the Arduino receiving program eightLeds.ino
. The send()
takes an argument returned by an earlier nameLocate()
call on the ARDUINO program.
Lines 48-54 deal with a command-line argument, if present.
Line 61 creates an instance of SIMPL
, on which we will hinge all aspects of SIMPL messaging within the script.
Line 64 locates the SIMPL name ARDUINO attached to the sketch eightLeds.ino
via tclSurrogate
.
Lines 74-88 develop the Tk()
GUI for user input.
Listing 2
eightLeds.py
01 # import necessary modules 02 import sys 03 import csimpl 04 if sys.version < '3': 05 from Tkinter import * 06 iimport tkMessageBox 07 else: 08 from tkinter import * 09 from tkinter import messagebox 10 11 BRINKY_RITE = 0 12 global leds # a series of eight button widgets 13 global numLeds 14 15 # a callback for changing the text value of a led button 16 def hndlChange(event): 17 # make certain that entered text is black 18 event.widget["foreground"] = "black" 19 # decide on the next character after a left button mouse click 20 if event.widget["text"] == "OFF": 21 event.widget["text"] = "ON" 22 else: 23 event.widget["text"] = "OFF" 24 25 # a callback for the send button 26 def hndlSubmit(nee, receiverId): 27 # assemble the message 28 outToken = BRINKY_RITE 29 outValues = 0 30 for num in range(numLeds): 31 if leds[num]["text"] == "ON": 32 outValues |= (1 << num) 33 34 # load the message to be sent to the Arduino sketch 35 nee.packMsg(nee.BIN, "hb", outToken, outValues) 36 37 # send the message 38 retVal = nee.send(receiverId) 39 if retVal == -1: 40 if sys.version < '3': 41 tkMessageBox.showerror("Error", nee.whatsMyError()) 42 else: 43 messagebox.showerror("Error", nee.whatsMyError()) 44 sys.exit(-1) 45 46 # operational start of the program 47 # check command line 48 if len(sys.argv) == 1: 49 rName = "ARDUINO" 50 elif len(sys.argv) == 2: 51 rName = sys.argv[1] + ":" + "ARDUINO" 52 else: 53 print("Incorrect command line") 54 sys.exit(-1) 55 56 # initialize some variables 57 numLeds = 8 58 leds = [None] * numLeds 59 60 # make an instance of Simpl 61 nee = csimpl.Simpl( "EIGHT_LEDS", 1024) 62 63 # name locate the C program SIMPL receiver 64 receiverId = nee.nameLocate("sys.argv[1]:ARDUINO") 65 if receiverId == -1: 66 err = nee.whatsMyError() + ": is the receiver program running?" 67 if sys.version < '3': 68 tkMessageBox.showerror("Error", err) 69 else: 70 messagebox.showerror("Error", err) 71 sys.exit(-1) 72 73 # initialize Tk for graphics 74 root = Tk() 75 rowframe = Frame(root) 76 rowframe.pack(fill=BOTH) 77 for num in range(numLeds): 78 leds[num] = Button(rowframe, borderwidth=2, relief=SOLID, justify=CENTER, bg="Yellow", \ fg="Black", text="OFF", font=("Times", 12, "bold"), width=3) 79 leds[num].bind("<Button-1>", hndlChange) 80 leds[num].pack(side=LEFT) 81 82 # the bottom frame of buttons 83 rowframe = Frame(root) 84 rowframe.pack(fill=BOTH) 85 Button(rowframe, justify=CENTER, text="Send", font=('Times', 12, 'bold'), \ command=(lambda: hndlSubmit(nee, receiverId))).pack() 86 87 # handle user input 88 root.mainloop()
eightLeds.py In Brief
The first thing eightLeds.py
does is create an instance of SIMPL. As with many objects in Python, you can create an instance of a csimpl
object, or you can program it procedurally. Using an object is convenient in this case, because all things SIMPL will hinge off of it. Creating an instance effectively sets up the script for SIMPL messaging while some ugly details are looked after under the hood.
Because this script is a SIMPL sender, it needs to find the SIMPL receiver (ARDUINO) to which it intends to send a message. This is accomplished with the nameLocate()
method. If nameLocate()
fails, then the prospective receiver cannot be found. Of course, the receiver must be up and running if the sender has any hope of finding it.
Running the script without a command-line parameter sets the name locate to look on the local host. By using an optional command-line parameter, you can set the name locate to look just about anywhere. In fact, it may correspond to line 21 in the Arduino sketch eightLeds.ino
.
Next, a simple little GUI is presented for the user to select which LEDs are to be either On or Off. After the selections have been made, the message is sent to the Arduino, and the eight LEDs are set to their respective states.
Buy this article as PDF
(incl. VAT)