How to deal with a request for information from controller?
-
You are right, the idea (with REQ comands) was to let node requests response with a SET-command back to the requester. The examples does not have this functionality implemented. And as you might have suspected it was mainly thought to be used between sensors. But I haven't really had any use for it myself in my setup (easier to just push messages that do the request-setup).
But there are use-cases, especially for sleeping nodes that can wake up and request data and go back to sleep when they have received their response.
-
Hi, is there any way in which I can identify the message type in my incomingMessage function?
void incomingMessage(const MyMessage &message) { **//I need to identify firstly the command type (REQ, SET,PRESENTATION,etc)** // if(message.type == C_REQ) if (message.type == V_LIGHT) // this would be the subtype { if ((message.sensor - StartingChildID) < noZones) { Relays[message.sensor - StartingChildID].relayState = message.getBool(); } } }I need to be able to request values at will from nodes. Mainly for debug purposes.
Thanks. Regards
-
Shouldn't be nice to have this feature directly emebebed in Mysensors library (as the ack request feature)?
I mean, if I send a status request for a sensor through the controller, to ahve the library automaticcaly answer this type of messages. -
@hek Why there is no method getCommand() in MyMessage object like isAck()?
-
@hek Why there is no method getCommand() in MyMessage object like isAck()?
-
Is there any chance to create it for next version?
-
Is there any chance to create it for next version?
-
Well, it seems nobody has either the motivation or the answer itself... Wonder if I should have posted this question in the Development forum instead...
I've been researching and doing some trial and error since I posted the question, and it seems I found a way of doing it. Not sure if this is the correct way because I just did not find any documentation about it, but I'll share my discoveries anyway since it can help others.It seems the ack part is done automatically by the library itself. I've been able to see ack messages returning from the gateway to the controller without doing any treatment myself in my sketch.
Regarding how to differentiate "req" messages sent from the controller to a node, I've been able to detect if a message is a "req" one by reading the value of mGetCommand(message) in my sketch. If this is equal to 2, then it is a "req" message, if it is equal to 1, then it is a "set" message.
void incomingMessage(const MyMessage &message) { //debug only //Serial.print("message.destination="); //Serial.println(message.destination); //Serial.print("message.type="); //Serial.println(message.type); //Serial.print("message.sensor"); //Serial.println(message.sensor); int comando = mGetCommand(message); //Serial.print("comando="); //Serial.println(comando); if (message.type==V_LIGHT) { //comando=2 means information has been requested from controller, so we will send it back. //comando=1 means a new status has been received from controller to the node, so we will act as requested. if (comando==2){ gw.send(lightMsg.set(digitalRead(lightPin))); } if (comando==1){ // Change relay state setLightState(message.getBool(), false); // Write some debug info Serial.print("Incoming light status:"); Serial.println(message.getBool()); } } if (message.type==V_VAR1) { // Toggle program mode if (programModeActive == false){ // If we're not in programming mode, turn it on. programmingmode(1); } else { // If we are in programing mode, turn it off. programmingmode(0); } // Write some debug info Serial.print("Toggled program mode:"); Serial.println(message.getBool()); } }And below are the tests that show what I mentioned.
Sent this, which sets the light child 99 on node 51 to 1 (on), requesting an ack
51;99;1;1;2;10;0;3;0;14;Gateway startup complete. 0;0;3;0;9;send: 0-0-51-51 s=99,c=1,t=2,pt=0,l=1,st=ok:1 0;0;3;0;9;read: 51-51-0 s=99,c=1,t=2,pt=0,l=1:1 51;99;1;1;2;1The last one is the ack itself that returned from the gateway to the controller.
Then sent this, which requests the status of light child 99 on node 51, requesting an ack. Should return the ack and the response containing the current status of the light (1).
51;99;2;1;2;anything0;0;3;0;9;send: 0-0-51-51 s=99,c=2,t=2,pt=0,l=8,st=ok:anything 0;0;3;0;9;read: 51-51-0 s=99,c=2,t=2,pt=0,l=8:anything 51;99;2;1;2;anything 0;0;3;0;9;read: 51-51-0 s=99,c=1,t=2,pt=2,l=2:1 51;99;1;0;2;1This "51;99;2;1;2;anything" is the ack generated automatically by the library, and this "51;99;1;0;2;1" is the response that my program performs. Correct!
Then sent this, which sets the light child 99 on node 51 to 0 (off), requesting an ack
51;99;1;1;2;00;0;3;0;9;send: 0-0-51-51 s=99,c=1,t=2,pt=0,l=1,st=ok:0 0;0;3;0;9;read: 51-51-0 s=99,c=1,t=2,pt=0,l=1:0 51;99;1;1;2;0The last one above is the ack generated automatically by the library.
Then sent this, which requests the status of light child 99 on node 51, requesting an ack. Should return the ack and the response containing the current status of the light (0).
51;99;2;1;2;anything0;0;3;0;9;send: 0-0-51-51 s=99,c=2,t=2,pt=0,l=8,st=ok:anything 0;0;3;0;9;read: 51-51-0 s=99,c=2,t=2,pt=0,l=8:anything 51;99;2;1;2;anything 0;0;3;0;9;read: 51-51-0 s=99,c=1,t=2,pt=2,l=2:0 51;99;1;0;2;0This "51;99;2;1;2;anything" is the ack generated automatically by the library, and this "51;99;1;0;2;0" is the response that my program performs. Correct!
@marcusvdt said:
lightMsg
Hello
I wanted the same functionality and during forum found your post.
Can you please post your code as attached code block has missing functions e.g lightMsg.set... SetLightState(..Thanks
Ratnanabh -
I would like to share my sketch for 2.0.0-beta. Its a node with two relays and the ability to read the status from mqtt.
// Relais Node for MySensors 2.0.0-beta // I am using the filename and compile date for sktech announcements: #include <string.h> // Strip the path info from __FILE__ // For Windows use '\\' instead of '/'. #define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) // #define MY_DEBUG #define MY_RADIO_NRF24 #define SKETCH_NAME __FILENAME__ #define SKETCH_DATE __DATE__ #define MY_NODE_ID 2 #define MY_PARENT_NODE_ID 0 #define MY_REPEATER_FEATURE false #include <SPI.h> #include <MySensor.h> #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay void before() { for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF); } } void setup() { } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo(SKETCH_NAME, SKETCH_DATE); for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_LIGHT); } } void loop() { } void receive(const MyMessage &message) { int msgcmd = mGetCommand(message); // Verify its a switch command (V_LIGHT) if (message.type==V_LIGHT) { // Command type 1 is a set command if (msgcmd==1){ // Change relay state digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom saveState(message.sensor, message.getBool()); } // Command type 2 is a request for the sensor state if (msgcmd==2){ // construct message body with requested sensor-id and type V_LIGHT (= switch) MyMessage msg(message.sensor,V_LIGHT); // Example: requested sensor is 2 (message.sensor). // The programm reads the digital pin 4, calculated by 2 (sensor) - 1 (static) + 3 (first relay pin) // after that, the reading is compared to the RELAY_ON value (might be invers logic, regarding to your relais schematic) // if it is matching, the payload is set to 1, otherwise its set to 0 send(msg.set(digitalRead(message.sensor-1+RELAY_1) == RELAY_ON ? 1 : 0)); } } }and here are the lines in my openhab2 items.item:
Switch node2_sw1 "N2SW1" (all,node2) { mqtt=">[mosquitto:mysensors-in/2/1/1/0/2:command:ON:1],>[mosquitto:mysensors-in/2/1/1/0/2:command:OFF:0],<[mosquitto:mysensors-out/2/1/2/0/2:state:default]" } Switch node2_sw2 "N2SW2" (all,node2) { mqtt=">[mosquitto:mysensors-in/2/2/1/0/2:command:ON:1],>[mosquitto:mysensors-in/2/2/1/0/2:command:OFF:0],<[mosquitto:mysensors-out/2/2/2/0/2:state:default]" }I would like to repeat: Its for the last versions of mysensors and openhab2. Notice the absence of any "gw." and the different structure in the mqtt strings !
Can someone tell me, if it is a problem to redefine the "MyMessage msg(message.sensor,V_LIGHT); " every time a request is done? I am not sure.
Greetings
-
I would like to share my sketch for 2.0.0-beta. Its a node with two relays and the ability to read the status from mqtt.
// Relais Node for MySensors 2.0.0-beta // I am using the filename and compile date for sktech announcements: #include <string.h> // Strip the path info from __FILE__ // For Windows use '\\' instead of '/'. #define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) // #define MY_DEBUG #define MY_RADIO_NRF24 #define SKETCH_NAME __FILENAME__ #define SKETCH_DATE __DATE__ #define MY_NODE_ID 2 #define MY_PARENT_NODE_ID 0 #define MY_REPEATER_FEATURE false #include <SPI.h> #include <MySensor.h> #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay void before() { for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF); } } void setup() { } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo(SKETCH_NAME, SKETCH_DATE); for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_LIGHT); } } void loop() { } void receive(const MyMessage &message) { int msgcmd = mGetCommand(message); // Verify its a switch command (V_LIGHT) if (message.type==V_LIGHT) { // Command type 1 is a set command if (msgcmd==1){ // Change relay state digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom saveState(message.sensor, message.getBool()); } // Command type 2 is a request for the sensor state if (msgcmd==2){ // construct message body with requested sensor-id and type V_LIGHT (= switch) MyMessage msg(message.sensor,V_LIGHT); // Example: requested sensor is 2 (message.sensor). // The programm reads the digital pin 4, calculated by 2 (sensor) - 1 (static) + 3 (first relay pin) // after that, the reading is compared to the RELAY_ON value (might be invers logic, regarding to your relais schematic) // if it is matching, the payload is set to 1, otherwise its set to 0 send(msg.set(digitalRead(message.sensor-1+RELAY_1) == RELAY_ON ? 1 : 0)); } } }and here are the lines in my openhab2 items.item:
Switch node2_sw1 "N2SW1" (all,node2) { mqtt=">[mosquitto:mysensors-in/2/1/1/0/2:command:ON:1],>[mosquitto:mysensors-in/2/1/1/0/2:command:OFF:0],<[mosquitto:mysensors-out/2/1/2/0/2:state:default]" } Switch node2_sw2 "N2SW2" (all,node2) { mqtt=">[mosquitto:mysensors-in/2/2/1/0/2:command:ON:1],>[mosquitto:mysensors-in/2/2/1/0/2:command:OFF:0],<[mosquitto:mysensors-out/2/2/2/0/2:state:default]" }I would like to repeat: Its for the last versions of mysensors and openhab2. Notice the absence of any "gw." and the different structure in the mqtt strings !
Can someone tell me, if it is a problem to redefine the "MyMessage msg(message.sensor,V_LIGHT); " every time a request is done? I am not sure.
Greetings
I dont see openHAB requesting any state, so maybe its a better idea to read the state of the output pin immediately after setting the relay and publish this and bind it to a control lamp. If this isnt toggling, the command wasnt received.