Snek Lang: feels like Python on Arduinos

Snake image credit: Snek project (https://sneklang.org/)

There’s a long time I’ve been interested on some interpreter similar to Python that could run on low end Arduino boards, such as Arduino Nano, Uno or Mega. I have tried “python-on-a-chip” (https://code.google.com/archive/p/python-on-a-chip/) / PyMite (https://github.com/dwhall/p14p) and TinyPy (https://github.com/philhassey/tinypy). Some of these projects are quite old and none of them had the characteristics I wanted. Moreover, seems that they are not maintained anymore. I also tested Arduino-PyMite (https://github.com/netpipe/Arduino-Pymite), a version of PyMite that can be easily built and loaded on Arduino using Arduino IDE, but it is also not what I wanted.

But today I found a recent project by accident: Snek: A Python-inspired Language for Embedded Devices (https://sneklang.org/). Created by Keith Packard, at the end of 2019, snek has a syntax similar to Python and implements a subset of Python language. I got impressed on how the tools are well organized and structured and about the robustness of the language running tests on an Arduino Mega and an Arduino Uno (https://keithp.com/blogs/snek-duino/).

But what I loved most, is the interactive REPL over serial port using standard terminals. Just flash snek on an Arduino, open your terminal and “talk” to the board as you would on a Python REPL prompt. This is so much clean and practical!

Another great work of Keith was to provide tools to easily install / flash snek on Arduinos and other boards. There are also Ubuntu packages for snek, and even a PC interpreter to play with snek on a desktop computer.

In my case, I downloaded and installed the Linux version with these commands:

$ wget https://sneklang.org/dist/snek-linux-1.5.sh
$ chmod +x snek-linux-1.5.sh
$ ./snek-linux-1.5.sh

I installed snek on ~/Snek. After that, we can use tools to easily flash snek on our Arduinos! For example, to flash snek on an Arduino Mega, simply run:

$ cd Snek
$ ./snek-mega-install

Done! Now we can already access snek prompt with any serial terminal. In my case, I tried with minicom:

minicom  -b 38400 -D /dev/ttyACM0

Note that some snek versions are configured to 115200 bps and other, as I have seen on the source code are configured for 38400. The package mentioned above are set to 38400 for Arduino Mega and Uno. Anyway, I could then easily use the REPL to control a LED and read the Arduino’s ADC:

Blinking the LED on D13 pin:

Code:

> talkto(D13)
> while True:
+  on()
+  time.sleep(1)
+  off()
+  time.sleep(1)

Reading the ADC:

Snek input values range from 0 to 1 on the ADC, so 1 means 5V for the standard ADC and 0 means 0V. If we multiply the reading by 5.0, we will have the ADC reading in volts. Quick and easy!

Code:

> while True:
+  print(read(A0)*5.0)
+  time.sleep(0.8)

Detailed documentation and functions:

https://sneklang.org/doc/snek.html

Snek’s author/creator presenting it:

https://www.youtube.com/watch?v=EWMFNqkgt1o&t=2323s

Sources:

https://github.com/keith-packard/snek

Snek has also an editor written in Python, called snekde, but I am happy with the simple serial console. One interesting possibility is to create a web page using avrbro (https://github.com/kaelhem/avrbro) and Web Serial, allowing new users to flash snek on Arduinos directly from a standard web page!

Finally, I decided to implement some snek functions as BIPES blocks! Yes! BIPES meets Arduino thanks to Snek! And it worked nicely! Now we can use BIPES to program Arduino with Snek using blocks!

https://bipes.net.br/beta2/ui/

Here is a complete BIPS program, with 3 functions, generating and running code for Snek!

And a simpler example running:

Result:

My thanks and congratulations to Keith Packard for this great project! I also thank Jorge Marques (https://github.com/JorgeGMarques | https://www.linkedin.com/in/gastmaier/ ) very much for the refactoring he has made on BIPES code, making it easier to add Arduino / Snek target boards with specific parameters, such as serial speed and buffer size configured at BIPES devinfo.json (https://github.com/rafaelaroca/BIPES_ui_testing).

Leave a comment