RFM12Pi V2

From Wiki | OpenEnergyMonitor
Revision as of 18:11, 11 June 2014 by Richardneish (talk | contribs) (Fix link formatting)
Jump to navigation Jump to search

RFM12Pi V2

The RFM12Pi allows the Raspberry Pi to receive wireless data from RFM12B wireless module.

RFM12PiV2 Pi small.png

Development of the RFM12Pi has been a team effort between Martin Harizanov and Glyn Hudson.

The RFM12Pi V2 consists of an RFM12B wireless module and an ATmega328 microprocessor running OptiBoot serial bootloader (modified for 8mhz internal oscillator) which is Arduino compatible.

The ATmega328 runs a slightly modified version of JeeLabs's RF12Demo. Raw wireless packets are passed to the Pi via serial using the Pi's internal UART serial port. The raw packets are processed by the emoncms Raspberry Pi module or python emoncms gateway See emoncms documentation on emoncms.org for detailed documentation regarding setting up and running emoncms on a Raspberry Pi with an RFM12Pi.

Technical Overview

  • ATmega328 based, has more memory and hardware serial support
  • Runs on the internal 8Mhz oscillator (to save unnecessary components)
  • SMT used, board layout is optimized for pick-and-place assembly
  • Lower profile to fit inside most Raspberry Pi enclosures
  • The new RFM2Pi with ATmega328 allows programming directly from Raspberry Pi using avrdude (..and OptiBoot)

RFM12Pi V2 Setup

The RFM12Pi V2 can be purchased pre-assembled and ready flashed with firmware from the [shop.openenergymonitor.com/raspberry-pi/ OpenEnergyMonitor shop]. No assembly or setup is needed to the RFM12Pi itself.

See Raspberry Pi documentation on emoncms.org for pre-built emoncms Raspberry Pi read-to-go SD card & Pi setup instructions.

If you have assembled the RFM12Pi yourself or wish to re-flash the firmware this can be done using the ISP header and a ISP programmer. See #Flashing the Bootloader

Upgrading RFM12Pi Firmware Direct from the Pi

  • These instructions were written for the original/older Raspberry Pi SD card image based on Raspbian. They probably won't work (we've yet to try) using the newer emoncms forwarding gateway images based on IPE. We recommend running Raspbian to upgrade the firmware. Please post if you get this working under IPE. *

If you're running the pre-built emoncms Raspberry Pi SD card image then uploading firmware to the Pi is as simple as grabbing the latest firmware build from github, stopping the rfm12piphp service, (to free up the serial port) then upload the .hex file via serial using avrdude:

$ cd RFM2Pi/firmware/RF12_Demo_atmega328
$ git pull
$ sudo service rfm12piphp stop
$ sudo avrdude -v  -c arduino -p ATMEGA328P -P /dev/ttyAMA0 -b 38400 -U flash:w:RF12_Demo_atmega328.cpp.hex
$ sudo service rfm12piphp start

If you're not running the pre-built emoncms SD card image, then you'll need to set up the following:

Since the Raspberry Pi lacks a DTR pin that makes it oh-so-easy to upload your hex files into the avr, we need this hack to make it just as easy. Thanks to JCW from JeLabs for working this out and Martin Harizanov for implementing it for the RFM12Pi.

$ sudo apt-get update 
$ sudo apt-get install arduino
$ sudo apt-get install python-dev&&python-rpi.gpio
$ git clone https://github.com/mharizanov/avrdude-rpi.git
$ cd avrdude-rpi
$ sudo cp autoreset /usr/bin
$ sudo cp avrdude-autoreset /usr/bin
$ sudo mv /usr/bin/avrdude /usr/bin/avrdude-original
$ sudo ln -s /usr/bin/avrdude-autoreset /usr/bin/avrdude
$ cd /home/pi
$ git clone https://github.com/mharizanov/RFM2Pi.git
$ cd RFM2Pi/firmware/RF12_Demo_atmega328
$ sudo service rfm12piphp stop
$ avrdude -v  -c arduino -p ATMEGA328P -P /dev/ttyAMA0 -b 38400 -U flash:w:RF12_Demo_atmega328.cpp.hex
$ sudo service rfm12piphp start

Flashing the Bootloader

The ATmega328 on the RFM12Pi should come pre-loaded with the Optiboot bootloader and RFM12Pi firmware, this step should not be required unless you have assembled the RFM12Pi yourself and used a blank ATmega328.

Note: the RFM12Pi is a 3.3V only therefore it should not be powered by a 5V ISP programmer.

To avoid accidental connection to 5V, the VCC pin on the ISP header is by default, disconnected. A jumper connection between the two adjacent pads must be made to connect the ISP VCC to the VCC on the board. On some ISP programmers, (such as the AVR MKII that I use) the VCC pin needs to be connected to initialise the programmer even though the programmer itself does not power the board. An easy way to power the board with 3.3V for programming, is to connect it onto the Pi's GPIO taking care to align pin 1.

The stock RFM12Pi firmware can be downloaded in pre-compiled .hex form from github here. This pre-compiled .hex file contains both the optiboot bootloader and the modified RF12Demo Sketch.

The .hex file can be flashed, and the fuses set, on the ATmega328 using the following avrdude command:

$ sudo avrdude -v -p atmega328p -c avrispmkII -P usb -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xDE:m -Ulfuse:w:0xE2:m -U flash:w:Optiboot328_8mhz_RF12_Demo.hex -B 10

If, for any reason (e.g to make a backup) you want to read the data back from the ATmega328 this can be done with:

 $ avrdude -v -p atmega328p -c avrispmkII -P usb -U flash:r:test_intel.hex:i

Alternatively, you could flash the modified Optiboot bootloader, then upload the RF12_Demo_atmega328.cpp.hex to RFM12Pi direct from the Pi, see below for instructions on how to modify the OptiBoot bootloader, and above, for instructions on how to upload direct from the Pi once the bootloader is in place.

Modified OptiBoot Bootloader

The ATmega328 on the RFM12Pi gets its clock from the the internal 8Mhz oscillator. This requires a special version of Optiboot optimised for 38.4 kbps. The modified and compiled version can be obtained at: downloaded from Martin's github.

The full guide on how to modify and re-compile Optboot is detailed on the Arduino forum here.

In order to compile Arduino sketches to work for this modified bootloder, we need to add a new entry in arduino-1.0.2\hardware\arduino\boards.txt.

atmega328_384_8.name=ATmega328 Optiboot @ 38,400baud w/ 8MHz Int. RC Osc.




Now we just need to select board > ATmega328 Optiboot @ 38,400baud w/ 8MHz Int. RC Osc in the Arduino IDE when compiling sketches.

Design Files & Reference

The board schematic, layout and firmware (in Eagle CAD format) are available on Martin Harizanov's github

Martin Harizanov's original blog post introducing the RFM12Pi V2

RFM12Pi V2.6 brd.png

RFM12Pi V1.6 sch.png


Minicom can be used to view the RFM12Pi serial output via the Pi's internal serial port. If your running a stock Raspbien image you will need to open access to ttyACM0 the internal UART serial port which the RFM12Pi uses. To do this see instruction here:


Next, stop any process that's using the serial port by either

$ sudo service rfm12piphp stop

if you're using the rfm21piphp service, or if you're using the OEM gateway on the rock-solid read-only gateway,

$ ps -ef | grep python

$ kill -9 xxxx

replacing xxxx with the PID of your python script process.

$ sudo apt-get install minicom

$ minicom -b9600 -D/dev/ttyAMA0

[CTRL + A] then [X] to exit minicom

If all is working well, you should see the following when the RFM12pi is connected to the Pi's GPIO. Note the packets being received from Node 10 (emonTx).

The default Frequency of the RFM12pi is 868Mhz, group 210 and node ID 15.

These parameters can be changed in software by issuing the correct serial commands as illustrated below

RFM12Pi serial.png

Hacks, Modifications & Extensions

Controlling RF Plugs

Thanks to Paul Tanner the RFM12Pi V2 has successfully been used to control (switch on/off) KAKU mains plugs via wireless OOK protocol.

The KAKU protocol is built into the RF12 demo, however the default firmware (currently as of July 2013) on the RFM12Pi uses a cut down version of the RF12 demo. In order to use the full functionality of the RF12 demo including the OOK KAKU protocol the firmware on the ATmega328 needs to be upgraded. We have released a version of the full RF12 demo which as been slightly customised for the RF12Pi (baud rate, turn off bad packet reporting and "OK" printing). This firmware is available on github here.

The firmware can be updated direct from the pi by following the instructions above but specifying the Full_RF12demo_atmega328.cpp.hex in the avrdude commandline.

See the original forum thread with full upgrading instructions from Paul if you're not running the pre-built emoncmspi SD card.

Using RFM12Pi with a BeagleBoard Black

Thanks for forum member Docin, it's possible to use the RFM12Pi on BeagleBoard Black


Upgrading RFM12Pi firmware from BeagleBoard Black:

http://mymiscellaneoustricks.wordpress.com/2014/02/16/mise-a-jour-du-firmware-du-rfm12pi/ http://mymiscellaneoustricks.wordpress.com/2014/02/16/mise-a-jour-du-firmware-du-rfm12pi/]