[SOLVED] MQTT gateway based on Arduino Uno - Uno compatible W5100 Ethernet shield - RFM69HW radio



  • Hi Folks,

    I'm new to MySensors, so before beginning building things, I read the docs, search the forum, etc., to be able to form me an idea how to start.

    First taking the docs: https://www.mysensors.org/build/ethernet_gateway and https://www.mysensors.org/build/mqtt_gateway

    The MQTT gateway is based on the ethernet gateway, so we are starting there.
    The recommandation is to use a W5100 Ethernet module. This module has problems sharing SPI with the radio, so it is proposed to use soft-spi for the radio.
    The W5100 ethernet shield module is hardware wired:
    0_1556721306433_arduino-ethernet-shield-pinout.png

    The radio should then be wired for soft-spi following the docs.
    Because there is no example for a RFM69HW, I imagine that we must follow the same schematic as for the NRF24L01 radio.

    The given sketch is based on the use of a NRF24L01 radio, so when searching for the correct settings for RFM69HW in the forum, I stumbled on plenty of topics of people trying to build the same thing.
    Some topics are already several years old, speaking about changing core ethernet libraries for the W5100 to disable the interrupts, others are building on forks of MySensors, forks of RFM69 library, etc.
    I saw also a remark in a topic that for using soft-spi, we should use the new RFM69 driver in the MySensors lib.
    In one of the topics with an own fork, was mentioned that, because the transactions in SPI where now supported in the RFM69 library, we can now wire also the RFM69HW radio to the same ports on the Arduino as the ethernet module.

    So, when reading all these topics, I did loose my view on the things 😞 :
    With the current version of the MySensors library (2.3.1)



  • Nobody has never build a gateway based on an Arduino Uno, a W5100 shield and a RFM69HW?

    So I will answer my own questions 🙂
    I did take my chance and choose to build first a gateway version using the hardware SPI bus (no soft spi) and I was lucky, it worked directly! So no need to try the soft-spi version.

    Because I did already build previously a sensor (https://www.mysensors.org/build/distance), I had all hardware already ready to be used.

    So what is the used hardware?

    The hardware is wired following next schematic:
    0_1556974214950_Arduino Uno Ethernet Gateway.png

    Connecting the RFM69HW hardware was somewhat tricky. The used pin headers on the RFM69HW are not following the standard spaced 2.54mm pin headers like on the Arduino print. I did not have a converter plate, so I soldered some hard wire into the RFM69HW pins and bend them into the pin holes from the protoboard. See photo.
    0_1556974628264_DSC_1460.JPG
    So what about the software:

    • Arduino IDE version 1.8.9
    • MySensors library 2.3.1

    I did take as base the sketch found on the website https://www.mysensors.org/build/mqtt_gateway or https://github.com/mysensors/MySensors/blob/master/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino

    Important is to configure the specific RFM69 defines
    #define MY_RADIO_RFM69
    #define MY_RFM69_FREQUENCY RFM69_868MHZ // Set your frequency here
    #define MY_IS_RFM69HW // Omit if your RFM is not "H"
    #define MY_RFM69_NEW_DRIVER

    When the W5100 Arduino shield is connected we have to move SS (slave select) pin for the RFM69 radio because the shield is using the default D10
    #define MY_RFM69_CS_PIN 6

    /*
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2019 Sensnology AB
     * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * Version 1.1 - Eric Van Bocxlaer
     *
     * DESCRIPTION
     * 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 received. In inclusion mode will blink fast only on presentation received
     * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
     * - ERR (red) - fast blink on error during transmission error or receive crc error
     *
     * See http://www.mysensors.org/build/esp8266_gateway for wiring instructions.
     * nRF24L01+  ESP8266
     * VCC        VCC
     * CE         GPIO4
     * CSN/CS     GPIO15
     * SCK        GPIO14
     * MISO       GPIO12
     * MOSI       GPIO13
     *
     * Not all ESP8266 modules have all pins available on their external interface.
     * This code has been tested on an ESP-12 module.
     * The ESP8266 requires a certain pin configuration to download code, and another one to run code:
     * - Connect REST (reset) via 10K pullup resistor to VCC, and via switch to GND ('reset switch')
     * - Connect GPIO15 via 10K pulldown resistor to GND
     * - Connect CH_PD via 10K resistor to VCC
     * - Connect GPIO2 via 10K resistor to VCC
     * - Connect GPIO0 via 10K resistor to VCC, and via switch to GND ('bootload switch')
     *
      * Inclusion mode button:
     * - Connect GPIO5 via switch to GND ('inclusion switch')
     *
     * Hardware SHA204 signing is currently not supported!
     *
     * Make sure to fill in your ssid and WiFi password below for ssid & pass.
     * 
     * ***************************************************************************************************************
     * Connection with RFM69HW and EthernetShieldW5100 through the SPI bus (Serial Peripheral Interface)
     * On this bus we can have only one master (= the Arduino Uno) and different slaves (= the W5100 and the RFM69HW).
     * The master can only talk to one slave each time!
     * 
     * RFM69HW    Uno R3    EthernetShieldW5100(autowired)
     * VCC 3.3V   VCC 3.3V  
     *            VCC 5.0V  VCC 5.0V
     * DIO0       D2/INT0
     * NSS        D6
     * SCK        D13/SCK   SCK (D13)
     * MISO       D12/MISO  MISO (D12)
     * MOSI       D11/MOSI  MOSI (D11)
     *            D4        SS (sd-card)
     *            D10/SS    SS (W5100)(Default in ethernet library: do not wire different with a no shield version!)
     *            
     * 
     * NSS/SS : Slave Select for SPI bus (the master selects with which slave he want to talk)
     * SCK : serial clock signal delivered by the master
     * MISO : Master Input Slave Output (arduino receives data from the slave)
     * MOSI : Master Output Slave Input (arduino sends data to the slave)
     */
    
    
    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    // Enable specific RFM69 debug prints to serial monitor
    //#define MY_DEBUG_VERBOSE_RFM69
    
    // Enables and select radio type (if attached)
    //#define MY_RADIO_RF24
    //#define MY_RADIO_RFM95
    #define MY_RADIO_RFM69
    #define MY_RFM69_FREQUENCY RFM69_868MHZ // Set your frequency here
    #define MY_IS_RFM69HW // Omit if your RFM is not "H"
    #define MY_RFM69_NEW_DRIVER
    
    #define MY_GATEWAY_MQTT_CLIENT
    
    // Set this node's subscribe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "mygateway1-out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in"
    
    // Set MQTT client id
    #define MY_MQTT_CLIENT_ID "mysensors-1"
    
    // Enable gateway ethernet module type : not needed here because MY_GATEWAY_MQTT_CLIENT defines already the use of W5100
    //#define MY_GATEWAY_W5100
    #define SD_SS_PIN (4)  // chip-select for SD card (normally unused, pull high!, see code!) 
    
    // W5100 Ethernet module SPI enable (this setting is optional if using a shield/module that manages SPI_EN signal)
    //#define MY_W5100_SPI_EN 4
    
    // Enable Soft SPI for NRF radio (note different radio wiring is required)
    // The W5100 ethernet module seems to have a hard time co-operate with
    // radio on the same spi bus.
    // Situation on 2019-05-04: with the new RFM69HW driver this is not true anymore for a radio RFM69!
    //#if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
    //#define MY_SOFTSPI
    //#define MY_SOFT_SPI_SCK_PIN 14
    //#define MY_SOFT_SPI_MISO_PIN 16
    //#define MY_SOFT_SPI_MOSI_PIN 15
    //#endif
    
    // When W5100 shield is connected we have to move SS (slave select) pin for the RFM69 radio because the shield
    // is using the default D10
    #define MY_RFM69_CS_PIN 6
    
    // When W5100 is connected we have to move CE/CSN pins for NRF radio
    //#ifndef MY_RF24_CE_PIN
    //#define MY_RF24_CE_PIN 5
    //#endif
    //#ifndef MY_RF24_CS_PIN
    //#define MY_RF24_CS_PIN 6
    //#endif
    
    // Enable these if your MQTT broker requires username/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,178,87
    
    // If using static ip you can define Gateway and Subnet address as well
    //#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
    //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    // MQTT broker ip address or url. Define one or the other.
    //#define MY_CONTROLLER_URL_ADDRESS "m20.cloudmqtt.com"
    #define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
    
    // The MQTT broker port to to open
    // With simple arduino uno's we can't use encrypted communication!
    #define MY_PORT 1883
    
    /*
    // Enable inclusion mode
    #define MY_INCLUSION_MODE_FEATURE
    // Enable Inclusion mode button on gateway
    //#define MY_INCLUSION_BUTTON_FEATURE
    // Set inclusion mode duration (in seconds)
    #define MY_INCLUSION_MODE_DURATION 60
    // Digital pin used for inclusion mode button
    //#define MY_INCLUSION_MODE_BUTTON_PIN  3
    
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // Flash leds on rx/tx/err
    // Uncomment to override default HW configurations
    //#define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    //#define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    //#define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    */
    
    #include <Ethernet.h>
    #include <MySensors.h>
    
    void before() {
        pinMode(SD_SS_PIN, OUTPUT);
        digitalWrite(SD_SS_PIN, HIGH); 
    }
    
    void setup()
    {
        // Setup locally attached sensors
    }
    
    void presentation()
    {
        // Present locally attached sensors here
    }
    
    void loop()
    {
        // Send locally attached sensors data here
    }
    

    I hope that this will be helpful for some people trying to build something equivalent 🙂


 

182
Online

8.7k
Users

9.5k
Topics

99.9k
Posts