I'm trying to learn PyFirmata, but don't want to get all of the hardware I need to do so. Instead I want to use a simulator, right now I'm using SimulIDE. In order to use PyFirmata, a board has to be connected to a COM port. Is there a way to get around this and use SimulIDE, or another simulator, instead?
Here is the code I want to run:
import pyfirmata
import time
board = pyfirmata.Arduino('/dev/ttyACM0')
while True:
board.digital[13].write(1)
time.sleep(1)
board.digital[13].write(0)
time.sleep(1)
According to this page, it's possible to connect your simulated Arduino to a real or virtual serial port (look for the "Connecting to Serial Port" section). You haven't specified what OS you're using; I'll show an example of doing that with Linux.
Create a virtual serial port on your host
We can use
socat
to create a virtual serial port like this:This links a pair of PTY devices (which will have unpredictable names like
/dev/pts/6
and/dev/pts/7
) and creates convenience symlinks in/tmp/vport
. The-v
option means socat will print on the console any data being sent back and forth.As an alternative to
socat
, you could also usetty0tty
to create a linked pair of PTY devices. If you're on Windows, I believe thatcom0com
does something similar, but I don't have an environment in which to test that out.Create a serial port in SimulIDE
In the component panel, open the "Perifericals" section...
...and place a "SerialPort" in your circuit. Connect the TX pin on the serial port to the RX pin on your Arduino, and the RX pin on the serial port to the TX pin on the Arduino.
Double click on the serial port and configure it to connect to
/tmp/vport/board
:Then select the "Config" panel and make sure the port is set for
57600
bps:Close the configuration window and select the "Open" button on the serial port. It should end up looking something like:
Load some firmware on your Arduino and run it
For testing the serial port, I used the following code (in a sketch named
SerialRead
:And then compiled it using the
arduino-cli
tool like this:This creates (among other files) the file
build/SerialRead.ino.hex
, which is what we'll load into the simulator.In SimulIDE, right-click on the Arduino and select "Load firmware", then browse to that
SerialRead.ino.hex
file and load it:Connect a serial tool to the virtual port
I'm using
picocom
to verify that things are connected properly (but you can use any other serial communication software, likeminicom
,cu
, etc). On the host, run:In SimulIDE, power on the Arduino. In
picocom
, start typing something and you should see (if you type "ABCD"):This confirms that we have functional serial connectivity between the virtual serial port on our host and the software running on the simulated Arduino. Now let's try Firmata!
So what about Firmata?
Now that we've confirmed that we have the appropriate connectivity we can start working with
pyfirmata
. First, you'll need to compile theStandardFirmata
sketch into a.hex
file, as we did with theSerialRead
sketch earlier, and then load the.hex
file into your simulated Arduino.I wired up an LED between pin
7
andGND
, and a switch between pin8
and3v3
, and used the following code to test things out:Running this Python code produces the following behavior in SimulIDE: