IoT exploration with VS Code, Python, and the Device Simulator Express
The explosion of devices in the IoT space is more than a little overwhelming. Where do you start?
Why choose hardware when you can experiment entirely in software? That’s the power of the Device Simulator Express (DSE).
The DSE comes out of the Microsoft Garage internship program – two separate teams worked on the simulator over the past year. The development was done on GitHub and is open to contributors.
You can start experimenting with programming Python on IoT devices without buying anything – in fact, there’s nothing to buy at all. Everything you need is open-source and free. Just download:
- Visual Studio Code (https://code.visualstudio.com/)
- Python 3.7+ (https://www.python.org/downloads/)
- Device Simulator Express (https://marketplace.visualstudio.com/items?itemName=ms-python.devicesimulatorexpress)
When you install the Device Simulator Express, it will automatically install the Python Visual Studio Code Extension, if it’s not already present.
And now you’re ready to start experimenting!
Simulating devices in Visual Studio Code
There are three different simulators in the Device Simulator Express as of writing. The first is the Adafruit Circuit Playground Express, a circular device with two buttons, ten RGB NeoPixel LED lights, and a host of other sensors.
In the DSE, you see a graphical representation of the device, and all of the buttons and touchpads work with mouse clicks. You write code in Python to control the Circuit Playground Express and then experiment with the simulation. In Visual Studio Code, you’ll see your code in one pane alongside the graphical device, so you experience the rapid-fire “write a line of code, run it, make changes” cycle that makes programming fun and easy to understand.
And when you’re ready to experiment on hardware, the Device Simulator Express extension makes it easy to push your code via USB into the IoT hardware.
And the Circuit Playground Express is not the only device in the simulator!
The BBC micro:bit is a small rectangular device with a five-by-five grid of LEDs, two buttons, and several other sensors.
The matrix of LEDs makes it easy to create simple graphical images, characters, and even animations. One of the more popular demo apps for the micro:bit is a dice simulation – you shake the device triggering the accelerometer (which you can do with your mouse in the DSE) and the micro:bit LEDs blink until they stop on a one-to-six die pattern.
Next up is the Adafruit CLUE, which is much like the micro:bit, but adds an LCD display instead of the matrix of LEDs.
Now you’ve got some serious graphical power available with the 1.3 inch IPS LCD color display – the canonical example project on the CLUE is the game Snake. You can make compact, old-school, 8-bit style video games on the Adafruit CLUE.
With all the programming done in Python inside Visual Studio Code, you can experiment with moving your applications between the various hardware simulations and see how each device functions a bit differently, with different API calls needed.
The power of being able to experiment without buying hardware first provides a chance to explore what device is best for your particular application. More simulations added to the Device Simulator Express project will expand the number of choices for experimentation.
A powerful tool for learning Python
But perhaps the most exciting thing to do with the DSE is teaching new programmers. There’s no better form of the “Hello World” app than a blinking LED.
By being able to simulate the IoT hardware, you eliminate a significant barrier of entry to programming IoT devices – both the cost and the time it takes to get the specific hardware. With a pure software implementation, it doesn’t take long to get to learning programming.
And where there is a limited amount of hardware available, an entire class can continue to experiment with their software in the simulation and share the devices around to have the experience of running their code on a device – because a real blinking LED is even better!
A community-led project
The Device Simulator Express project is just over a year old – and there’s so much more that can be done.
There is an opportunity to build simulations of more devices – for example, the Adafruit FLORA, designed as an IoT device to be incorporated into clothing, would be a relatively simple addition to the project since it is quite similar to the Circuit Playground Express.
And there are more elaborate IoT devices available out there. Building the simulations would be more challenging, but would also open the door to more advanced experimentation of hardware in software, speeding development with the rapid code iteration cycles that are possible when not needing to push apps onto devices.
The front-end parts of the Device Simulator Express that provide the hardware visualizations are coded with React and TypeScript, working with the Visual Studio Code Extension API to facilitate integration.
The back-end is a Python-based simulation library, which does have some underlying C code (as domost Python libraries). Ultimately, the back-end services are simulations, not emulations, and so there are differences when you dig deep. But if you’re into that sort of thing, you’ll see that in some cases, the existing implementations of the DSE include some “unintended behaviors” of the actual hardware. Yes, the Device Simulation Express include simulation of the hardware bugs.
If you’d like to learn more about contributing to the Device Simulator Express project, check out the GitHub repository microsoft/vscode-python-devicesimulator.
Beyond coding on the project itself, there are many opportunities to build excellent training material using the DSE. Videos, walk-through, code samples – all tools to help more people discover the power of programming and the fun of exploring the world of IoT applications.