In this tutorial we are going to remotely control an Arduino from a python-script
The hardware-components will be modeled in an object-oriented manner to provide a nice and reusable interface
In order to remotely control the Arduino it has to be flashed with a firmware that accepts and executes commands
1 at_commands[] = {
2 {.name= "AT", .cb= atcb_at},
3 {.name= "AT+SET_OUT", .cb= atcb_setout},
4 {.name= "AT+SET_IN", .cb= atcb_setin},
5 …
6 {.name= NULL, .cb=NULL}
7 };
The sketch above accepts AT-style commands that can be used to read and write digital pins
Upload the sketch from the previous slide to
your Arduino and configure the
serial monitor
to send a carriage return ␍
and a newline ␊
character at the end of each line, and to
use a speed of 115200 Baud
AT+SET_OUT=13
AT+WRITE_HIGH=13
Enter the commands above into the serial monitor to turn on the Arduino on-board LED
The Arduino should
acknowledge both commands with an OK
and turn on the LED
Study the commands in the at_commands
array
find the commands that let you read out the
HIGH
or LOW
state of pin 10
Use a piece of wire to change the state of pin 10
1 duino= Arduino('/dev/ttyACM0')
2
3 key1= InputPin(duino, 10, True)
4 #led1= OutputPin(duino, 13)
The code above contains class definitions that model the connection to an Arduino and its input pins
Change '/dev/ttyACM0'
to the name of
your serial port, run the program and
observe its output
Hint: You can use the Arduino IDE to find the name of your serial port
Write a class OutputPin
that, analogous to
the class InputPin
, models an output pin
The class should provide a method
set_state
that turns the output
on or off
When you are done, uncomment the commented-out lines, to verify that your implementation works as expected
Hint: you can use the interactive commandline
to test your implementation python3 -i 16_atuino.py
1 class OutputPin(object):
2 def set_state(self, state):
3 self.arduino.exec_cmd(
4 'WRITE_HIGH' if state else 'WRITE_LOW',
5 self.pin_no
6 )