Python is a high-level, interpreted, object-oriented scripting language. One of the biggest tenants of Python is to have a clear, easy-to-understand syntax that reads as close to English as possible. That is, you should be able to read a Python script and understand it even if you haven’t learned Python. Python also has less punctuation (special symbols) and fewer syntactical machinations than other languages.
Here are a few of the key features of Python.
- An interpreter processes Python at runtime. No compiler is used .
- Python supports object-oriented programming constructs by way of classes and methods.
- Python is a great language for the beginner-level programmers and supports the development of a wide range of applications.
- Python is a scripting language but can be used for a wide range of applications.
- Python is very popular and used throughout the world giving it a huge support base.
- Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.
Python was developed by Guido van Rossum from the late 1980s to the early 1990s at the National Research Institute for Mathematics and Computer Science in the Netherlands and maintained by a core development team at the institute. It was derived from and influenced by many languages including Modula-3, C, C++, and even Unix shell scripting languages.
A fascinating fact about Python is it was named after the BBC show, “Monty Python's Flying Circus” and has nothing to do with the reptile by the same name. Quoting Monty Python in source code documentation (and even a humorous diversion for error messages) is very common and while some professional developers may cringe at the insinuation, it's considered by Pythonistas as showing your Python street cred. If you like Monty Python, I encourage you to use snippets from the shows in your code. One place I like to have fun with is printing messages. My favorite goes something like this:
> DUPLICATE FILE ERROR: He says they've already got one!
Some may wonder how a language like Python could possibly be helpful in writing IOT solutions. The answer to that is a cool product called MicroPython. In short, MicroPython is a condensed, optimized code of Python 3 that has been loaded in hardware. This means rather than having to have an interpreter run on an operating system to execute the Python code, the MicroPython chip can run the Python code directly on the hardware. No operating system is needed. In fact, MicroPython has basic file I/O built in.
We’ll learn more about MicroPython in the next chapter. It’s an exciting new option for those who want to explore IOT but don’t want to learn a complex programming language or spend a lot of time learning new operating systems, tools, and hardware. But first, let’s look at how easy it is to use MicroPython.
Online Python Simulator
For those of you who are eager to get a taste of what it is like to use Python to control hardware, the good folks over at micropython.org have an online, interactive MicroPython simulator using one of the most popular MicroPython boards, the pyboard. When you visit the site, you will see an interface that has a live video image of a pyboard connected to several devices including a servo, LEDs, an LCD panel, and even an SD drive.
The simulator has several sample scripts (Python programs are called scripts) that you can try out. There are scripts to turn LEDs on, print to the LCD, and even move the servos. The simulator is designed to allow you to select a script and submit it to a queue to run. If there are no other scripts in the queue, you will see the script you selected run almost right away. Figure 1 shows an example of a simple script to turn an LED on. In this case, it is the LED on the board itself.
Figure 1. MicroPython Simulator - LED on (courtesy of micropython.org)
Notice in the image we see several sections or panels. Clockwise from top left we see the current code (Current code) and current output from the code that is running on the board (Current output), a video feed of the board in action, an area to display the output of the script (using the
print() function) for the code you executed (My output), and a panel (My code) that allows you to scroll through the available example scripts and execute them. The following buttons allow you to control the script.
- Run!: Executes the current code in the My Code panel
- clear: Clears the My Code panel
- left arrow: Cycle back through the list of example scripts
- right arrow: Cycle forward through the list of example scripts
One of the really cool things about the simulator is you can use the My code panel and write your own code! Just click the clear button and position your cursor on line number 2 (below the import statement) and start typing. When you’re ready, click the Run! button. Figure 2 shows an example of a script that prints two lines showing code I wrote to run on the board.
Figure 2. MicroPython Simulator - Custom code (courtesy of micropython.org)
Figures 3 and 4 show two additional example scripts.
Figure 3. MicroPython Simulator – LCD (courtesy of micropython.org)
MicroPython Simulator – UART Calculations (courtesy of micropython.org)
While you may not fully understand the Python code in the examples, I suggest you take a few moments to try out the scripts so you can see how much fun it is (and how little code it requires) to control hardware with MicroPython. If nothing else, the simulator should give you an appetite to learn more about MicroPython.
The Internet of Things is an exciting new world for us all. Those of us young in heart but old enough to remember the Jetson’s TV series recall seeing a taste of what is possible in make-believe land. Talking toasters, flying cars that spring from briefcases, and robots with attitude notwithstanding, television fantasy of decades ago is coming true. We have wristwatches that double as phones and video players, we can unlock our cars from around the world, find out if our dog has gone outside, and even answer the door from across the city. All of this is possible and working today with the advent of the IOT.
In my blog, we discovered what the IOT is and saw some examples of well-known IOT solutions. We also discovered how Microsoft is opening doors for Windows users by expanding its Windows 10 operating system to the IOT via the
Raspberry Pi hardware. This is a very exciting opportunity for people who do not want to learn the nuances of a Linux-based operating system to explore the world of hardware and IOT from a familiar and well-understood platform.