Recently I got notice about ComputerCraft, a Minecraft mod which is all about computers and programming in Minecraft. Each computer block could be connected to redstone in-/outputs, and is programmable in Lua. Today I installed the mod and played a little with it. The result is: I am now able to use a redstone switch to turn on/off a real life light. As a back end I use my recent work on RCSwitch, running on the Pi (using the Python REST-Server to accept command), and making a HTTP-post request from ComputerCraft/Lua. Pretty simple, pretty impressive.
Some time ago, I wrote about how to install the amazin Micro Python on the STM32F4-Discovery. While this works very well, one might easily come to the point where the internal flash memory of the STM32 will be not enough. I guess, this is why the Micro Python board comes with an micro SD card slot. On the STM32F4 there is no such slot, and Micro Python does not enable SD card support for this board by default. But this could easily be changed by using an Micro SD to SD card adapter which could be connected to the STM32.
Btw.: Mirco Python accesses the SD Card through SDIO in 4-Bit mode which makes the access pretty fast …
Patch the Micro Python Sources
In general, build Micro Python for the STM32F4-Discovery as already described in this article. But since the original sources have no SD enabled for the STM, before compiling the following changes need to be made to the Micro Python source code:
To enable SD card support, and specify SD card detection switch, edit stmhal/boards/STM32F4DISC/mpconfigboard.h like this:
#define STM32F4DISC #define MICROPY_HW_BOARD_NAME "F4DISC" #define MICROPY_HW_MCU_NAME "STM32F407" #define MICROPY_HW_HAS_SWITCH (1) #define MICROPY_HW_HAS_SDCARD (1) #define MICROPY_HW_HAS_MMA7660 (0) #define MICROPY_HW_HAS_LIS3DSH (1) #define MICROPY_HW_HAS_LCD (0) #define MICROPY_HW_ENABLE_RNG (1) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_TIMER (1) #define MICROPY_HW_ENABLE_SERVO (0) #define MICROPY_HW_ENABLE_DAC (0) #define MICROPY_HW_ENABLE_I2C1 (1) #define MICROPY_HW_ENABLE_SPI1 (1) #define MICROPY_HW_ENABLE_SPI3 (0) #define MICROPY_HW_ENABLE_CC3K (0) // USRSW is pulled low. Pressing the button makes the input go high. #define MICROPY_HW_USRSW_PIN (pin_A0) #define MICROPY_HW_USRSW_PULL (GPIO_NOPULL) #define MICROPY_HW_USRSW_EXTI_MODE (GPIO_MODE_IT_RISING) #define MICROPY_HW_USRSW_PRESSED (1) // LEDs #define MICROPY_HW_LED1 (pin_D14) // red #define MICROPY_HW_LED2 (pin_D12) // green #define MICROPY_HW_LED3 (pin_D13) // orange #define MICROPY_HW_LED4 (pin_D15) // blue #define MICROPY_HW_LED_OTYPE (GPIO_MODE_OUTPUT_PP) #define MICROPY_HW_LED_ON(pin) (pin->gpio->BSRRL = pin->pin_mask) #define MICROPY_HW_LED_OFF(pin) (pin->gpio->BSRRH = pin->pin_mask) // SD card detect switch #define MICROPY_HW_SDCARD_DETECT_PIN (pin_A8) #define MICROPY_HW_SDCARD_DETECT_PULL (GPIO_PULLUP) #define MICROPY_HW_SDCARD_DETECT_PRESENT (GPIO_PIN_RESET)
Wiring the SD-Card to the STM32F4-Discovery
The SD card and the STM need to be wired as follows:
SD-Card STM32F4-Discovery ------------------------------- 1 SDIO_D3 PC11 2 SDIO_CMD PD2 3 VSS GND 4 VDD 3V 5 SDIO_CLK PC12 6 SDIO_SW PA8 7 SDIO_D0 PC8 8 SDIO_D1 PC9 9 SDIO_D2 PC10
The pins on a SD card are numbered like this:
___________________ / 1 2 3 4 5 6 7 8 | | 9 | | |
Note: PC10/PC12 are also connected to the CS43L22,but this seams not to be a problem as long as you don’t use both: SD and CS43L22. Also it is suggested to use some pull-ups (47k) on SDIO_CMD, SDIO_D0, SDIO_D1, SDIO_D2, SDIO_D3, but I decided not to care :-).
Prepare the SD Card
Not much to do here, Just make sure to format the SD card to use with FAT-FS. On Linux, this is done like so (instead of sdc1 use the partition of your SD card!!):
Use the SD Card
Now, with the new SD card attached, reset the STM32 Micro Python board. After some time, the SD card should show up on your Linux as a empty new drive (e.g. as 4.0 GB Volume). Next, let’s connect to the Python shell on the board (e.g. by “screen /dev/ttyACM0 115200″) and see if we could access the card.
Note: the Micro Python web page states, that if a SD card is present, booting will be done from this card. However, it seams that on the STM32 booting is still done from internal flash, but the SD card is accessible anyway.
First, lets write a file to the SD card. In Micro Python, the internal Flash has the paht “0:/” and the SD card has “1:/”. Knowing this, it is pretty simple to write “Hello World” into a file on the card:
f = open("1:/hello.txt", "w") f.write("Hello World from Micro Python") f.close()
After a reset, the new file “hello.txt” should be shown on the SD card under Linux.
Ok, this is all fine, but how about storing and loading Python modules on the SD card? From Linux, let’s put the following sample (named “blink.py”) to the SD card:
import pyb leds = [pyb.LED(i) for i in range(1,5)] for l in leds: l.off() n = 0 try: while True: n = (n + 1) % 4 leds[n].toggle() pyb.delay(50) finally: for l in leds: l.off()
Reset Micro Python with “Ctrl-D”, and try the following:
Which will end in an erro:
Traceback (most recent call last): File "
", line 0, in ImportError: No module named 'blink'
This is because Python is only searching “0:\” and “0:\lib” for modules by default. We could easily fix this by doing the following:
import sys sys.path.append("1:/") import blink
It might be a good idea to put the above into “main.py” on the internal flash …
Install ARM cross-gcc
sudo apt-get install gcc-arm-linux-gnueabi make ncurses-dev
Get RasPi Kernel
E.g. use Kernel from GIT:
git clone https://github.com/raspberrypi/linux.git linux-raspberrypi
Compile the Kernel (needs to be done once before building the rcswitch-kmod)
To be able to compile out-of-tree Kernel modules, you need to cross-compile the targets Kernel at least once. This is done like so:
cd linux-raspberrypi make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- bcmrpi_defconfig make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
Depending on your machine, this may take some time …
Get the module from git:
git clone https://github.com/wendlers/rcswitch-kmod.git rcswitch-kmod cd rcswitch-kmod
Edit setenv_raspi.sh, make sure LINUX_DIR points to the directory of your previously build kernel:
Next, build the module:
source ./setenv_raspi.sh make all
Now copy the Kernel module to your PI. E.g. by using ssh:
scp module/src/rcswitch.ko pi@
Wiring the RF Transmitter to the PI (to use Kernel module defaults)
To use the modules build in default, connect the RF-Transmitter as shown in the picture below:
Loading the Module, Testing Module
On the Pi, the module could be loaded like this:
sudo su insmod /home/pi/rcswitch.ko
Verify the module was loaded properly:
Should output something like this:
[ 3392.757506] rcswitch: init [ 3392.757540] rcswitch: using tx_repeat of 10 [ 3392.757609] rcswitch: registered command interface under: /sys/kernel/rcswitch/command [ 3392.757623] rcswitch: registered power interface under: /sys/kernel/rcswitch/power [ 3392.757641] rcswitch: using gpio #17 for TX [ 3392.757650] rcswitch: EN disabled
And the following sysfs entries should have bean created:
ls -al /sys/kernel/rcswitch
--w--w--w- 1 root root 4096 Jul 25 23:15 command -rw-rw-rw- 1 root root 4096 Jul 25 23:46 power
If all of the above is in place, you should be able to operate your switches. E. g. turn on switch at address 11111, channel A:
echo 11111A1 > /sys/kernel/rcswitch/command
And to turn the same switch off:
echo 11111A0 > /sys/kernel/rcswitch/command
Note: there is also a python example which could be used on the Pi here.
Made a overview of how the over-air protocol looks for the Pollin RC Switches (see previous article for more details). The length of the pulses is a multible of 350us. The wave-forms where captured with an OpenBench logic sniffer.
Some time ago, I wrote about using an EDIMAX Smart Plug with Python. The EDIMAX has build in Wifi and runs its own web server. While this is convenient, it also makes the EDIMAX quiet expensive. Lately I got notice (thanks Eric), about very cheap 434MHz based plugs sold by Pollin for less then 10 EURO per 3 pieces (including a remote control I will not need anyway). Also there are libraries for using them with a cheap 434MHz transmitter as well as some remote control software for the RaspbarryPi.
What I didn’t like about the existing solutions was the fact, that the driver ran in user spacing using wiringPi to access the GPIOs. To my opinion there is absolutely no need to wrap all this low-level stuff in user-space. The Linux kernel itself provides a very good abstraction layer to access GPIOs in a machine independent way. Also exposing functionality could be done easily through sysfs.
Thus, I decided to somehow rewrite the above code (rcswitch-pi) to live in a Kernel module being accessible through sysfs. The complete results could be found here.
Operating the outlets now becomes very simple and could be done from almost any programming language. Even from bash.
E.g. to turn the power on for address ’11111′ and channel ‘A’ one could use:
echo "11111A1" > /sys/kernel/rcswitch/command
Or to turn the power off for the same device:
echo "11111A0" > /sys/kernel/rcswitch/command
I also included optional power-management, allowing the transmitter to be turned on/off (if VCC of the transmitter is connected to an GPIO):
Power on the transmitter:
echo "1" > /sys/kernel/rcswitch/power
Power off the transmitter:
echo "0" > /sys/kernel/rcswitch/power
Get current power state of the transmitter:
This far, I only tested the module on my 8Devices Carambola board (running kernel 3.3.8). But it should work without modifications for any other Linux board.
For more instructions, please see the projects README.
More links regarding RC outlets:
And some pictures of mine:
At Google I/O this year, Cardboard was presented. Cardboard are DIY 3D goggles made of cardboard, using a smart phone as screen and sensor device. Cardboard seams to be based on OpenDive from Stefan Welker who also published the STL files for 3D printing his goggles on ThingiVerse. And printing them was exactly what I did lately.
Obviously not all parts could be printed. One needs fitting lenses and some rubber band to “mount” the goggles on the head. Stefan Welker used to offer a kit on amazon, including lenses and rubber band. Unfortunately it looks like Google bought all the Kits for Cardboard. Thus, I replaced the lenses with the 30mm 5x ones included in this set. They do not fit in the original lens holders, but they do fit with this holders available at ThingiVerse.
The rubber band is a 40mm rubber band easily available. I used this one from amazon.
The results one gets with the OpenDrive are amazingly good! Especially playing Quake 2 is real fun! The only down side is, that there are not many (real) games available yet.
Some more links regarding OpenDive:
- OpenDive build instructions
- Dive SDK (including instructins on how to install Quake2 for the OpenDive)
- Order a ready made OpenDive
And some pictures of my OpenDive:
I created a first passive component for my brick-o-lage project: a big button with feedback LED. The button is mode from a big “pilz” switch which presses a smaller switch inside the case. Very simple but dangerous looking :-). The 3D files could be found on my github account. I tried to make the case stable enough so it could be hit hard too.
Since the bricks do have whole which are LEGO Technics compatible, they could be plugged together:
Big Button Brick Specifications
- One Pliz-Switch (40mm)
- One small switch
- Some wire, plug
Big Button Brick Case
For more information on how the communication with the Smart Plug works, see this post on ELV (available in German only).
The code could be used as library or as command line utility:
Using as library
p = SmartPlug("http://172.16.100.75:10000/smartplug.cgi", ('admin', '1234')) # turn plug off p.state = "OFF" # turn plug on p.state = "ON" # get plug state print(p.state)
Using as command line utility
turn plug on:
python smartplug.py -u http://172.16.100.75:10000 -l admin -p 1234 -s ON
turn plug off:
python smartplug.py -u http://172.16.100.75:10000 -l admin -p 1234 -s OFF
get plug state:
python smartplug.py -u http://172.16.100.75:10000 -l admin -p 1234 -g
I finished a case for the STM32F4-Discovery board. It consists of two parts: the case body and the case cover. The cover is hold in place by two snap-ins. The cover has spaces for all the header, jumpers and buttons. Also I made the cover very thin at the places where the LEDs of the board are located. This makes the LEDs nicely shine through the cover. All the CAD data could be found on thingiverse.
The short video below shows how well it works using thin material to make LEDs shine through:
Here are some more pictures:
The Micro Python project is an effort to make the Python3 language fast and lean enough to run on micro controllers and if was successfully founded by this KickStarter campaign. It is intended to run on it’s own hardware, the pyboard. Unfortunately, this board is not widely availabel, and everyone who missed the KickStarter will not be able to get such a board until now. Luckily the firmware also offers support for more ARM-Coretex M4 based micros, like the widely available STM32F4-Discovery. Since I like the idea running Python on a micro, and due to the fact my STM32F4-Discovery arrived today, I decided to immediately give Micro Python a try. In the following article I will describe in short what needs to be done to make Micro Python running on the discovery by using Ubuntu.
STM32F4-Discovery, that’s all you need since it includes already a stlink based debugger interface one could use for flashing.
Software needed on the host (Ubuntu)
To compile Micro Python, the arm-none-eabi cross-compiler is needed. On newer Ubuntu versions this could be easily installed through apt:
sudo apt-get install gcc-arm-none-eabi
To download the ELF firmware, you will also need the GDB for arm-none-eabi. In theory this could be done by the following apt statement:
sudo apt-get install gdb-arm-none-eabi
But unfortunately if you have installed any other gdb already (which is likely), this command will fail due a conflicting manpage! Thus, the following work-around is needed:
sudo apt-get install -d gdb-arm-none-eabi sudo dpkg -i --force-overwrite /var/cache/apt/archives/gdb-arm-none-eabi_18.104.22.16831218-0ubuntu1+1_amd64.deb
Building Micro Python for the Discovery
Clone the Micro Python git repository:
git clone https://github.com/micropython/micropython.git
Now change into the micropython directory an build for the STM32F4-Discovery:
This should create the firmware.elf in stmhal/build-STM32F4DISC.
Flash the Micro Python firmware to the Discovery
Connect the Discovery through USB (the one on with the Mini-B connector, don’t connect the USB-OTG), then start (in a separate terminal) the stlinkt utilit:
The st-util now waits for a connection from GDB. This is done like so (assuming you are still in the micropython/stmhal directory):
Within GDB connect to st-util by:
target extended localhost:4242
And flash the firmware with:
The multi-color COM-LED should blink while loading. After downloading finised, terminate GDB.
Now disconnect the USB Mini-B cable to power the board completely off. Connect a USB-OTG on the opposite side, and reconnect the USB Mini-B cable for power. After a few seconds, the STM should be mounted as a storage device showing the files “boot.py” and “main.py”. It is also possible to open a Python shell on the serial device /dev/ttyACM0:
You now could follow the tutorial on the Micro Python web page (accelerometer is not supported on the STM).
Note: in theory it should be also possible to download the firmware through DFU as described in the Micro Python README. But to my understanding you need to enable DFU mode on the STM by removing some resistors to change boot-configuration.
Micro Python looks very well done and promising. I think I should give it a try in some of my next projects.