Python is a great general purpose high level programming language. It is beneficial for many reasons, one being that its simple syntax rules allows the language code to be readable and maintanable. It is fairly easy to develop also due to the wide array of available packages to support most types of development. When it comes to embedded development you may be asking why Python over C or C++. Each has their own advantages of course, mainly coming down to the debate of development speed vs run time of embedded systems. Some developers look to python for proof of concerpt purpsoses while looking at C or C++ for production development. Each has their advantage and disadvantage in this scenario. Here we are going to show hwo userful and easy Python can be to get you up and running with a fairly complicated example on the MaaXboard.
The following hardware is required for following along with this blog.
a) MaaXBoard SBC - www.avnet.me/maaxboard-buy
b) USB Serial converter or some other way of getting terminal access such as SSH or HDMI terminal connection
c) 16GB SD Card Link
d) USB Type C Power Supply Link
Now that you have the required hardware lets look at where to find the image required to run this demo.
Download the Debian Out of Box image by going to www.avnet.me/maaxboard Reference Designs -> Out of Box Image -> Debian Linux Out of Box Image.
You can then use a program such as Win32DiskImager or Etcher.io to flash the image onto the 16GB SD Card.
Insert the SD Card into the board. Plug in the device you wish to get terminal access ffrom whether that is via a HDMI monitor w/ mouse and keyboard or via SSH or a USB UART TTL converter. Plug in the USB Type C power supply to power on the board.
3.Exploring Existing Python Libararies in the MaaXBoard Debian Image
a) Now that you have booted your board, use the login credentials to login to the image (root/avnet)
b) Use the ls command to explore the available python libaries
Once doing so youcan see the supported Python installs. In the MaaXBoard out of box case it supports Python 2.7 / Python 3 / Python 3.7
Later on in this blog we are going to be exploring using Python3, so lets dig a little bit deeper in regards to the distribution packages with this Python3 install.
We get a list of available distribution packages with this install
In the event of needing an additonal package in your application, once you install it using apt-get install you can run the same LS command as above and see the newly added package here.
For example we are going to be using the python serial application. Connect your board up to the internert via Lan or Wifi and run the following command.
After the new package is installed run the same ls command above to see the newly added package "serial"
4. Running a Basic Python3 example HELLO PYTHON
Now that we have explored the current Python3 distribution that is installed, lets get into how to create and run a basic application.
Lets start by going to our boot directory so we can access this file if we want to via the SD Card on the PC if need be.
In this example we are going to use vi editor to create out python 3 application. Start by using vi to create a the file python3_hello.py
doing so will create a file you can edit called python3_hello.py
Now on your keyboard press the 'i' key to put your file into insert mode so you can type and edit the file adding in print("Hello_Python")
Now to save and exit the file press the following keys on order
a) Escape Key (exits insert/edit mode)
b) Shift + : (opens up vi interface)
c) wq (write & quit)
Now that we have created and saved this python3 application lets run it! python3 python3_hello.py
Congrats you have now written/compiled your first application successfully on your MaaXboard! Now lets look into running more advanced applications on your embdded system
5. Using advanced Packages.
In this example we are going to create a webserver using an available python package called Flask. In this instance we are going to install this package using pip3. However first we must install pip3.
a) Make sure your MaaXBoard is connected to either Lan or Wifi
b) Update your existing packages
c) Install pip3, this will take some time.
Installing Flask using pip3
a) using pip3 install flask using pip 3 (pip3 install flask)
Now that flask is installed lets make a new directory under the boot area
a) mkdir webapp to make the webapp directory, you can then use ls to verify it was created
Now that we have successfully installed flask and created the webapp directory lets change directory to the webapp directory and create the applcation that will take adavantage of the flask package we installed.
a) cd webapp
b) Similiarly as what was done with the hello word example use vi to create / edit an application labeled app.py. Refer to below with what to put in this application. Now save and exit the vi editor. PLEASE make sure everthing is copied exactly as shown
from flask import Flask
app = Flask(__name__)
return 'Hello world'
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')
c) Now that you have created the application, use ifconfig to determine wlan0 IP address so we can access the webserver over the local network. In my instance it was 192.168.0.15. Please note if you dont have an IP address you will need to connect your MaaxBoard to the internet via Lan or Wifi.
d) Now we get to run the application and then view the webserver from our PC.
i Run the applciation similiarly to the Hello Python example earlier
ii. Assuming your PC is on the same network as the MaaXBoard, open up a webrowser and type in the IP address we found in step C to open the webserver we just created.
If everything was done correctly you will see Hello World in the webrowser!
e) Congrats you just learned how to take the package Flash and run a basic webserver on your MaaXBoard! If you want more information in regards to how to use the Flask package for more exciting webserver implementations please see here for more details! https://towardsdatascience.com/python-webserver-with-flask-and-raspberry-pi-398423cc6f5d
This is just one example of how python can get you up and running off of existing packages that are freely available. In additon to the package flask there are various others that can be of use in your future development.
Here is a neat list of a few available packages that can kick start your next development.
Feel free to ask questions and comment about your experiencing with using python!