Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. [SOLVED] MQTT message does not show (MySensors on ESP8266)

[SOLVED] MQTT message does not show (MySensors on ESP8266)

Scheduled Pinned Locked Moved Troubleshooting
4 Posts 3 Posters 1.8k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pansen
    wrote on last edited by pansen
    #1

    Hi,

    I put the below code on a Sonoff switch (which has an ESP8266 on it). I installed mosquitto as MQTT broker on my orange pi and tested it, so the broker is ok.

    Now, when I subscribe to the topic I set, nothing is shown when my relay state changes:

    mosquitto_sub -h localhost -t Sonoff-Out
    

    I am sure I am just missing some configuration or a command...Any ideas?

    main.h

    #define PIN_PIR_SENSOR 14
    #define PIN_RELAY  12
    #define PIN_LED 13
    #define RELAY_ON 1
    #define RELAY_OFF 0
    #define LED_ON 0
    #define LED_OFF 1
    
    #define CHILD_ID 73
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
    #define MY_BAUD_RATE 115200
    
    #define MY_GATEWAY_MQTT_CLIENT
    #define MY_GATEWAY_ESP8266
    
    #define MY_ESP8266_SSID "REMOVED"
    #define MY_ESP8266_PASSWORD "REMOVED"
    #define MY_ESP8266_HOSTNAME "Sonoff-PIRSwitch-Lamp"
    
    //#define MY_IP_ADDRESS 1,1,1,80
    //#define MY_IP_GATEWAY_ADDRESS 1,1,1,10
    //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
    
    //#define MY_USE_UDP
    #define MY_CONTROLLER_IP_ADDRESS 1,1,1,1
    
    // The port to keep open on node server mode
    #define MY_PORT 1883
    
    // How many clients should be able to connect to this gateway (default 1)
    //#define MY_GATEWAY_MAX_CLIENTS 1
    
    // Set this node's subscribe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "Sonoff-Out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "Sonoff-In"
    
    // Set MQTT client id
    #define MY_MQTT_CLIENT_ID "Sonoff-PIRSwitch-Lamp"
    
    #if defined(MY_USE_UDP)
    #include <WiFiUdp.h>
    #endif
    
    #include <Arduino.h>
    //#include <ESP8266mDNS.h>
    #include <ESP8266WiFi.h>
    #include <MySensors.h>
    
    void setup();
    void loop();
    void present();
    void receive(const MyMessage &message);
    void ISR_PIRPinHasChanged();
    void SetRelayAndLedStatus();
    void ToggleRelay();
    void TransmitStatusToController();
    

    main.cpp

    #include "main.h"
    
    int currentRelayStatus;
    MyMessage msg(CHILD_ID,V_STATUS);
    
    void setup(){
      pinMode(PIN_LED, OUTPUT);
      pinMode(PIN_RELAY, OUTPUT);
      pinMode(PIN_PIR_SENSOR, INPUT);
    
      currentRelayStatus = RELAY_OFF;
      attachInterrupt(PIN_PIR_SENSOR, ISR_PIRPinHasChanged, RISING);
    
      //flash led
      digitalWrite(PIN_LED,LED_ON);
      delay(100);
      digitalWrite(PIN_LED,LED_OFF);
      delay(100);
      digitalWrite(PIN_LED,LED_OFF);
      delay(100);
      digitalWrite(PIN_LED,LED_OFF);
      delay(100);
    }
    
    void loop()
    {
    
    }
    
    void presentation()  {
      // Send the sketch version information
      sendSketchInfo("Sonoff PIR Switch", "1.1");
      // Register sensor
      present(CHILD_ID, S_BINARY);
      SetRelayAndLedStatus();
      TransmitStatusToController();
    }
    
    void receive(const MyMessage &message)
    {
      if (message.type==V_STATUS  && message.sensor==CHILD_ID) {
        if(message.getBool() == true)
        {
          currentRelayStatus = RELAY_ON;
        }
        else{
          currentRelayStatus = RELAY_OFF;
        }
        SetRelayAndLedStatus();
        TransmitStatusToController();
      }
    }
    
    
    void ISR_PIRPinHasChanged(){
      int currentPinStatus = digitalRead(PIN_PIR_SENSOR);
      if(currentPinStatus == LOW){
        Serial.println("ISR fired, pin LOW");
      }
      else{
        Serial.println("ISR fired, pin HIGH");
        ToggleRelay();
      }
    }
    
    void ToggleRelay(){
      if(currentRelayStatus == RELAY_ON){
        Serial.println("toggling relay to OFF");
        currentRelayStatus = RELAY_OFF;
      }
      else{
        Serial.println("toggling relay to ON");
        currentRelayStatus = RELAY_ON;
      }
      SetRelayAndLedStatus();
      TransmitStatusToController();
    }
    
    void SetRelayAndLedStatus(){
      if(currentRelayStatus == RELAY_ON){
        Serial.println("switching on");
        digitalWrite(PIN_LED, LED_ON);
        digitalWrite(PIN_RELAY, RELAY_ON);
      }
      else{
        Serial.println("switching off");
        digitalWrite(PIN_LED, LED_OFF);
        digitalWrite(PIN_RELAY, RELAY_OFF);
      }
    }
    
    void TransmitStatusToController(){
      Serial.println("sending status to controller");
      if(currentRelayStatus == RELAY_ON){
        send(msg.set(true));
      }
      else{
        send(msg.set(false));
      }
    }
    

    Orange Pi Plus 2e connected to nrf24 PA via SPI running git-development MySensors gateway, OpenHAB2, mosquitto and MySQL persistence.

    Y 1 Reply Last reply
    0
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #2

      Have you considered using easy esp or other similar firmware?

      P 1 Reply Last reply
      0
      • P pansen

        Hi,

        I put the below code on a Sonoff switch (which has an ESP8266 on it). I installed mosquitto as MQTT broker on my orange pi and tested it, so the broker is ok.

        Now, when I subscribe to the topic I set, nothing is shown when my relay state changes:

        mosquitto_sub -h localhost -t Sonoff-Out
        

        I am sure I am just missing some configuration or a command...Any ideas?

        main.h

        #define PIN_PIR_SENSOR 14
        #define PIN_RELAY  12
        #define PIN_LED 13
        #define RELAY_ON 1
        #define RELAY_OFF 0
        #define LED_ON 0
        #define LED_OFF 1
        
        #define CHILD_ID 73
        
        // Enable debug prints to serial monitor
        #define MY_DEBUG
        
        // Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h
        #define MY_BAUD_RATE 115200
        
        #define MY_GATEWAY_MQTT_CLIENT
        #define MY_GATEWAY_ESP8266
        
        #define MY_ESP8266_SSID "REMOVED"
        #define MY_ESP8266_PASSWORD "REMOVED"
        #define MY_ESP8266_HOSTNAME "Sonoff-PIRSwitch-Lamp"
        
        //#define MY_IP_ADDRESS 1,1,1,80
        //#define MY_IP_GATEWAY_ADDRESS 1,1,1,10
        //#define MY_IP_SUBNET_ADDRESS 255,255,255,0
        
        //#define MY_USE_UDP
        #define MY_CONTROLLER_IP_ADDRESS 1,1,1,1
        
        // The port to keep open on node server mode
        #define MY_PORT 1883
        
        // How many clients should be able to connect to this gateway (default 1)
        //#define MY_GATEWAY_MAX_CLIENTS 1
        
        // Set this node's subscribe and publish topic prefix
        #define MY_MQTT_PUBLISH_TOPIC_PREFIX "Sonoff-Out"
        #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "Sonoff-In"
        
        // Set MQTT client id
        #define MY_MQTT_CLIENT_ID "Sonoff-PIRSwitch-Lamp"
        
        #if defined(MY_USE_UDP)
        #include <WiFiUdp.h>
        #endif
        
        #include <Arduino.h>
        //#include <ESP8266mDNS.h>
        #include <ESP8266WiFi.h>
        #include <MySensors.h>
        
        void setup();
        void loop();
        void present();
        void receive(const MyMessage &message);
        void ISR_PIRPinHasChanged();
        void SetRelayAndLedStatus();
        void ToggleRelay();
        void TransmitStatusToController();
        

        main.cpp

        #include "main.h"
        
        int currentRelayStatus;
        MyMessage msg(CHILD_ID,V_STATUS);
        
        void setup(){
          pinMode(PIN_LED, OUTPUT);
          pinMode(PIN_RELAY, OUTPUT);
          pinMode(PIN_PIR_SENSOR, INPUT);
        
          currentRelayStatus = RELAY_OFF;
          attachInterrupt(PIN_PIR_SENSOR, ISR_PIRPinHasChanged, RISING);
        
          //flash led
          digitalWrite(PIN_LED,LED_ON);
          delay(100);
          digitalWrite(PIN_LED,LED_OFF);
          delay(100);
          digitalWrite(PIN_LED,LED_OFF);
          delay(100);
          digitalWrite(PIN_LED,LED_OFF);
          delay(100);
        }
        
        void loop()
        {
        
        }
        
        void presentation()  {
          // Send the sketch version information
          sendSketchInfo("Sonoff PIR Switch", "1.1");
          // Register sensor
          present(CHILD_ID, S_BINARY);
          SetRelayAndLedStatus();
          TransmitStatusToController();
        }
        
        void receive(const MyMessage &message)
        {
          if (message.type==V_STATUS  && message.sensor==CHILD_ID) {
            if(message.getBool() == true)
            {
              currentRelayStatus = RELAY_ON;
            }
            else{
              currentRelayStatus = RELAY_OFF;
            }
            SetRelayAndLedStatus();
            TransmitStatusToController();
          }
        }
        
        
        void ISR_PIRPinHasChanged(){
          int currentPinStatus = digitalRead(PIN_PIR_SENSOR);
          if(currentPinStatus == LOW){
            Serial.println("ISR fired, pin LOW");
          }
          else{
            Serial.println("ISR fired, pin HIGH");
            ToggleRelay();
          }
        }
        
        void ToggleRelay(){
          if(currentRelayStatus == RELAY_ON){
            Serial.println("toggling relay to OFF");
            currentRelayStatus = RELAY_OFF;
          }
          else{
            Serial.println("toggling relay to ON");
            currentRelayStatus = RELAY_ON;
          }
          SetRelayAndLedStatus();
          TransmitStatusToController();
        }
        
        void SetRelayAndLedStatus(){
          if(currentRelayStatus == RELAY_ON){
            Serial.println("switching on");
            digitalWrite(PIN_LED, LED_ON);
            digitalWrite(PIN_RELAY, RELAY_ON);
          }
          else{
            Serial.println("switching off");
            digitalWrite(PIN_LED, LED_OFF);
            digitalWrite(PIN_RELAY, RELAY_OFF);
          }
        }
        
        void TransmitStatusToController(){
          Serial.println("sending status to controller");
          if(currentRelayStatus == RELAY_ON){
            send(msg.set(true));
          }
          else{
            send(msg.set(false));
          }
        }
        
        Y Offline
        Y Offline
        Yveaux
        Mod
        wrote on last edited by
        #3

        @pansen said in MQTT message does not show (MySensors on ESP8266):

        mosquitto_sub -h localhost -t Sonoff-Out

        Try subscribing to all topics starting with the Sonoff-Out prefix, using a wildcard, and see if that makes a difference:

        mosquitto_sub -h localhost -t Sonoff-Out/#
        

        http://yveaux.blogspot.nl

        1 Reply Last reply
        1
        • gohanG gohan

          Have you considered using easy esp or other similar firmware?

          P Offline
          P Offline
          pansen
          wrote on last edited by pansen
          #4

          @gohan said in MQTT message does not show (MySensors on ESP8266):

          Have you considered using easy esp or other similar firmware?

          Esay esp won't work for me since it depends on the Arduino IDE but I am using platform.io. Any incentives to change?

          @Yveaux said in MQTT message does not show (MySensors on ESP8266):

          @pansen said in MQTT message does not show (MySensors on ESP8266):

          mosquitto_sub -h localhost -t Sonoff-Out

          Try subscribing to all topics starting with the Sonoff-Out prefix, using a wildcard, and see if that makes a difference:

          mosquitto_sub -h localhost -t Sonoff-Out/#
          

          That was it, thanks!

          0
          1
          0
          1
          0
          1
          2.1.1
          Sonoff PIR Switch
          1.1
          0
          

          edit: and some verbose output for completeness:

          x@x:~$ mosquitto_sub -h localhost -v -t Sonoff-Out/#
          Sonoff-Out/0/73/1/0/2 1
          Sonoff-Out/0/73/1/0/2 0
          

          Orange Pi Plus 2e connected to nrf24 PA via SPI running git-development MySensors gateway, OpenHAB2, mosquitto and MySQL persistence.

          1 Reply Last reply
          0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          16

          Online

          12.0k

          Users

          11.2k

          Topics

          113.4k

          Posts


          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular