How to determine from which Node a message is comming from

  • Contest Winner

    First of all, not sure whether this is the right topic for my question, so if not I apologize.

    I've been experimenting with node to node communication inspired by @GertSanders his presentation during the Dutch MySensors meetup. I haven't discovered this feature of MySensors before, but it's brilliant.

    I can communicate from one sensor to another without the interference of any controller. Love it! It opens new possibilities, and probably keep me awake for a couple of nights 😉

    So I'd like to know, if there's a way to determine the sender of a message in the incomming message handler. I do see a setDestination() method but - at least in 1.5 - there's no getter and it seems like MySensors ignores that. For my use case it would be great if I can determine what the id is of the node of which the message is originating from.

    I also tried to create a message with the default constructor like this:

      MyMessage myMsg(); 
      myMsg.setDestination( aNodeId );

    but that results into the following compile error:

    error: request for member 'setDestination' in 'myMsg', which is of non-class type 'MyMessage()'
       myMsg.setDestination( aNodeId );

    I can get around that easily, so I think I misunderstood the API documentation for that.

    void sendMessage( uint8_t aNodeId, char *aMsg ) {
      MyMessage myMsg( 255, V_VAR1 );
      myMsg.setDestination( aNodeId );
      myMsg.set( aMsg );
      gw.send( myMsg, true );

  • Mod

    @TheoL this code:

    void receive(const MyMessage &message) {
      LOG(F("Received %s from %s\n"), msgTypeAsCharRepresentation((mysensor_data)message.type), nodeTypeAsCharRepresentation(message.sender));
      sendPingOrPongResponse( message );  

    from the PingPong example suggests that the sender is available.

    Is that a solution to your problem or did I not quite understand your question?

  • Contest Winner

    @mfalkvidd Thanx for the quick reply and the example. It's exactly what I was I was looking for. I just overlooked the union definition in Message.h.

    I'm currently sending the message to the Node itself as a test. But event that that works.