Do I have an error or am I mistaken?



  • Greetings,

    A simple variation of the following code (with the for statement in the void loop and no incomingMessage statements) transmits the analog output of A0 through A7 to my gateway - repeatedly! I am trying to make this happen only when the gateway requests data. I am not presenting sensors, as I am only interested in the payload and the node ID (1, in this example).

    My question is . . . do I have this right (below)? If I do, would someone be so kind as to suggest the gateway code that will request that this node "dump" its message data to the Gateway? I have tried 1;255;2;0;17/n, and similar variations, to no avail If I have this wrong, what might I be missing to respond to a message from the gateway. As a side note, since this routine sends A0 - A7 in succession, I don't have a specific sensor ID to respond. Should the second number in my gateway code be 255 for non-descript sensor requests, and finally, do I need a subtype if I am not filtering for one?

    Many thanks for any constructive input (remembering I am in C++ kindergarten) - Baran

    #include <SPI.h>
    #include <MySensor.h>
    int AnalogSensorPin=0;
    int AnalogLevel;
    #define CHILD_ID_POWER 0

    MySensor gw;
    MyMessage msg(CHILD_ID_POWER, V_WATT);

    void setup()
    {
    gw.begin(incomingMessage, AUTO, true);
    // Register all sensors to gateway (they will be created as child devices)
    gw.present(CHILD_ID_POWER, S_POWER);
    }

    void loop()
    {
    // Alway process incoming messages whenever possible
    gw.process();
    }

    void incomingMessage(const MyMessage &message) {
    for (AnalogSensorPin = 0; AnalogSensorPin < 8; AnalogSensorPin ++){
    AnalogLevel = (analogRead(AnalogSensorPin));
    Serial.print(" DC Current from String Number");
    Serial.print (AnalogSensorPin+1); // This prints on the sensor side, not the gateway side!
    Serial.print (" ");
    Serial.println(AnalogLevel);
    gw.send(msg.set(AnalogLevel));
    }
    }



  • My reading of that main loop is that every time it runs the loop, it will create the message and send it. I think you need an IF statement in there to say "If I receive this message, THEN process the packet and send, ELSE do nothing".


  • Admin

    (format your code please)

    It looks ok. incomingMessage should be triggered every time your node gets a message.

    But avoid sending internal messages because they are handled by the library and is normally not passed to incomingMessage.

    You should be able to send almost anything to the node. For example (node id 42 below):
    42;1;1;0;1;\n
    Should do the trick

    How do you send your messages?



  • Greetings Hek,

    I am never sending messages to my nodes, only requests for data from the analog sensors. As I am already receiving analog inputs 0 through 7, I have no need to present eight different sensors. What I am unable to get right is the message that I would send to a node (like the script above) that would cause it to unleash one round of sensor data. I know the first number is the node, itself. The second number is the sensor but that is weird here because all sensors are read in one shot. Should I simply call it sensor 0 to match the CHILD_ID_POWER? I know the third number is a request if it is a "2" and the fourth can be a "0" for no ack. As I call MyMessage msg with V_WATT, the fifth number is "17" so, in my mind (and nowhere else because it does not work;), the code my gateway should send to node 1 to request data is 1,0,2,0,17/n. I am wrong, of course, because it doesn't work. If anyone could set me straight, I would deeply appreciate it. Also, thanks to Chester for the IF statement advice.. As a PS, my nodes communicate well and my gateway notices when my node is activated.


Log in to reply
 

Suggested Topics

  • 3
  • 1
  • 5
  • 1
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts