ARDUINO / DASDUINO AND PYTHON

INTRODUCTION
Python is one of the most popular and most widely used programming languages today, and it is predicted that it will have even brighter future. What we are more interested in is that with it, you can “talk” to Dasduino/Arduino via a simple serial interface. All UNIX systems can read and write serial devices and that job is greatly faciliated by pySerial Library.
INSTALATION:PYTHON AND PYSERIAL
Before we start with serial port programming, let’s install Python and pySerial.
NOTE: In this tutorial, we will be using Python 2.7. If you want to use Python 3.x, you have a short tutorial on how to change the code, on the very end.
Short tutorial on how to install .tar file:
In the terminal, execute the following commands:
tar xfvz /Users
/*username*/
Downloads/pyserial-2.7.tar.gz
cd pyserial-2.7
sudo python
setup
.py install
If you’re using Windows OS, download .msi file.
ARDUINO CODE
The first step is to make Dasduino/Arduino to send data through the serial port. This is the same as when using the serial monitor, meaning we use Serial.print() function. We worked with several examples of the prints in the Serial Monitor, so we will use that. We will use the HC-SR04 ultrasonic module and how can be read in the HUM tutorial.
To begin with, let’s connect the module to Dasduino/Arduino:
We will make small changes in the code:
PYTHON CODE
Python code can be written and executed in various ways, and we will use IDLE for the simplicity of the presentation. On Windows, can be found at: Start – All Programs – Python – IDLE. If you use OS X, IDLE is located in the Application / Python folder.
The first step is to add pySerial library in the code:
import serial
After that, we open a connection:
croduinoSerial = serial.Serial(‘COM71’, 9600)
Keep in mind that ‘COM71’ and baud 9600 are specific for Windows computer that I use at this moment. More can be read on it pySerial API.
Next step is making a loop which will continuously read data from serial port:
while True: #infinite loop
if (croduinoSerial.inWaiting() > 0): #if any data exists
myData = croduinoSerial.readline() #save files in myData
print (myData)
The script can run with the F5 key. After that, in the Python Shell we can see the print of data that Dasduino/Arduino sent via serial port. If you have any further need to keep on processing received data, keep in mind that readline() stores variables as strings. You can change them in the float by using:
FROM PYTHON ON DASDUINO/ARDUINO
Sending data on Dasduino/Arduino with Python 2.x is really simple:
PYTHON 3.X
In Python 3.x, string is a Unicode itself. If we want the library to work successfully, we must convert it in bytes, which wasn’t necessary in Python 2.x
croduinoSerial.write(b
'text'
)
WHAT ELSE?
There are several very good open-source platforms for the realization of this communication. Most of them have a GUI (Graphical User Interface) framework for control, and some of them are running collected data in realtime in the form of a diagram. Our recommendations are:
Instrumentino
Python Firmata
GUI s pySerial i Matplotlib
The plan in the upcoming lessons is for you to make something similar by yourself, so we can understand more easily the communication between Dasduino/Arduino and Python. If you have difficulty performing that, feel free to leave a question or comment down below to improve this tutorial…