MQTT Gateway and output controller in one?



  • Hi,
    This is my first post on this forum so I hope it is in the right place.
    At the moment I have two Arduinos that control my room automation setup.
    One is set up as a MySensors MQTT gateway that connects to the broker running on my Pi.
    The other is controlling power point outputs that have a remote control that has been modified to be controlled with the pins on the Arduino, These are currently being controlled over serial to openHAB. Is there any way to combine the MySensors MQTT gatway and the power point controller into one device (With the power point controller being moved from serial to MQTT/MySensors)?

    Current Code on the MQTT Gateway:

    #include <SPI.h>
    
    // 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 nodes subscripe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "MySensors-out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "MySensors-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
    #define MY_RF24_CE_PIN 5
    #define MY_RF24_CS_PIN 6
    
    // 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 10,0,0,202
    
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 10,0,0,138
    #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 10, 0, 0, 201
    
    // The MQTT broker port to to open 
    #define MY_PORT 1883      
    
     /*
    // Flash leds on rx/tx/err
    #define MY_LEDS_BLINKING_FEATURE
    // Set blinking period
    #define MY_DEFAULT_LED_BLINK_PERIOD 300
    
    // 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 
    
    // 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 <MySensor.h>
    
    void setup() { 
    }
    
    void presentation() {
      // Present locally attached sensors here    
    }
    
    
    void loop() {
      // Send locally attech sensors data here
    }
    
    

    Current Code on the power point controller:

    int PowerOnPin = 11;
    int PowerOffPin = 12;
    
    void setup() {
      pinMode(PowerOnPin, OUTPUT);
      pinMode(PowerOffPin, OUTPUT);
      Serial.begin(9600);
    }
    
    void loop() {
    
    
    //checking for availiable serial data
    //if avaliable it is acted upon
    if (Serial.available() > 0) {
     String input = "";
     while (Serial.available() > 0){
      input += (char) Serial.read();
      delay(5);
     }
     if (input == "ON"){
            TurnOn();
     }
     else if(input == "OFF"){
         TurnOff();
        }
     
     }
     
     }
      
    
    void TurnOff() {
     digitalWrite(PowerOffPin, HIGH);
     delay(1000);
     digitalWrite(PowerOffPin, LOW);
     delay(1000); 
    }
    
    void TurnOn() {
     digitalWrite(PowerOnPin, HIGH);
     delay(1000);
     digitalWrite(PowerOnPin, LOW);
     delay(1000); 
    }
    

    Thanks 😄 ,
    Bay101


  • Mod

    Yes, it should be possible. Convert the serial node to use MySensors. The relay example should be a good base. Add the converted code to the mqtt sketch and you should be fine.


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts