Author Topic: [Update] RFM69 Library for ESP8266 - Moteino Compatible!  (Read 52041 times)

someburner

  • NewMember
  • *
  • Posts: 13
[Update] RFM69 Library for ESP8266 - Moteino Compatible!
« on: October 29, 2015, 01:06:31 PM »
Hello all,

I'm working for an IoT startup that is currently prototyping with Moteinos. The idea is a home base unit + Moteino outside the home. Anyways, we needed a low-cost WiFi unit that would interface well with a Moteino. So I've spent quite some time working out most of the kinks and can proudly say that I have a fully functioning base unit! To my knowledge, everything works like a normal Moteino should, minus implementation (different function names and different way of calling them). And it is written in C. I tried using the ESP8266-Arduino library at first, but it was too messy trying to use that and Hardware SPI on the ESP8266 at the same time.

Originally I started out by using the esp-link project, but have mostly moved away from it and just use esphttpd and done some modifications to various libraries here and there.

One of the major things I got working was OTA firmware updating. Right now the process goes: you compile your FW, use a program to convert it to a .bin file, then upload it through the web console, which copies it to SPI flash. Then you hit begin and it begins the process. It may be a bit buggy because SPI flash must be written to/from in 32 bit chunks, and I haven't thoroughly tested it. Also, this has all been cropped out from the project I'm working on so there may be a few orphaned things in there that need to be cleaned up.

But anyways, feel free to check it out. Any and all feedback, comments, suggestions, bugs are appreciated! I'm fairly busy most of the time, but I should be able to help here and there if people have issues. If you are having general ESP8266 troubles you should first try checking out the esp-link project, as it is a similar project and has more information on how to compile stuff.

https://github.com/someburner/esp-rfm69

I've also included a basic .ino that is loaded on the Moteino and just sends a test message every 10 seconds or so. And below are the screenshots of what you should see outputted.

https://github.com/someburner/esp-rfm69/blob/master/doc/SendTest.ino

Screenshots: (Rssi value is like it is because I didn't have an antenna attached for this!)




« Last Edit: March 09, 2016, 01:10:27 AM by someburner »

someburner

  • NewMember
  • *
  • Posts: 13
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #1 on: March 09, 2016, 01:35:05 AM »
Howdy,

Just wanted to share that I've done a pretty major overhaul and you can find it at the same place: https://github.com/someburner/esp-rfm69

Mainly I switched over to a nginx-based HTTP server and am now using SPIFFS for all the SPI Flash stuff. So now it's much easier to upload firmware (drag-and-drop). I pretty much re-wrote the driver as well, and it should be more stable.

I saw that Felix made a post about the ESP8266. While I agree that sub-1GHz is better for low-power and range, I think it's unfair to completely dismiss the device and I think it has great potential as a Gateway device.

So why ESP8266?

Well, it's a WiFi-ready chip, with 4MB SPI Flash for $2.

Besides price, IMO it's overkill to be using a Raspberry Pi as a gateway device. Not to mention a lot less portable. The ESP8266 supports OTA updating and the compiled binaries total to less than 1MB. In contrast, to set up a RPi you have to purchase one for a minimum of $15 (A+), format an SD card, add a WiFi dongle, install Linux, and whatever else I'm forgetting. And unless you need your gateway to have video output for some reason, an ESP8266 gateway serving a small web app is just as good.

The downside is having to code in C, or perhaps C++. But the project to add MicroPython support to the ESP8266 looks promising so that may not be the case for much longer.


joelucid

  • Hero Member
  • *****
  • Posts: 868
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #2 on: March 09, 2016, 05:32:09 AM »
Hey burner,

so this spiked my interest since I'm currently evaluating a esp8266 based gateway. I looked at your stuff and then - just to make sure I don't invest too much time in the native esp8266 route if an Arduino approach might work - I compiled RFM69.cpp as is for the esp8266.

I wired a rfm69hw to the esp HSPI port and DIO0 to D2. Then in RFM69.cpp:

Code: [Select]
#define RF69_IRQ_PIN          D2
#define RF69_IRQ_NUM          D2

In select() instead of MODE0 I used: 

Code: [Select]
SPI.setDataMode(SPI_MODE0);

And finally I thew out the SPI config caching in select and unselect.

To my absolute astonishment with these three changes it just worked. I have a gateway sketch running and it's perfectly showing incoming packets. With an RSSI much better than I ever got on the Pi. Even though this is all on a breadboard.

So I guess this raises the question why a native port?

Joe

joelucid

  • Hero Member
  • *****
  • Posts: 868
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #3 on: March 09, 2016, 05:40:08 AM »


Pretty amazing that this setup results in an RSSI at least 10 better than a Moteino mounted on a Pi2 right next to it.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #4 on: March 09, 2016, 08:06:21 AM »
Pretty amazing that this setup results in an RSSI at least 10 better than a Moteino mounted on a Pi2 right next to it.
Nice, what is the RSSI? Are all other things equal for comparison sake?
« Last Edit: March 09, 2016, 08:07:55 AM by Felix »

joelucid

  • Hero Member
  • *****
  • Posts: 868
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #5 on: March 09, 2016, 08:26:38 AM »
Quote
Nice, what is the RSSI? Are all other things equal for comparison sake?

From the bathroom moteino I get an RSSI of ~58 from the ESP8266 and ~65 at best from the Pi. Of course as you remember I have the periodic RSSI impairment problem, so around half of all packets are received at the Pi at an RSSI of >80.

Haven't done much testing yet though. But I'm excited that your lib runs unchanged. Amazing. With 4MB of flash and a rfm69hw added the ESP8266 should make for a great boot server.

john4444

  • Jr. Member
  • **
  • Posts: 71
  • Country: us
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #6 on: March 09, 2016, 03:36:04 PM »
Quote
I still don't know of an alternate hypothesis to explain why RSSI would appear as less on the Pi, unless the Pi is somehow adversely changing something ...
The PI generates a lot of noise on the +5v/ground lines as well as emitted RF.
The effect of that noise is that the received RSSI levels appear weaker.
However, this effect is entirely due to the higher noise-floor at the receiver, not the actual transmitted signal level.

BTW the latest RasPi, model 3 has much less "noise" than earlier models.
John AE5HQ

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #7 on: March 09, 2016, 04:35:24 PM »
WHere did you order your Pi3 from?

someburner

  • NewMember
  • *
  • Posts: 13
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #8 on: March 10, 2016, 12:59:47 AM »

So I guess this raises the question why a native port?


Good to know that it works natively with the Arduino port now!

As to your question, I am working towards designing a consumer product so reliability is important. The Arduino port code base is pretty large and it's easier to debug my own code that has the bare minimum of things my project requires. Besides that:

1. When I first got going with the ESP8266 the Arduino SPI libraries were pretty much non-existent so I didn't have a choice initially
2. Because of #1, I'm now very familiar with the Native SDK, so I can easily make any sort of customization and not have to worry about breaking Arduino code, or vice-vera.
3. I still don't trust the Arduino port with regards to handling interrupts, or long-term stability.
4. Overhead. I'm curious how much Arduino really adds, but since I've already written it in C I don't see the benefit of switching.
5. Code portability. ESP32 is on the way or I may switch to a different platform entirely.
6. Just the overall idea of using a setup(), and loop() running over an SDK that actually executes like an RTOS with callbacks seems counter intuitive to me.

To expand on #6, that is also why you can see I've implemented the driver more like a state machine. The original RFM69 driver has a number of blocking while loops which are dangerous to use with the ESP8266. A simple use case with a few send and receives here and there works fine, as you know. But try doing an OTA update or something with a high volume of packets and that will crash and burn. On the flip side, the ESP8266 is much faster than a Moteino, running at 80 MHz, so writing the driver in a way that 'makes sense' for the ESP8266's OS can give us a considerable performance boost.

With that said, I'll definitely consider switching over to Micropython when a stable port becomes available.

WhiteHare

  • Hero Member
  • *****
  • Posts: 1300
  • Country: us
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #9 on: March 10, 2016, 09:30:34 AM »

In select() instead of MODE0 I used: 

Code: [Select]
SPI.setDataMode(SPI_MODE0);

"SPI.setDataMode(SPI_MODE0);" is the code that's already there.  Did you mean SPI_MODE1?  I have confirmed (https://github.com/esp8266/Arduino/blob/master/libraries/SPI/SPI.h) that:
Code: [Select]
const uint8_t SPI_MODE0 = 0x00; ///<  CPOL: 0  CPHA: 0
const uint8_t SPI_MODE1 = 0x01; ///<  CPOL: 0  CPHA: 1
const uint8_t SPI_MODE2 = 0x10; ///<  CPOL: 1  CPHA: 0
const uint8_t SPI_MODE3 = 0x11; ///<  CPOL: 1  CPHA: 1

That is the same as Table 19-2 (SPI Modes) in the atmega328p datasheet. However, the SX1231H datasheet (page 44) says, "The SPI interface gives access to the configuration register via a synchronous full-duplex protocol corresponding to CPOL = 0 and CPHA = 0 in Motorola/Freescale nomenclature. "  Therefore, isn't the existing SPI_MODE0 parameter used in SetDataMode already correct?




And finally I thew out the SPI config caching in select and unselect.


By that, do you mean deleting
Code: [Select]
  _SPCR = SPCR;
  _SPSR = SPSR;
from select(), and deleting
Code: [Select]
  SPCR = _SPCR;
  SPSR = _SPSR;
from unselect()?  I guess those were there to handle interrupt contention between the RFM69 and the SPI flash memory, which no longer exists.

« Last Edit: March 11, 2016, 12:52:15 PM by WhiteHare »

john4444

  • Jr. Member
  • **
  • Posts: 71
  • Country: us
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #10 on: March 10, 2016, 12:00:57 PM »
Quote
WHere did you order your Pi3 from?
Adafruit
John AE5HQ

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #11 on: March 10, 2016, 12:04:11 PM »
Yup, I was looking to get one too but I was too late, out of stock now.

jra

  • Jr. Member
  • **
  • Posts: 81
  • Country: us
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #12 on: March 10, 2016, 03:51:15 PM »
This seems to happen every time a new Pi flavor comes out.  Adafruit, Element14 and the usual suspects have them for $35, they sell out in 4 hours, someone starts selling the few they managed to scarf up for $60 on Amazon and everyone gives it a 1 star review because of the price.  While waiting a month or so for stock to be replenished there will be a flurry of articles stating that Odroid or Orange or Banana or Beagle has a better/faster/cheaper solution.  Attaching an RFM69 directly to a ESP8266 opens up a whole range of intriguing inexpensive possibilities independent of the better noise immunity.  If Joe's patch makes it into github my one request would be to #if defined(ESP8266) the patch so the same code can be compiled on ATmega328 and ATmega1284 as well.

joelucid

  • Hero Member
  • *****
  • Posts: 868
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #13 on: March 10, 2016, 05:18:54 PM »
Quote
Attaching an RFM69 directly to a ESP8266 opens up a whole range of intriguing inexpensive possibilities independent of the better noise immunity.

I'm still working on getting comfortable with the ESP8266. The documentation is just so horrible if you're used to normal datasheets and it feels a bit like a waste of time to search around for basic info that could easily been written up in a decent datasheet.

On the other hand I completely agree cutting out the 328p is not only intriguingly inexpensive but also so much simpler. I have a whole encoding scheme to ensure failsafe and recoverable serial communication between Pi and 328p without ack latency. As soon as you speak SPI that just goes completely away.

And having a reasonably powered mcu as gateway gives you the flexibility to really get creative on the network protocol side. I mean just think about it: 255 nodes and 2048 byte RAM. That gives you 8 byte per node. Not much if you want negotiated bitrates, rx time windows etc. And doing it on the Pi creates all kinds of timing issues.

Definitely worth a bit more experimenting.

joelucid

  • Hero Member
  • *****
  • Posts: 868
Re: [Update] RFM69 Library for ESP8266 - Moteino Compatible!
« Reply #14 on: March 11, 2016, 06:12:07 AM »
Quote
"SPI.setDataMode(SPI_MODE0);" is the code that's already there

True, just verified the diff. Sorry I can't remember what bit me there.

Quote
By that, do you mean deleting
Code: [Select]
  _SPCR = SPCR;
  _SPSR = SPSR;
from select(), and deleting
Code: [Select]
  SPCR = _SPCR;
  SPSR = _SPSR;
from unselect()?  I guess those were there to handle interrupt contention between the RFM69 and the SPI flash memory, which no longer exists.

Yes.