MQTT/Ethernet Gateway using Leonardo(32u4), W5100, RFM69H, Hard SPI

  • Hi Folks,

    I'm brand new at this MySensors thing, and I have struggled a lot trying to get a gateway going. I think I have mostly succeeded now though, so I thought I'd share how I got here and hopefully save others some trouble.

    I'm using Arduino Leonardo which is the Uno-like board that has the ATMega32u4 processor. It has a couple of differences from the '328 varieties that will trip you up if you're not careful (at least, they certainly tripped me up). The hardware SPI pins are on the ICSP header, and the interrupt pin for the radio triggers a different INT vector, for example.

    It's also a 5V chip, so I had to use a level converter for the radio module. I was lucky that my choice of prototyping shield for the Uno had a spot for SOIC-14 and so I populated it with TXS0104E and connected the radio through that. I connected the interrupt output from the radio module (DIO0) directly to the Atmel though.

    I had to use some tinned wire to extend the pins on the ICSP header so they would connect through the W5100 shield and into the prototyping shield that I mounted the radio on. I connected the RFM69 chip-select to pin 6 of the Arduino, and used 3, 5, 7 for the LEDs.

    I fumbled around for a long time before I realised that MySensors doesn't support the RFM69 module using SoftSPI. I think this should be made more clear on the Ethernet Gateway page. I ended up learning a lot about SPI and the W5100 Ethernet Shield, and came to the conclusion that the recent work on making SPI transactions atomic seems to have made sharing the SPI port possible. So I swapped out the RFM69 driver in the MySensors library with this one which includes the new SPI transaction features.

    My sketch is posted below and it's mostly the stock MQTT client gateway with the config set appropriately. The only other change was the RFM driver swap I mentioned above.

     * Version 1.0 - Henrik Ekblad
     * The W5100 MQTT gateway sends radio network (or locally attached sensors) data to your MQTT broker.
     * The node also listens to MY_MQTT_TOPIC_PREFIX and sends out those messages to the radio network
     * LED purposes:
     * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
     * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
     * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
     * - ERR (red) - fast blink on error during transmission error or recieve crc error
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    // Enables and select radio type (if attached)
    #define MY_RADIO_RFM69
    #define MY_IS_RFM69HW
    #define MY_RFM69_FREQUENCY RF69_433MHZ
    #define MY_RF69_IRQ_PIN 2
    #define MY_RF69_IRQ_NUM  1
    #define MY_RF69_SPI_CS 6
    // Set this node's subscribe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "sensors-out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "sensors-in"
    // Set MQTT client id
    #define MY_MQTT_CLIENT_ID "sensors"
    // Enable these if your MQTT broker requires usenrame/password
    //#define MY_MQTT_USER "username"
    //#define MY_MQTT_PASSWORD "password"
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,1,30
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,1,254
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    // MQTT broker ip address
    #define MY_CONTROLLER_IP_ADDRESS 192,168,1,10
    // The MQTT broker port to to open
    #define MY_PORT 1883
    // Enable inclusion mode
    // Enable Inclusion mode button on gateway
    // Set inclusion mode duration (in seconds)
    // Digital pin used for inclusion mode button
    // Set LED blinking period
    // Flash leds on rx/tx/err
    #define MY_DEFAULT_ERR_LED_PIN 3 //3
    #define MY_DEFAULT_RX_LED_PIN  5 //5
    #define MY_DEFAULT_TX_LED_PIN  7 //7
    #include <Ethernet.h>
    #include <MySensors.h>
    void setup()
    void presentation()
    	// Present locally attached sensors here
    void loop()
    	// Send locally attached sensors data here

  • Mod

    Is your gateway still working fine? Btw, nrf24 modules are 5V tolerant in the signal pins but require 3.3V max on the vcc

  • Yep, still running well. I have a few more sensors hanging off it now but I wouldn't say it's a stressful application, maybe 1-3 messages per 10 seconds.

    The NRF modules may be 5V tolerant, but I'm using the RFM69 radio at 433MHz and they're definitely not.

  • Mod

    What level converter did you use?

  • TXS0104E

  • @Carywin Thanks for the instructions!!

    I followed it and just got it working on a Leonardo clone. I am receiving messages from nodes on the Mosquitto broker on a Raspberry Pi.

    0;255;3;0;9;Sending message on topic: sensors-out/213/213/1/0/23
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;Sending message on topic: sensors-out/213/213/1/0/23
    0;255;3;0;9;TSF:MSG:ACK REQ
    0;255;3;0;9;Sending message on topic: sensors-out/207/207/1/0/23
    0;255;3;0;9;TSF:MSG:ACK REQ
    mosquitto_sub -v -t 'sensors-out/#' -u XXXXXXX -P XXXXXXX
    sensors-out/0/255/0/0/18 2.1.1
    sensors-out/0/255/0/0/18 2.1.1
    sensors-out/213/213/1/0/23 10217
    sensors-out/208/208/1/0/0 24.69
    sensors-out/213/213/1/0/23 10218
    sensors-out/213/213/1/0/23 10219
    sensors-out/213/213/1/0/23 10220
    sensors-out/213/213/1/0/23 10221
    sensors-out/213/213/1/0/23 10222
    sensors-out/213/213/1/0/23 10223
    sensors-out/208/208/1/0/0 24.69
    sensors-out/213/213/1/0/23 10224

    I am using Mysensors 2.1.1. and didn't need to do anything on the nodes.

    Never worked with a Leonardo and spend a lot of time getting the serial port working on Win7. Also need to press reset everytime right before uploading a sketch.

    Also need to add:

    void setup() {
        while (!Serial) {;}

    otherwise Serial monitor shows nothing.

Log in to reply

Suggested Topics

  • 8
  • 1
  • 3
  • 90
  • 2
  • 2