Is it possible to request sensor status (via MQTT)?



  • Hi,

    I'm testing a dual-SSR node connected to 220v (therefore, never sleeping).
    The node data is the following:

    Node ID: 80
    Childs:
    * HTU21 TEMP: 20
    * HTU21 HUM: 40
    * SSR1: 10
    * SSR2: 11
    

    Following the MySensors API structure

    Node-ID   /   Child-ID   /  Command   /  Ack   / Type  / Payload
    

    I believe I'm able to set a relay via MQTT with the command line command:

    mosquitto_pub -d -h 192.168.2.180 -u myuser -P mypass -t mysensors-in/80/10/1/1/2 -m "0"
    

    and get (what I believe its the confirmation):

    pi@raspberrypi:~ $ mosquitto_sub -h 192.168.2.180 -p 1883 -u myuser -P mypass -t mysensors-out/80/# -d
    Client mosqsub|5920-raspberryp sending CONNECT
    Client mosqsub|5920-raspberryp received CONNACK (0)
    Client mosqsub|5920-raspberryp sending SUBSCRIBE (Mid: 1, Topic: mysensors-out/80/#, QoS: 0)
    Client mosqsub|5920-raspberryp received SUBACK
    Subscribed (mid: 1): 0
    
    
    Client mosqsub|5920-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/80/10/1/1/2', ... (1 bytes))
    0
    

    I don't have at the moment anything connected to the relay to confirm if its switching or not, so I was trying to use the "request" command to request the child statuses.

    Something like:

    mosquitto_pub -d -h 192.168.2.180 -u myuser -P mypass -t mysensors-in/80/10/2/1/2 -m ""
    
    

    Even trying to request temperature statuses:

    pi@raspberrypi:~/mysensors-back $ mosquitto_pub -d -h 192.168.2.180 -u myuser -P mypass -t mysensors-in/80/20/2/1/0 -m ""
    
    

    but I'm not getting anything:

    Client mosqsub|6459-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/80/20/2/1/0', ... (0 bytes))
    Client mosqsub|6459-raspberryp received PUBLISH (d0, q0, r0, m0, 'mysensors-out/80/10/2/1/2', ... (0 bytes))
    

    I've read in the API documentation that the "request" command is "usually from an actuator destined for controller" so I'm wondering if it would be just "usually" or definitely "uniquelly" 😊 . Would there be any way of "asking" the sensors their statuses (via MQTT)?

    Cheers



  • "Usually" is the correct term. You can request whatever you want using MQTT, but you need to handle the response manually inside the receive() function. This isn't an automatic internal process like the echo or ack response.

    void receive(const MyMessage &message)
    {
    	if (message.getCommand() == C_REQ)
    	{
    		switch (message.getSensor())
    		{
    		case 10:
    			// Send SSR 1 state
    			break;
    		case 11:
    			// Send SSR 2 state
    			break;
    		case 20:
    			// Send temperature sensor value
    			break;
    		case 40:
    			// Send humidity sensor value
    			break;
    		default:
    			break;
    		}
    	}
    }
    

    Note: It's best to not send a message from within receive() as it can cause all sorts of weird stuff, including recursive loops. Preferably, you'd set a flag and handle everything else in the main loop.


Log in to reply
 

Suggested Topics

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts