Recently we posted an article on how to use ITEAD OS SDK to implement GPIO and UART functions and we received a lot of feedback that such simplified operations are quite convenient. Many guys wondered whether SDK functions also support Python, we are here to clarify that the official version which will be released next month supports direct import operation of Python, while the current BETA version does not.
Although direct Python operations were not provided, as Python supports so dynamic link library import, you can also use GPIO and UART functions after importing in the BETA version. Here I would like to demonstrate how to use these functions in Pyhton and to realize interaction between GUI graphical interface and functions of external devices with use of TKinter.
In the first example, we use GPIO to drive the LED, where we used Tkinter to make graphical control interface, and two buttons were offered. After you press the corresponding button with the mouse, Iteaduino Plus will turn on or off the LED.
First, same with C program we mentioned in the last post, we still need to create a new file suffixed with ". py", such as "LED.py".
Next, open the file and write our code :
import Tkinter from ctypes import * clib=cdll.LoadLibrary("/usr/local/lib/libwiringIO.so") def ledon(): clib.digitalWrite(c_int(144),c_int(1)) def ledoff(): clib.digitalWrite(c_int(144),c_int(0)) clib.pinMode(c_int(144),c_int(1)) root=Tkinter.Tk() lb_title=Tkinter.Label(root,text="LED TEST") lb_title.pack() btn_on=Tkinter.Button(root,text='LED ON',command=ledon) btn_on.pack(side=Tkinter.RIGHT) btn_off=Tkinter.Button(root,text='LED OFF',command=ledoff) btn_off.pack(side=Tkinter.LEFT) root.mainloop()
Code starts, in order to use SDK functions written in C, we must import Ctype standard library first, and then import so file (the dynamic link library under Linux) including GPIO operation functions in the way it offers.
After importing, we can use "pinMode" and "digitalWrite" functions to achieve pin-out configuration and output level control. For how to use Tkinter to build interfaces and buttons, you can find details HERE.
Save and exit, then use shortcut key combinations "ctrl + alt + T" to bring up the terminal window. Then browse to the location where the file is saved, and use command "python led.py" to run the code above – note that root privileges must be utilized to run this command, or it may prompt need of root privileges.
After running, control window appears as shown below. Click button LED ON with the mouse, LED which connects to the board will be turned on; click button LED OFF, LED will be off. After replacing the LED module with relay module, you can control switches of other external appliances.
The second example is still to demonstrate how to use SIM900 to make phone calls, but here we used Python and TKinter to build a graphical interface, where we can enter the phone number, then press the button and dial the numbers.
Create a file "gsm.py", and then enter the following code:
from Tkinter import * from ctypes import * clib=cdll.LoadLibrary("/usr/local/lib/libwiringIO.so") def call(): at='ATD' fen=';' callnumber=at+entry.get()+fen clib.SerialPrintln(c_int(13),c_char_p(callnumber)) clib.SerialBegin(c_int(13),c_int(9600)) root=Tk() entry=Entry(root) entry.pack() send=Button(root,text='CALL',command=call) send.pack() root.mainloop()
Still it is a quite simple piece of code and also imports so file including UART functions with in the way that Ctype standard library provides. Next, it is to build input box. When the button is pressed down, function "SerialPrintln" will be used to send AT commands.
Remember to press down power on button on SIM900 module to make the module start before running the program. Please also note that connection of the module should be done after turning off Iteaduino Plus, as plugging in and out during running will cause damages to the boards and modules.
Save and run, the following interface will appear, enter the phone number you want to call in the input box, then press the button CALL, Iteaduino Plus will send commands through UART to control SIM900 module dial this number.
Fabrication of video has not been finished yet, and it will be added to the front after completion. Later on, we will introduce how to use PWM function to make breathing light effects and to make dragging progress bar interface with Python to control brightness of LED, so stay tuned to our blog.