Difference between revisions of "RFM69Pi V3"

From OpenEnergyMonitor Wiki
Jump to: navigation, search
(Created page with "==RFM69Pi V2== The RFM69Pi allows the Raspberry Pi to receive wireless data from RFM12B wireless module. File:RFM69PiV2_Pi_small.png‎ ''Development of the RFM69Pi ha...")
 
(Note for disabling BT)
 
(25 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==RFM69Pi V2==
+
==RFM69Pi V3==
 +
  
The RFM69Pi allows the Raspberry Pi to receive wireless data from RFM12B wireless module.  
+
[[File:RFM69Pi RasPi.JPG|750px]]
  
[[File:RFM69PiV2_Pi_small.png‎]]
+
''Development of the RFM69Pi has been a team effort between [http://harizanov.com/ Martin Harizanov], Glyn Hudson and Ken Boak.''
  
''Development of the RFM69Pi has been a team effort between [http://harizanov.com/ Martin Harizanov] and Glyn Hudson.''
+
The RFM69Pi enables the Raspberry Pi to receive data from other RFM69CW or RFM12B (backward compatible) wireless modules
  
 +
The RFM69Pi V3 consists of an [http://openenergymonitor.org/emon/buildingblocks/rfm69cw RFM69CW wireless module] and an ATmega328 microprocessor running the OptiBoot serial bootloader (modified for 8MHz internal oscillator) which is Arduino compatible. See [http://openenergymonitor.blogspot.com/2014/12/introducing-rfm69cw.html our blog post] introducing the new RFM69CW module.
  
The RFM69Pi V2 consists of an [http://openenergymonitor.org/emon/buildingblocks/rfm12b-wireless RFM12B wireless module] and an ATmega328 microprocessor running OptiBoot serial bootloader (modified for 8mhz internal oscillator) which is Arduino compatible.
+
The ATmega328 runs a [https://github.com/openenergymonitor/RFM2Pi/blob/master/firmware/RFM69CW_RF_Demo_ATmega328/RFM69CW_RF12_Demo_ATmega328/RFM69CW_RF12_Demo_ATmega328.ino modified] version of [https://github.com/jcw/jeelib/tree/master/examples/RF12/RF12demo JeeLabs's RF12Demo].  
 
+
Raw wireless packets are passed to the Pi via the Pi's internal serial port.  
The ATmega328 runs a [https://github.com/mharizanov/RFM2Pi/blob/master/firmware/RF12_Demo_atmega328/RF12_Demo_atmega328.ino slightly modified] version of [https://github.com/jcw/jeelib/tree/master/examples/RF12/RF12demo 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 [https://github.com/emoncms/raspberrypi emoncms Raspberry Pi module] or [https://github.com/emoncms/docs/blob/master/raspberrypigateway.md python emoncms gateway]
+
See emoncms documentation on [http://emoncms.org/site/docs/raspberrypi emoncms.org] for detailed documentation regarding setting up and running emoncms on a Raspberry Pi with an RFM69Pi. 
+
  
 
===Technical Overview===
 
===Technical Overview===
Line 18: Line 17:
 
*ATmega328 based, has more memory and hardware serial support
 
*ATmega328 based, has more memory and hardware serial support
 
*Runs on the internal 8Mhz oscillator (to save unnecessary components)
 
*Runs on the internal 8Mhz oscillator (to save unnecessary components)
*SMT used, board layout is optimized for pick-and-place assembly
+
*SMT components.
 
*Lower profile to fit inside most Raspberry Pi enclosures
 
*Lower profile to fit inside most Raspberry Pi enclosures
 
*The new RFM2Pi with ATmega328 allows programming directly from Raspberry Pi using avrdude (..and OptiBoot)
 
*The new RFM2Pi with ATmega328 allows programming directly from Raspberry Pi using avrdude (..and OptiBoot)
 +
*''Communicates with Raspberry Pi via /dev/ttyAMA0 at 38400 bps''
  
 +
===RFM69Pi V3 Setup===
  
===RFM69Pi V2 Setup===
+
The RFM69Pi V3 can be purchased pre-assembled and pre-loaded with firmware from the [http://shop.openenergymonitor.com/raspberry-pi/ OpenEnergyMonitor shop]. No assembly or setup required.
  
The RFM69Pi 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 RFM69Pi itself.
+
See main [http://openenergymonitor.org/emon/guide OpenEnergyMonitor Build guide]
  
See [http://emoncms.org/site/docs/raspberrypiimage Raspberry Pi documentation on emoncms.org] for pre-built emoncms Raspberry Pi read-to-go SD card & Pi setup instructions.  
+
Communication with the RFM69Pi on the Raspberry Pi is handled by [https://github.com/emonhub/emonhub/ the emonHub Python service]. Using emonHub, received RF data can be posted to one or more Emoncms servers or another internet service.  
  
If you have assembled the RFM69Pi yourself or wish to re-flash the firmware this can be done using the ISP header and a ISP programmer. See [[#Flashing the Bootloader]]
+
If you have assembled the RFM69Pi, or wish to update/reflash the Arduino based firmware, that can be done using the ISP header and a ISP programmer. See [[#Flashing the Bootloader]]
  
 +
===Communicating Directly with the RFM69Pi===
 +
 +
The RFM69Pi communicates with Raspberry Pi via /dev/ttyAMA0 at 38400 bps. The Raspberry Pi's serial console must be disabled to enable the RFM69Pi serial communication:
 +
 +
<code>
 +
$ sudo wget https://raw.github.com/lurch/rpi-serial-console/master/rpi-serial-console -O /usr/bin/rpi-serial-console && sudo chmod +x /usr/bin/rpi-serial-console
 +
 +
$ sudo rpi-serial-console disable
 +
 +
$ sudo reboot
 +
 +
$ rpi-serial-console status
 +
 +
 +
</code>
 +
You should see the message ''Serial console on /dev/ttyAMA0 is disabled'', the serial port is now free for communication with the ATMega328 on the RFM69Pi
 +
 +
Minicom can be used to view the serial output of the RFM69Pi direcly:
 +
 +
<code>
 +
$ sudo apt-get install minicom
 +
 +
$ minicom -b38400 -D/dev/ttyAMA0</code>
 +
 +
[CTRL + A] then [X] to exit minicom
 +
 +
===Raspberry Pi 3 Compatibility===
 +
 +
The emonPi communicates with the RasPi via GPIO 14/15 which on the Model B,B+ and Pi2 is mapped to UART0. However on the Pi3 these pins are mapped to UART1 since UART0 is now used for the bluetooth module. However UART1 is software UART and baud rate is dependant to clock speed which can change with the CPU load, undervoltage and temperature; therefore not stable enough. One hack is to force the CPU to a lower speed ( add <code>core_freq=250</code> to <code>/boot/cmdline.txt</code>) which cripples the Pi3 performance. A better solution for the emonPi is to swap BT and map UART1 back to UART0 (ttyAMA0) so we can talk to the emonPi in the same way as before.
 +
 +
RasPi 3 by default has bluetooth (BT) mapped to <code>/dev/AMA0</code>. To allow use to use high performace hardware serial (dev/ttyAMA0) and move bluetooth to software serial (/dev/ttyS0) we need to add an overlay to <code>config.txt</code>. See <code>/boot/overlays/README</code> for more details. See [https://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3/ this post] for RasPi3 serial explained.
 +
 +
<code>sudo nano /boot/config.txt</code>
 +
 +
Add to the end of the file
 +
 +
<code>dtoverlay=pi3-miniuart-bt</code>
 +
 +
We also need to run to stop BT modem trying to use UART
 +
 +
<code>sudo systemctl disable hciuart</code>
 +
 +
See RasPi device tree commit for pi3-disable-bt and forum thread discussion
 +
 +
Reboot then test serial comms with:
 +
 +
<code>sudo minicom -D /dev/ttyAMA0 -b38400</code>
 +
 +
You should see data from emonPi ATmega328, sending serial v should result in emonPi returning it's firmware version and config settings.
 +
 +
To fix SSHD bug (when using the on board WiFi adapter and NO Ethernet). Edit <code>/etc/ssh/sshd_config</code> and append:
 +
 +
<code>IPQoS cs0 cs0</code>
  
 
====Upgrading RFM69Pi Firmware Direct from the Pi ====
 
====Upgrading RFM69Pi Firmware Direct from the Pi ====
  
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 RFM69Pi.  
+
Since the Raspberry Pi lacks a DTR pin that makes it oh-so-easy to upload your hex files to 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 on the RFM12Pi.
 +
 
 +
'''The RFM69Pi uses GPIO Pin 4 (BCM) as the DTR reset pin '''
  
 
<code><pre>
 
<code><pre>
$ sudo apt-get update  
+
 
$ sudo apt-get install arduino
+
#Install avrdude, and GPIO auto reset
$ sudo apt-get install python-dev&&python-rpi.gpio
+
$ sudo apt-get update
$ git clone https://github.com/mharizanov/avrdude-rpi.git
+
$ $ git clone https://github.com/openenergymonitor/avrdude-rpi.git ~/avrdude-rpi && ~/avrdude-rpi/install
$ cd avrdude-rpi
+
 
$ sudo cp autoreset /usr/bin
+
 
$ sudo cp avrdude-autoreset /usr/bin
+
# Grab latest firmware for RFM69Pi and perform the upload via serial
$ sudo mv /usr/bin/avrdude /usr/bin/avrdude-original
+
$ git clone https://github.com/openenergymonitor/RFM2Pi.git
$ sudo ln -s /usr/bin/avrdude-autoreset /usr/bin/avrdude
+
$ cd RFM2Pi/firmware/RFM69CW_RF_Demo_ATmega328/
$ cd /home/pi
+
$ git clone https://github.com/openenergymonitor/RFM2Pi
+
$ cd RFM2Pi/firmware/RF12_Demo_atmega328
+
 
$ sudo service emonhub stop
 
$ sudo service emonhub stop
$ avrdude -v -c arduino -p ATMEGA328P -P /dev/ttyAMA0 -b 38400 -U flash:w:RF12_Demo_atmega328.cpp.hex
+
$ avrdude -v -c arduino -p ATMEGA328P -P /dev/ttyAMA0 -b 38400 -U flash:w:RFM69CW_RF12_Demo_ATmega328.cpp.hex
 
$ sudo service emonhub start
 
$ sudo service emonhub start
 
</pre></code>
 
</pre></code>
  
 
+
'
''Update: Paul has made a modified version of avrdude autoreset which does not conflict with uploads via serial UART and only kicks in when uploading to ttyAMA0 with a confirmation message to informs user which gpio pin is being used for DTR (reset): https://github.com/pb66/avrdude-rpi. Forum thread http://openenergymonitor.org/emon/node/6121''
+
  
 
====Flashing the Bootloader====
 
====Flashing the Bootloader====
  
'''The ATmega328 on the RFM69Pi should come pre-loaded with the Optiboot bootloader and RFM69Pi firmware, this step should not be required unless you have assembled the RFM69Pi yourself and used a blank ATmega328.'''  
+
'''The ATmega328 on the RFM69Pi should come pre-loaded with the Optiboot bootloader and RFM69Pi firmware. This step should not be required unless you have assembled the RFM69Pi with a blank ATmega328.'''  
  
  
''Note: the RFM69Pi is a 3.3V only therefore it should not be powered by a 5V ISP programmer.''
+
''Note: the RFM69Pi is a 3.3V device. 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 RFM69Pi firmware can be downloaded in pre-compiled .hex form from github [https://github.com/openenergymonitor/RFM2Pi/tree/master/firmware/RFM69CW_RF_Demo_ATmega328 here]. This pre-compiled .hex file contains both the optiboot bootloader and the modified RF12Demo Sketch.  
 
+
 
+
The stock RFM69Pi firmware can be downloaded in pre-compiled .hex form from github [https://github.com/mharizanov/RFM2Pi/blob/master/firmware/RF12_Demo_atmega328/Optiboot328_8mhz_RF12_Demo.hex 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:   
 
The .hex file can be flashed, and the fuses set, on the ATmega328 using the following avrdude command:   
  
<code><pre>$ 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:i -B 10</pre></code>
+
<code><pre>$ 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_RFM69CW_RF12_Demo_ATmega328.cpp.hex:i -B 10</pre></code>
  
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:
+
If you want to read the data from the ATmega328 (e.g to make a backup) that can be done with:
  
 
<code><pre> $ avrdude -v -p atmega328p -c avrispmkII -P usb -U flash:r:test_intel.hex:i</pre></code>
 
<code><pre> $ avrdude -v -p atmega328p -c avrispmkII -P usb -U flash:r:test_intel.hex:i</pre></code>
  
Alternatively, you could flash the modified Optiboot bootloader, then upload the RF12_Demo_atmega328.cpp.hex to RFM69Pi 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.  
+
Alternatively, you could flash the modified Optiboot bootloader, then upload the RF12_Demo_atmega328.cpp.hex to RFM69Pi 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=====
 
===== Modified OptiBoot Bootloader=====
The ATmega328 on the RFM69Pi 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: [https://github.com/mharizanov/RFM2Pi/tree/master/firmware/RF12_Demo_atmega328 downloaded from Martin's github].
+
The ATmega328 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: [https://github.com/mharizanov/RFM2Pi/tree/master/firmware/RF12_Demo_atmega328 from Martin's github].
  
 
The full guide on how to modify and re-compile Optboot is detailed on the Arduino forum [http://arduino.cc/forum/index.php?topic=124879.0 here].
 
The full guide on how to modify and re-compile Optboot is detailed on the Arduino forum [http://arduino.cc/forum/index.php?topic=124879.0 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.
+
In order to compile Arduino sketches to work for this modified bootloader, we need to add a new entry in arduino-1.0.2\hardware\arduino\boards.txt.
  
 
<code><pre>
 
<code><pre>
Line 109: Line 159:
 
</pre></code>  
 
</pre></code>  
  
Now we just need to select board > ATmega328 Optiboot @ 38,400baud w/ 8MHz Int. RC Osc in the Arduino IDE when compiling sketches.
+
Now we need to select: Board > ATmega328 Optiboot at 38,400 bps with 8MHz Internal RC Osc in the Arduino IDE when compiling sketches.
  
 
===Design Files & Reference===
 
===Design Files & Reference===
 
The board schematic, layout (in Eagle CAD format) are available on the[https://github.com/openenergymonitor/Hardware/tree/master/RFM2Pi OpenEnergyMonitor Hardware GitHub]
 
The board schematic, layout (in Eagle CAD format) are available on the[https://github.com/openenergymonitor/Hardware/tree/master/RFM2Pi OpenEnergyMonitor Hardware GitHub]
  
The Arduino compatiable firmware is available on the [https://github.com/openenergymonitor/RFM2Pi/tree/master/firmware/RF12_Demo_atmega328 OpenEnergyMonitor Github: ]
+
The Arduino compatiable firmware is available on the [https://github.com/openenergymonitor/RFM2Pi/ OpenEnergyMonitor Github: ]
  
[http://harizanov.com/2013/02/new-rfm2pi-board-in-the-works/ Martin Harizanov's original blog post introducing the RFM69Pi V2]
 
  
  
[[File:RFM69Pi_V1.6_pinout_brd.png‎‎]]
 
  
[[File:RFM69Pi V1.6 sch.png‎‎]]
+
[[File:RFm69PiV3.1 brd.jpg‎‎|750px]]
 +
 
 +
[[File:RFm69PiV3.1 sch.png‎‎|750px]]
 +
 
  
 
===Debugging===
 
===Debugging===
  
Minicom can be used to view the RFM69Pi 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 RFM69Pi uses. To do this see instruction here:  
+
Minicom can be used to view the RFM69Pi serial output via the Pi's internal serial port. If you're running a stock Raspbian image, you will need to enable access to ttyACM0 the internal UART serial port which the RFM69Pi uses. To do this, see instructions here:
  
 
[https://github.com/lurch/rpi-serial-console https://github.com/lurch/rpi-serial-console]
 
[https://github.com/lurch/rpi-serial-console https://github.com/lurch/rpi-serial-console]
 
 
Next, stop any process that's using the serial port by either
 
 
<code> $ sudo service rfm12piphp stop </code>
 
 
if you're using the rfm21piphp service, or if you're using the OEM gateway on the rock-solid read-only gateway,
 
 
<code>
 
$ ps -ef | grep python
 
 
$ kill -9 xxxx</code>
 
 
replacing xxxx with the PID of your python script process.
 
  
 
<code>
 
<code>
Line 150: Line 187:
 
[CTRL + A] then [X] to exit minicom
 
[CTRL + A] then [X] to exit minicom
  
If all is working well, you should see the following when the RFM69Pi is connected to the Pi's GPIO. Note the packets being received from Node 10 (emonTx).
+
If all is working well, you should see the following when the RFM69Pi is connected to the Pi's GPIO:
  
'''The default Frequency of the RFM69Pi is 433Mhz, group 210 and node ID 15'''.  
+
<code>
 +
[RF12demo.12] O i15 g210 @ 433 MHz q1
 +
OK 10 129 0 0 0 0 0 0 0 55 93 200 0 0 0 0 0 0 0 0 0 0 0 0 0 (-65)
 +
</code>
  
These parameters can be changed in software by issuing the correct serial commands as illustrated below
+
Note the packets being received from Node 10 (emonTx). Send the command 'v' to return firmware version and RF settings.
  
 +
'''The default Frequency of the RFM69Pi is 433Mhz, group 210 and node ID 15'''.
  
[[File:RFM12Pi_serial.png|500px]]
+
Send the command 'p' to return a list of available commands:
  
===Hacks, Modifications & Extensions===
+
<code>
 
+
Available commands:                                                           
====Controlling RF Plugs====
+
  <nn> i    - set node ID (standard node ids are 1..30)                       
Thanks to Paul Tanner the RFM69Pi V2 has successfully been used to control (switch on/off) KAKU mains plugs via wireless OOK protocol.
+
  <n> b      - set MHz band (4 = 433, 8 = 868, 9 = 915)                       
 
+
  <nnnn> o  - change frequency offset within the band (default 1600)          
The KAKU protocol is built into the RF12 demo, however the default firmware (currently as of July 2013) on the RFM69Pi uses a [https://github.com/mharizanov/RFM2Pi/blob/master/firmware/RF12_Demo_atmega328/RF12_Demo_atmega328.ino 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 [https://github.com/mharizanov/RFM2Pi/tree/master/firmware/Full_RF12demo_atmega328 here].
+
              96..3903 is the range supported by the RFM12B                   
 
+
  <nnn> g    - set network group (RFM12 only allows 212, 0 = any)              
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.
+
  <n> c      - set collect mode (advanced, normally 0)                         
 
+
  t          - broadcast max-size test packet, request ack                     
See the [http://openenergymonitor.org/emon/node/2245 original forum thread] with full upgrading instructions from Paul if you're not running the pre-built emoncmspi SD card.
+
  ...,<nn> a - send data packet to node <nn>, request ack                     
 +
  ...,<nn> s - send data packet to node <nn>, no ack                           
 +
  <n> q      - set quiet mode (1 = don't report bad packets)                   
 +
  <n> x      - set reporting format (0: decimal, 1: hex, 2: hex+ascii)         
 +
  123 z      - total power down, needs a reset to start up again               
 +
Remote control commands:                                                      
 +
  <hchi>,<hclo>,<addr>,<cmd> f    - FS20 command (868 MHz)                   
 +
  <addr>,<dev>,<on> k              - KAKU command (433 MHz)                   
 +
Current configuration:                                                         
 +
E i5 g210 @ 433 MHz 
 +
</code>
  
====Using RFM69Pi with a BeagleBoard Black====
 
  
Thanks for forum member [http://openenergymonitor.org/emon/user/5403 Docin], it's possible to use the RFM69Pi on BeagleBoard Black
+
These parameters can be changed in software by issuing the correct serial commands e.g b4 for 433Mhz, 210g for 210 network group.
  
[http://mymiscellaneoustricks.wordpress.com/2014/02/15/rfm12pi/ http://mymiscellaneoustricks.wordpress.com/2014/02/15/rfm12pi/]
 
  
Upgrading RFM69Pi firmware from BeagleBoard Black:
+
'''Example commands:'''
  
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/]
+
* '8b' - set frequency to 868 (default 433)
 +
* '210g' - set group to 210 (default)
 +
* '1q' - turn off quite mode (don't return bad packets '?')

Latest revision as of 12:08, 11 June 2019

RFM69Pi V3

RFM69Pi RasPi.JPG

Development of the RFM69Pi has been a team effort between Martin Harizanov, Glyn Hudson and Ken Boak.

The RFM69Pi enables the Raspberry Pi to receive data from other RFM69CW or RFM12B (backward compatible) wireless modules

The RFM69Pi V3 consists of an RFM69CW wireless module and an ATmega328 microprocessor running the OptiBoot serial bootloader (modified for 8MHz internal oscillator) which is Arduino compatible. See our blog post introducing the new RFM69CW module.

The ATmega328 runs a modified version of JeeLabs's RF12Demo. Raw wireless packets are passed to the Pi via the Pi's internal serial port.

Technical Overview

  • ATmega328 based, has more memory and hardware serial support
  • Runs on the internal 8Mhz oscillator (to save unnecessary components)
  • SMT components.
  • Lower profile to fit inside most Raspberry Pi enclosures
  • The new RFM2Pi with ATmega328 allows programming directly from Raspberry Pi using avrdude (..and OptiBoot)
  • Communicates with Raspberry Pi via /dev/ttyAMA0 at 38400 bps

RFM69Pi V3 Setup

The RFM69Pi V3 can be purchased pre-assembled and pre-loaded with firmware from the OpenEnergyMonitor shop. No assembly or setup required.

See main OpenEnergyMonitor Build guide

Communication with the RFM69Pi on the Raspberry Pi is handled by the emonHub Python service. Using emonHub, received RF data can be posted to one or more Emoncms servers or another internet service.

If you have assembled the RFM69Pi, or wish to update/reflash the Arduino based firmware, that can be done using the ISP header and a ISP programmer. See #Flashing the Bootloader

Communicating Directly with the RFM69Pi

The RFM69Pi communicates with Raspberry Pi via /dev/ttyAMA0 at 38400 bps. The Raspberry Pi's serial console must be disabled to enable the RFM69Pi serial communication:

$ sudo wget https://raw.github.com/lurch/rpi-serial-console/master/rpi-serial-console -O /usr/bin/rpi-serial-console && sudo chmod +x /usr/bin/rpi-serial-console

$ sudo rpi-serial-console disable

$ sudo reboot

$ rpi-serial-console status


You should see the message Serial console on /dev/ttyAMA0 is disabled, the serial port is now free for communication with the ATMega328 on the RFM69Pi

Minicom can be used to view the serial output of the RFM69Pi direcly:

$ sudo apt-get install minicom

$ minicom -b38400 -D/dev/ttyAMA0

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

Raspberry Pi 3 Compatibility

The emonPi communicates with the RasPi via GPIO 14/15 which on the Model B,B+ and Pi2 is mapped to UART0. However on the Pi3 these pins are mapped to UART1 since UART0 is now used for the bluetooth module. However UART1 is software UART and baud rate is dependant to clock speed which can change with the CPU load, undervoltage and temperature; therefore not stable enough. One hack is to force the CPU to a lower speed ( add core_freq=250 to /boot/cmdline.txt) which cripples the Pi3 performance. A better solution for the emonPi is to swap BT and map UART1 back to UART0 (ttyAMA0) so we can talk to the emonPi in the same way as before.

RasPi 3 by default has bluetooth (BT) mapped to /dev/AMA0. To allow use to use high performace hardware serial (dev/ttyAMA0) and move bluetooth to software serial (/dev/ttyS0) we need to add an overlay to config.txt. See /boot/overlays/README for more details. See this post for RasPi3 serial explained.

sudo nano /boot/config.txt

Add to the end of the file

dtoverlay=pi3-miniuart-bt

We also need to run to stop BT modem trying to use UART

sudo systemctl disable hciuart

See RasPi device tree commit for pi3-disable-bt and forum thread discussion

Reboot then test serial comms with:

sudo minicom -D /dev/ttyAMA0 -b38400

You should see data from emonPi ATmega328, sending serial v should result in emonPi returning it's firmware version and config settings.

To fix SSHD bug (when using the on board WiFi adapter and NO Ethernet). Edit /etc/ssh/sshd_config and append:

IPQoS cs0 cs0

Upgrading RFM69Pi Firmware Direct from the Pi

Since the Raspberry Pi lacks a DTR pin that makes it oh-so-easy to upload your hex files to 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 on the RFM12Pi.

The RFM69Pi uses GPIO Pin 4 (BCM) as the DTR reset pin

#Install avrdude, and GPIO auto reset
$ sudo apt-get update
$ $ git clone https://github.com/openenergymonitor/avrdude-rpi.git ~/avrdude-rpi && ~/avrdude-rpi/install


# Grab latest firmware for RFM69Pi and perform the upload via serial 
$ git clone https://github.com/openenergymonitor/RFM2Pi.git
$ cd RFM2Pi/firmware/RFM69CW_RF_Demo_ATmega328/
$ sudo service emonhub stop
$ avrdude -v -c arduino -p ATMEGA328P -P /dev/ttyAMA0 -b 38400 -U flash:w:RFM69CW_RF12_Demo_ATmega328.cpp.hex
$ sudo service emonhub start

'

Flashing the Bootloader

The ATmega328 on the RFM69Pi should come pre-loaded with the Optiboot bootloader and RFM69Pi firmware. This step should not be required unless you have assembled the RFM69Pi with a blank ATmega328.


Note: the RFM69Pi is a 3.3V device. It should not be powered by a 5V ISP programmer.


The stock RFM69Pi 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_RFM69CW_RF12_Demo_ATmega328.cpp.hex:i -B 10

If you want to read the data from the ATmega328 (e.g to make a backup) that 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 RFM69Pi 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 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: 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 bootloader, 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.

atmega328_384_8.upload.protocol=arduino
atmega328_384_8.upload.maximum_size=30720
atmega328_384_8.upload.speed=38400

atmega328_384_8.bootloader.low_fuses=0xE2
atmega328_384_8.bootloader.high_fuses=0xDE
atmega328_384_8.bootloader.extended_fuses=0x05
atmega328_384_8.bootloader.path=optiboot
atmega328_384_8.bootloader.file=optiboot_atmega328_384_8.hex
atmega328_384_8.bootloader.unlock_bits=0x3F
atmega328_384_8.bootloader.lock_bits=0x0F

atmega328_384_8.build.mcu=atmega328p
atmega328_384_8.build.f_cpu=8000000L
atmega328_384_8.build.core=arduino
atmega328_384_8.build.variant=standard

Now we need to select: Board > ATmega328 Optiboot at 38,400 bps with 8MHz Internal RC Osc in the Arduino IDE when compiling sketches.

Design Files & Reference

The board schematic, layout (in Eagle CAD format) are available on theOpenEnergyMonitor Hardware GitHub

The Arduino compatiable firmware is available on the OpenEnergyMonitor Github:



RFm69PiV3.1 brd.jpg

RFm69PiV3.1 sch.png


Debugging

Minicom can be used to view the RFM69Pi serial output via the Pi's internal serial port. If you're running a stock Raspbian image, you will need to enable access to ttyACM0 the internal UART serial port which the RFM69Pi uses. To do this, see instructions here:

https://github.com/lurch/rpi-serial-console

$ sudo apt-get install minicom

$ minicom -b38400 -D/dev/ttyAMA0

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

If all is working well, you should see the following when the RFM69Pi is connected to the Pi's GPIO:

[RF12demo.12] O i15 g210 @ 433 MHz q1 OK 10 129 0 0 0 0 0 0 0 55 93 200 0 0 0 0 0 0 0 0 0 0 0 0 0 (-65)

Note the packets being received from Node 10 (emonTx). Send the command 'v' to return firmware version and RF settings.

The default Frequency of the RFM69Pi is 433Mhz, group 210 and node ID 15.

Send the command 'p' to return a list of available commands:

Available commands:

 <nn> i     - set node ID (standard node ids are 1..30)                        
 <n> b      - set MHz band (4 = 433, 8 = 868, 9 = 915)                         
 <nnnn> o   - change frequency offset within the band (default 1600)           
              96..3903 is the range supported by the RFM12B                    
 <nnn> g    - set network group (RFM12 only allows 212, 0 = any)               
 <n> c      - set collect mode (advanced, normally 0)                          
 t          - broadcast max-size test packet, request ack                      
 ...,<nn> a - send data packet to node <nn>, request ack                       
 ...,<nn> s - send data packet to node <nn>, no ack                            
 <n> q      - set quiet mode (1 = don't report bad packets)                    
 <n> x      - set reporting format (0: decimal, 1: hex, 2: hex+ascii)          
 123 z      - total power down, needs a reset to start up again                

Remote control commands:

 <hchi>,<hclo>,<addr>,<cmd> f     - FS20 command (868 MHz)                     
 <addr>,<dev>,<on> k              - KAKU command (433 MHz)                     

Current configuration:

E i5 g210 @ 433 MHz  


These parameters can be changed in software by issuing the correct serial commands e.g b4 for 433Mhz, 210g for 210 network group.


Example commands:

  • '8b' - set frequency to 868 (default 433)
  • '210g' - set group to 210 (default)
  • '1q' - turn off quite mode (don't return bad packets '?')