<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] MQTT message does not show (MySensors on ESP8266)]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">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.</p>
<p dir="auto">Now, when I subscribe to the topic I set, nothing is shown when my relay state changes:</p>
<pre><code>mosquitto_sub -h localhost -t Sonoff-Out
</code></pre>
<p dir="auto">I am sure I am just missing some configuration or a command...Any ideas?</p>
<p dir="auto">main.h</p>
<pre><code>#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 &lt;WiFiUdp.h&gt;
#endif

#include &lt;Arduino.h&gt;
//#include &lt;ESP8266mDNS.h&gt;
#include &lt;ESP8266WiFi.h&gt;
#include &lt;MySensors.h&gt;

void setup();
void loop();
void present();
void receive(const MyMessage &amp;message);
void ISR_PIRPinHasChanged();
void SetRelayAndLedStatus();
void ToggleRelay();
void TransmitStatusToController();
</code></pre>
<p dir="auto">main.cpp</p>
<pre><code>#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 &amp;message)
{
  if (message.type==V_STATUS  &amp;&amp; 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));
  }
}
</code></pre>
]]></description><link>https://forum.mysensors.org/topic/6490/solved-mqtt-message-does-not-show-mysensors-on-esp8266</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 01:25:49 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/6490.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 18 Mar 2017 13:26:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] MQTT message does not show (MySensors on ESP8266) on Sun, 19 Mar 2017 11:19:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/gohan" aria-label="Profile: gohan">@<bdi>gohan</bdi></a> said in <a href="/post/62680">MQTT message does not show (MySensors on ESP8266)</a>:</p>
<blockquote>
<p dir="auto">Have you considered using easy esp or other similar firmware?</p>
</blockquote>
<p dir="auto">Esay esp won't work for me since it depends on the Arduino IDE but I am using <a href="http://platform.io" rel="nofollow ugc">platform.io</a>. Any incentives to change?</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/yveaux" aria-label="Profile: Yveaux">@<bdi>Yveaux</bdi></a> said in <a href="/post/62686">MQTT message does not show (MySensors on ESP8266)</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pansen" aria-label="Profile: pansen">@<bdi>pansen</bdi></a> said in <a href="/post/62643">MQTT message does not show (MySensors on ESP8266)</a>:</p>
<blockquote>
<p dir="auto">mosquitto_sub -h localhost -t Sonoff-Out</p>
</blockquote>
<p dir="auto">Try subscribing to all topics starting with the Sonoff-Out prefix, using a wildcard, and see if that makes a difference:</p>
<pre><code>mosquitto_sub -h localhost -t Sonoff-Out/#
</code></pre>
</blockquote>
<p dir="auto">That was it, thanks!</p>
<pre><code>0
1
0
1
0
1
2.1.1
Sonoff PIR Switch
1.1
0
</code></pre>
<p dir="auto">edit: and some verbose output for completeness:</p>
<pre><code>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
</code></pre>
]]></description><link>https://forum.mysensors.org/post/62753</link><guid isPermaLink="true">https://forum.mysensors.org/post/62753</guid><dc:creator><![CDATA[pansen]]></dc:creator><pubDate>Sun, 19 Mar 2017 11:19:36 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] MQTT message does not show (MySensors on ESP8266) on Sat, 18 Mar 2017 18:49:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pansen" aria-label="Profile: pansen">@<bdi>pansen</bdi></a> said in <a href="/post/62643">MQTT message does not show (MySensors on ESP8266)</a>:</p>
<blockquote>
<p dir="auto">mosquitto_sub -h localhost -t Sonoff-Out</p>
</blockquote>
<p dir="auto">Try subscribing to all topics starting with the Sonoff-Out prefix, using a wildcard, and see if that makes a difference:</p>
<pre><code>mosquitto_sub -h localhost -t Sonoff-Out/#
</code></pre>
]]></description><link>https://forum.mysensors.org/post/62686</link><guid isPermaLink="true">https://forum.mysensors.org/post/62686</guid><dc:creator><![CDATA[Yveaux]]></dc:creator><pubDate>Sat, 18 Mar 2017 18:49:22 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] MQTT message does not show (MySensors on ESP8266) on Sat, 18 Mar 2017 18:22:56 GMT]]></title><description><![CDATA[<p dir="auto">Have you considered using easy esp or other similar firmware?</p>
]]></description><link>https://forum.mysensors.org/post/62680</link><guid isPermaLink="true">https://forum.mysensors.org/post/62680</guid><dc:creator><![CDATA[gohan]]></dc:creator><pubDate>Sat, 18 Mar 2017 18:22:56 GMT</pubDate></item></channel></rss>