[SOLVED] MQTT message does not show (MySensors on ESP8266)
-
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)); } }
-
Have you considered using easy esp or other similar firmware?
-
@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/#
-
@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