MQTT gateway issues nrf2401 +W5100 +Arduino Mega



  • hello everbody, i am a new with my sensors devices and i try to use MQTT gateway with Rpi broker (mosquitto)

    i try to program MQTT gateway with

    • NRF2401 device
    • Arduino Mega
    • ethernet shield W5100 bought on aliexpress
      Shield ethernet

    i alreday modify the power supply of the NRF2401 with adding 10µF parallel 100nF capacitors between GND and +3.3V

    after thta i can ping the gateway with cmd window and i see on the log ( see below) that MQTT connection is OK so i conclude that the ethernet part is OK.

    i took arduino's code on my sensors web site:
    gateway code

     * 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-2018 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
     *
     * 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.
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    //#define MY_DEBUG_VERBOSE_RF24
    
    // Enables and select radio type (if attached)
    #define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    //#define MY_RF24_PA_LEVEL RF24_PA_LOW
    
    #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"
    
    // W5100 Ethernet module SPI enable (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.
    
    //#if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
    #define MY_SOFTSPI
    #define MY_SOFT_SPI_SCK_PIN 52
    #define MY_SOFT_SPI_MISO_PIN 50
    #define MY_SOFT_SPI_MOSI_PIN 51
    //#endif
    
    
    
    // When W5100 is connected we have to move CE/CSN pins for NRF radio
    
    //#ifndef MY_RF24_CE_PIN
    #define MY_RF24_CE_PIN 49
    //#endif
    //#ifndef MY_RF24_CS_PIN
    #define MY_RF24_CS_PIN 53
    //#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,1,82
    #define MY_MAC_ADDRESS 0xA8, 0x61, 0x0A, 0xAE, 0x15, 0xEC
    
    // If using static ip you can define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,1,60
    #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, 1, 62
    
    // The MQTT broker port to to open
    #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 setup()
    {
        // Setup locally attached sensors
    }
    
    void presentation()
    {
        // Present locally attached sensors here
    }
    
    void loop()
    {
        // Send locally attached sensors data here
    }
    

    the compilation and programmation of arduino mega is correct but when i looked the serial port to see the debug i have many lines of informations and i do not see information from my temperature and humidity sensor
    I do not see where is the problem

    0 MCO:BGN:INIT GW,CP=RNNGA---,REL=255,VER=2.3.1
    4 TSM:INIT
    5 TSF:WUR:MS=0
    11 TSM:INIT:TSP OK
    13 TSM:INIT:GW MODE
    15 TSM:READY:ID=0,PAR=0,DIS=0
    18 MCO:REG:NOT NEEDED
    4926 GWT:TPC:IP=192.168.1.82
    5928 MCO:BGN:STP
    5931 MCO:BGN:INIT OK,TSP=0
    6484 GWT:TPC:IP=192.168.1.82
    7487 GWT:RMQ:MQTT RECONNECT
    7494 GWT:RMQ:MQTT CONNECTED
    7497 GWT:TPS:TOPIC=mygateway1-out/0/255/0/0/18,MSG SENT
    7503 TSM:READY:NWD REQ
    10251 TSF:MSG:SEND,0-0-255-255,s=255,c=3,t=20,pt=0,l=0,sg=0,ft=0,st=OK:
    10258 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10263 !TSF:MSG:LEN=0,EXP=7
    10265 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10270 !TSF:MSG:LEN=0,EXP=7
    10273 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10277 !TSF:MSG:LEN=0,EXP=7
    10280 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10285 !TSF:MSG:LEN=0,EXP=7
    10288 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10293 !TSF:MSG:LEN=0,EXP=7
    10296 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10300 !TSF:MSG:LEN=0,EXP=7
    10303 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10308 !TSF:MSG:LEN=0,EXP=7
    10310 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10315 !TSF:MSG:LEN=0,EXP=7
    10317 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10322 !TSF:MSG:LEN=0,EXP=7
    10326 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    10330 !TSF:MSG:LEN=0,EXP=7
    10333 TSF:MSG:READ,0-0-0,s=0,c=0,t=0,pt=0,l=0,sg=0:
    

    i try to change mysensors librairies but it did not work

    can you help me ? i have no solution for my problem .

    thank you very much for your help


  • Mod

    Hi @loc73, welcome to the MySensors community!

    Could you post the debug log from your dht node? Did you use this sketch without modifications? If not, please post the modified sketch also. What type of Arduino are you using for the dht node?



  • hello, thank you very much for the quick answer.

    i programmed my DHT node with the sketch that you given. below you can see the log of the node.

    the MQTT gateway is switch on during the recording.

    i build my sensors with arduino nano and DHT 11.

    i ordered electronic boards from this web site MY RFX. i am sure of the node because when i program a serial gateway with the same MEGA + NRF2401 i can see datas on serial monitor.

    the shield w5100 is ok because i used the same to send MQTT datas to the my brokers and i can see datas on the right topic.

    the only thing not very clean is i used 5cm wires to connect NRF2401 on arduino MEGA, maybe i will try to reduce the length of these wires.

    one more time thank you very much for your help.

    Insert Co
     __  __       ____
    |  \/  |_   _/ ___|  ___ _ __  ___  ___  _ __ ___
    | |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
    | |  | | |_| |___| |  __/ | | \__ \  _  | |  \__ \
    |_|  |_|\__, |____/ \___|_| |_|___/\___/|_|  |___/
            |___/                      2.3.1
    
    16 MCO:BGN:INIT NODE,CP=RNNNA---,REL=255,VER=2.3.1
    26 TSM:INIT
    27 TSF:WUR:MS=0
    34 TSM:INIT:TSP OK
    35 TSM:FPAR
    38 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2046 !TSM:FPAR:NO REPLY
    2049 TSM:FPAR
    2051 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    4059 !TSM:FPAR:NO REPLY
    4061 TSM:FPAR
    4063 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    6071 !TSM:FPAR:NO REPLY
    6073 TSM:FPAR
    6075 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    8083 !TSM:FPAR:FAIL
    8084 TSM:FAIL:CNT=1
    8086 TSM:FAIL:DIS
    8088 TSF:TDI:TSL
    18091 TSM:FAIL:RE-INIT
    18093 TSM:INIT
    18099 TSM:INIT:TSP OK
    18101 TSM:FPAR
    18104 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    20112 !TSM:FPAR:NO REPLY
    20114 TSM:FPAR
    20116 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    22124 !TSM:FPAR:NO REPLY
    22126 TSM:FPAR
    22129 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    24136 !TSM:FPAR:NO REPLY
    24138 TSM:FPAR
    24140 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    26148 !TSM:FPAR:FAIL
    26150 TSM:FAIL:CNT=2
    26151 TSM:FAIL:DIS
    26153 TSF:TDI:TSL
    36157 TSM:FAIL:RE-INIT
    36159 TSM:INIT
    36165 TSM:INIT:TSP OK
    36167 TSM:FPAR
    36170 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    38178 !TSM:FPAR:NO REPLY
    38180 TSM:FPAR
    38182 TSF:MSG:SEND,255-255-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    40192 !TSM:FPAR:NO REPLY
    40194 TSM:FPARde Here
    

    the sketch programmed

    /**
     * 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-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/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 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
     * 
     * DESCRIPTION
     * This sketch provides an example of how to implement a humidity/temperature
     * sensor using a DHT11/DHT-22.
     *  
     * For more information, please visit:
     * http://www.mysensors.org/build/humidity
     * 
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    //#define MY_RS485
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT.h>
    
    // Set this to the pin you connected the DHT's data pin to
    #define DHT_DATA_PIN 3
    
    // Set this offset if the sensor has a permanent small offset to the real temperatures.
    // In Celsius degrees (as measured by the device)
    #define SENSOR_TEMP_OFFSET 0
    
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 10000;
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    
    void presentation()  
    { 
      // Send the sketch version information to the gateway
      sendSketchInfo("TemperatureAndHumidity", "1.1");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
    
      metric = getControllerConfig().isMetric;
    }
    
    
    void setup()
    {
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      }
      // Sleep for the time of the minimum sampling period to give the sensor time to power up
      // (otherwise, timeout errors might occure for the first reading)
      sleep(dht.getMinimumSamplingPeriod());
    }
    
    
    void loop()      
    {  
      // Force reading sensor, so it works also after sleep()
      dht.readSensor(true);
    
      // Get temperature from DHT library
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
        // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
        lastTemp = temperature;
    
        // apply the offset before converting to something different than Celsius degrees
        temperature += SENSOR_TEMP_OFFSET;
    
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        send(msgTemp.set(temperature, 1));
    
        #ifdef MY_DEBUG
        Serial.print("T: ");
        Serial.println(temperature);
        #endif
      } else {
        // Increase no update counter if the temperature stayed the same
        nNoUpdatesTemp++;
      }
    
      // Get humidity from DHT library
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
        Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
        // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
        lastHum = humidity;
        // Reset no updates counter
        nNoUpdatesHum = 0;
        send(msgHum.set(humidity, 1));
    
        #ifdef MY_DEBUG
        Serial.print("H: ");
        Serial.println(humidity);
        #endif
      } else {
        // Increase no update counter if the humidity stayed the same
        nNoUpdatesHum++;
      }
    
      // Sleep for a while to save energy
      sleep(UPDATE_INTERVAL); 
    }```

  • Mod

    @loc73 the sketches look ok.

    Most common reason for failed communi ation is unstable power to the nrf24. How is it powered (on both nodes)? What capacitors are you using?

    If you haven't already, see https://forum.mysensors.org/topic/666/debug-faq-and-how-ask-for-help for a flowchart that covers how to troubleshoot the most common problems.



  • I had weird problems as well. My W5100 module was at fault.
    One module needed resistors and one didn't. I never figured out the problem. I also had what seemed a good ethernet but weird connection problems.

    Check this thread out, just in case it helps.
    https://forum.arduino.cc/index.php?topic=351477.msg3215306#msg3215306



  • thank you very much for the answer, i will try your advice and i will make a return of experience.


  • Mod

    @loc73 I have an identical (?) ethernet shield that requires pullup of the SD SS line for the nRF24 module to work correctly.

    On an Arduino Uno (to which the shield stacks on top), the SD_SS_PIN connects to pin 4.
    Therefore I added the following code to my gateway sketch:

    #define SD_SS_PIN                     (4)  // chip-select for SD card (unused, pull high!)  
    
    void before() {
        pinMode(SD_SS_PIN, OUTPUT);
        digitalWrite(SD_SS_PIN, HIGH); 
    } 
    

    Yours would probably connect to a different pin, but idea is the same.



  • @Yveaux i try your advice to add pull up resistor in my code and it works immediately!!!!!!

    THANK YOU VERY MUCH !!!!!!

    i will never find the solution without your help.

    @MasMat @mfalkvidd thank you too i keep in mind th flowvhart and resistors on the ethernet connector for next issues......


Log in to reply
 

Suggested Topics

  • 3
  • 6
  • 2
  • 1

11
Online

11.4k
Users

11.1k
Topics

112.7k
Posts