Example 2: Python Arduino Library

Overview

This example explores using Artemis to document a simple example script for the Python Arduino library PyDuino

Code

# @card title="Introduction" side-color=rgb(0,255,0)
''' @blockdoc
## PyDuino Bridge

![Image](https://content.arduino.cc/assets/arduino_logo_1200x630-01.png =300x150)  

-------------
**Author**: Daniel Saromo
**Summary**: Library for transparent bidirectional communication between Python and Arduino.
[Link](https://github.com/DanielSaromo/PyDuinoBridge/blob/master/python_example.py)
'''
# @stop

# @card title="Getting Started" side-color=rgb(0,255,0)
''' @blockdoc
#### Getting Started
If you haven't already, install the `PyDuinoBridge` library by running the following command:
```
pip install pyduinobridge
```
**Note** This requires Python 3.6 or higher.
'''
# @stop

# @card title="Setup" side-color=rgb(0,255,0)
''' @blockdoc
#### Choosing a Baud Rate
According to [Serial.Begin](https://www.arduino.cc/en/Serial.Begin), default Arduino baud rates are:

|  Baud Rates |
| :--: | 
| 300  |
| 600  |
| 1200 |
| 2400 |
| 4800 |
| 9600 |
| 14400 |
| 19200 |
| 28800 |
| 38400 |
| 57600 |
| 115200 |
'''
baud_rate = 115200
# @doc For our code we went with a baud rate of: BAUD_RATE
# @stop
 
# @card title="Setup" side-color=rgb(0,255,0)
''' @blockdoc
#### Choosing a Serial Port
Depending on your OS, use one of these serial port name types

|  OS | Serial Port |
| :--: | :--: |
| Windows | COM17 |
| Linux | /dev/ttyACM0 or /dev/ttyS80|
'''
serial_port = 'COM17'
# @doc For our code we went with serial_port: SERIAL_PORT
# @stop

# @card title="Initializing PyDuinoBridge" side-color=rgb(0,0,255)
''' @blockdoc
#### Initializing PyDuinoBridge
Now, let's go ahead and use these settings to initialize PyDuinoBridge.
We do so by writing:
# @linkedcode start="start_init" end="end_init"
'''
# @stop

# @marker start_init
print('Init!')
# @marker end_init

# @card title="Creating Test Data" side-color=rgb(0,0,255)
''' @blockdoc
#### Setting up some test data to send to the Arduino
Now, let's add some test data to send to the Arduino.
We will send the following test strings:
'''
testData = []
testData.append("<LED1,200,0.5>")
testData.append("<LED2,800,1.0>")
testData.append("<LED1,1000,0.0>")
testData.append("<LED2,100,1.0>")
testData.append("<LED1,100,0.0>")
testData.append("<LED2,1000,1.0>")
# @output table data=testData

# @card title="Sending Test Data" side-color=rgb(0,0,255)
# @doc Here are the strings our test program will send to the Arduino:
# @output table data=testData
''' @blockdoc
#### Sending the Test Data
Now, let's send the test data to the Arduino.
We do so by writing:
# @linkedcode start="start_send" end="end_send"
'''

# @marker start_send
dataFromArduino = 'data'
# @marker end_send

# @card title="Receiving Data" side-color=rgb(0,0,255)
# @doc Here is the response from the Arduino:
# @doc -------------
# @output heading data=dataFromArduino

# @card title="Shutting Down" side-color=rgb(255,0,0)
'''  @blockdoc
#### Shutting Down
Now, let's shut down the connection
We do so by writing:
# @linkedcode start="start_shutdown" end="end_shutdown"
'''
# @stop

# @marker start_shutdown
print('Shutting down')
# @marker end_shutdown

# @card title="Wrapping Up" side-color=rgb(255,0,0)
''' @blockdoc
#### Wrapping Up
We're done! We hope you liked this tutorial. If you'd like to learn more, you can do so here: [Link](https://github.com/DanielSaromo/PyDuinoBridge)
'''

Result


Did this page help you?