N00b + MQTT gw + openHAB2



  • Hi folks,
    I have started building my system and decided to build the MQTT gateway, use openHAB2 as a controller and NRF24 as radio.
    I have built the gw and after the 2x100ohm resistor on the w5100 module I think I have it working.
    I have and older laptop running Mosquitto and openHAB (is this suitable?).

    Problems:
    -I would like to have the gw arduino also to have a 2-relay module attached for two lights. Possible? Sketch at the bottom.
    -to test the gw+relays-on-board I have tried MQTT-commands but cant activate the relays (tested that they work with a jumper wire). I have searched but cant find the logic/command to control these.
    -when I send MQTT-topic mygateway1-in, I have no output on arduino serial. Normal?
    -the last line on the arduino (clone) is 0;255;3;0;9;Sending message on topic: mygateway1-out/0/255/0/0/18. This prints on my MQTT-spy "2.1.1". So at least something is working... After that I get "Relay" "1.0" and " " and " ", if the relay code is included.

    1. can I use the 2-relay module on the gw or make a separate relay-sensor?
    2. can I test the working of the gw with MQTT or do I have to have a controller?
    3. can't get openHAB2 to find MySensor bindings. Used typical/stable install. Uninstall and reinstall as Snapshot?

    Thanks for all and any help!

    -M

    Sketch (sorry couldn't figure how to make a box for it):

    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enables and select radio type (if attached)
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #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 14
    #define MY_SOFT_SPI_MISO_PIN 16
    #define MY_SOFT_SPI_MOSI_PIN 15
    #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 5
    #endif
    #ifndef MY_RF24_CS_PIN
    #define MY_RF24_CS_PIN 6
    #endif
    
    // 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,0,201
    #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,0,254
    #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, 0, 200
    
    // 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>
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    
    void before()
    {
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    	// Then set relay pins in output mode
    	pinMode(pin, OUTPUT);
    	// Set relay to last known state (using eeprom storage)
    	digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    }
    
    void setup()
    {
    
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay", "1.0");
    
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    	// Register all sensors to gw (they will be created as child devices)
    	present(sensor, S_BINARY);
      }
    }
    
    
    void loop()
    {
    
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_STATUS) {
    	// Change relay state
    	digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    	// Store state in eeprom
    	saveState(message.sensor, message.getBool());
    	// Write some debug info
    	Serial.print("Incoming change for sensor:");
    	Serial.print(message.sensor);
    	Serial.print(", New status: ");
    	Serial.println(message.getBool());
      }
    }
    

  • Mod

    To make things simple I'd start by using the ethernet gateway and Domoticz as controller. Once you have everything running and understood how things work you can move to mqtt and then add openhab.
    Adding relays to gateway is not a problem, you can control the relay just by setting digital pin high or low

    Ps when you add code, use the code tag



  • Thanks, will try. Sorry about the code flood 😔
    Should I try to include the code for the two relays or without? I dont have any other sensors yet to test with.
    Also I cant figure if i need to use UDP or not?
    Set the controller IP or not?


  • Mod

    To check mqtt is working you don't need controller, but if you want some easy interaction try first as I suggested: easy steps. I wanted to start with Openhab too, but it was too much complicated for a beginner while domoticz was much simpler to connect to mysensors



  • I do not fully understand why you are using MQTT. Can you quickly sketch your setup in a block diagram?

    If you want a quick setup for starters, go for

    Laptop <-> Serial Gateway Arduino <-> nrf24 ******** nrf24<->Arduino<->Relay

    then get OH2 up and running. If you use pure MQTT you don't need the MySensors binding because everything is going through the MQTT broker (mosquitto). I recently started playing with MQTT and it is way more complex than above suggested setup because you need to manually play with configs etc.

    I remember I had problems with OH2 because some java stuff was missing/not functioning. See the threads on this forum and the OH2 forums.

    Laptop is suitable, just think about the power consumption. If you let it running 24/7 I'd consider getting a (even older) Raspberry Pi. The costs will be amortized quickly. (Let's say 30W * 24h * 365d * 0.25ct/kWh = 66EUR meaning RPi will be amortized in 6 months).


  • Mod

    @pansen he is using mysensors mqtt gateway.



  • @gohan: I understand but what's the incentive? It's more easy to plug the Arduino via USB and do serial gateway than fiddling with MQTT and network stuff.


  • Mod

    @pansen ethernet has better advantages IMHO



  • I ran into NodeRed and thought it would be easy to do mqtt-functions (button press somewhere => do something elsewhere) using that and skipping coding.

    I thought the serial gateway would be a hassle in terms of testing. Thought NodeRed would ease the early testing part. Plus I want to be able to access the system from the net to check/control everything (I have DNS already). That's prob all the same.

    Upon @gohan recommendation, I wrote/tuned the ethernet sketch (see couple questions above) and "installed" Domoticz but as the compiling took SO LONG, I didn't have time to test it (weekend getaway house😀 ).

    I had an extra minilaptop lying around, installed Lubuntu on it. Thought it'd have a better life upcycled than thrown out. I have a diy UPS-system (+wind power coming) that powers the whole thing so I didnt want to buy new stuff.
    Plus, I've built the gateway, so I'd rather get it working.

    ++ if the controller breaks or locks up, I'd have a way to access & control the system with MQTT.... <= further thought: wont work right?


  • Mod

    If you want to play with node red, at the moment it is not supporting mysensors over mqtt but only serial and ethernet. Then depending on the level of integration and automation you want to have, there are different solutions: if you want some basic control of Arduino IO ports over Internet with a nice app, you could use cayenne, but if you want to have multiple wireless sensors and automation you'd better stay with mysensors and domoticz and start getting some experience; later on you can start looking at more complex scenarios.



  • @gohan Totally agree.
    I'll push forward with your recommendations: ethernet+domoticz. After looking into Domo, I think it could serve me well for my purposes as a whole.
    Any ideas about the code for the gateway: udp? controller ip? relays?
    I will build one more Mysensors relay so I can verify radio working etc.


  • Mod

    Your sketch seems ok, just make it ethernet gateway instead of mqtt.
    UDP and controller IP are nor necessary.



  • I use MQTT and Openhab2 - I find it more flexible then the Ethernet GW but yes you need to have a good understanding on all systems I suppose.

    Somethings to note

    • MySensors binding doesn't support MQTT gateway as far as I know, you need to use the MQTT binding and configure your OH items accordingly.
    • MySensors binding is in the IOT market place and not openhab-addons
    • You'll need to assign node ID to your nodes manually.

    Not entirely sure what your problem is here but don't think you've got all the right pieces for the puzzle yet.


  • Mod

    @Qu3Uk I remember that Mysensors binding works both ethernet and MQTT, but anyway ethernet is the best solution for beginners because you can use myscontroller to debug messages while connected to controller.



  • @gohan Ah really? I've loosely been following last i saw there was a proof of concept mentioned.



  • @gohan said in N00b + MQTT gw + openHAB2:

    but anyway ethernet is the best solution for beginners because you can use myscontroller to debug messages while connected to controller.

    With MQTT you can also debug using MQTT-Spy or MQTTLens or various other MQTT apps.

    MQTT is easy in interconnecting various systems without they have to know about each-other.

    As long as you have a MQTT broker (RabbitMQ, Mosquitto, ActiveMQ, ...) you're all set.

    In OpenHab in conf/services create a file called mqtt.cfg, and past the following:

    mqtt:broker.url=tcp://ip-address-of-mqttbroker:1883
    mqtt:broker.clientId=openhab
    mqtt:broker.retain=true
    mqtt:broker.async=false
    

    Then in your items file you can do:

    Switch Light_GV_Hall    			"Ceiling"       	(GV_Hall, Lights) {mqtt=">[broker:myhome/room/switch1:command:ON:1],>[broker:myhome/room/switch1:command:OFF:0],<[broker:myhome/room/switch1:state:ON:1],<[broker:myhome/room/switch1:state:OFF:0"}
    

    And now you can turn on/off the light from OpenHab as wel as from any other device that publishes to the same toppic (yes, even from MQTT-Spy and MQTTLens)


  • Mod

    @ericvdb I know, I am talking for a beginner perspective. The more advanced users know how to debug over mqtt and stuff



  • Update.
    After much trying the Ethernet gw would not connect to Domoticz.
    Then, tried my mqtt sketch again and presto! I have connection AND I can control the two relays on board the mqtt-gw.
    Had a cold one...or four!
    Thanks for the recommendations and help. I'm sure I will need more later on. Now I'm going to explore the Domoticz forum for answers.
    Unless someone with the same setup can tell me why the lights are crossed (on = off in reality and off=light on)?


  • Mod

    Depends on relay board



  • I just have the modules. low=relay on.
    I think it would be easier to change it in Domoticz or do I have to change it in the sketches (I think this will be more work when I add new sensors and relays)?


  • Mod

    Just do everything in code so you present relay status not pin status



  • @gohan How do I do that? I tried reversing the:
    #define RELAY_ON 0 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay

    I switched around the 0 and 1 but no real change...


  • Mod

    Did you get a debug log where you actually see the node receiving the message to change the relay status? Because setting the defines as you said should be correct



  • I see the mqtt message going out. I have to double check the 0 vs 1 in the messages.

    I'm about to build another sensor that has only relays. So I will troubleshoot this code & mqtt to make it match.


  • Mod

    Actually I mean the serial debug of the node, not the mqtt log



  • @gohan gonna examine that too before actually hooking it up. Thanks


Log in to reply
 

Suggested Topics

  • 3
  • 2
  • 4
  • 1
  • 3
  • 24

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts