The Magic "void receive(const MyMessage & message)"
RWoerz last edited by
Let me start by saying I'm NOT a programmer so if you find something wrong here please let me know. I'm just the hardware guy which in the before time was magnitudes harder than it is today. I'm not dead yet but I'm also not young. So if I missed this information somewher, sorry.
I think it's Magic BUT but I'll talk about that later. There seems to be a lot of outdate information about node to node data transfer. I'll share what I know and then ask some questions. A lot about how this works has changed with newer versions of the MySensors software which makes most of the examples I found most confusing and time consuming.
I would like to thank the person that had this at the bottom of one of their sketchs as it answered a multitude of questions (but not all). I also found it invaluable for trouble shooting. I just need a longer list.
On the receiving end somewhere after void loop.
void receive(const MyMessage & message)
Serial.println("received something: ");
Serial.print("message.sender= "); // Node ID of the Sender
Serial.print("message.type= "); //Message type, the number assigned
Serial.println(message.type); // V_TEMP is 0 zero. etc.
Serial.print("message.sensor= "); // Child ID of the Sensor/Device
Serial.print("message.payload= "); // This is where the wheels fall off
Serial.println(message.getFloat()); // This works great!
Serial.println(message.getBool()); // I think this does too
For me it showed the variable names used to dissect the serial protocal, the orignal was a little differant but I think it was a bit outdated. The big differance was how to read float values that are in the payload, I got the "get.float()" somewhere else. It works perfect if you want to read stuff like temps and humidities but not so great for intergers and strings so far. But being able to take the whole packet apart would be good to. I'm sure there's more. The "command" would also help.
So my question is, where is the definitive list of these vairable names. I'm currently trying to send integers, I tried message.getInt() without any luck. The compiler didn't complain but it also didn't work. I think I saw somewhere how to read the "ACK" but that will have to wait for another day.
After a lot of reading and trial and error it turns out to be pretty easy to send data from one node to another and I don't think you even need a gateway? You need three things when you want to send something from one Node another Node. Important Safety Note: I'm pretty sure at least on your recieving Node the repeater feature has to be enabled, NO going to sleep!
Second on your transmitting end you'll need to add the destination Node ID as the second vairable in your send command.
send(msg.setDestination(recieving node ID).set(sensor ID).set(vairable))
This could just be me but a strange thing happened after sending the first message with the new destination, the system set that as its default. So if you wanted it to also go to the the GW you'll have to add a
setDestination( GW_Node_ID) or "0" zero to all of your "normal" send commands.
And third on your receiving end you'll need the function: void receive(const MyMessage & message) line followed commands with what you want to do with the data and I've seen some complex ones.
The reason I called it Magic was because I have no clue how it works. As soon as you have the "void receive funtion" added to your scketch the data comes flooding in. If you have debug enabled you'll see packets coming in as soon as #include <MySensors.h> gets executed, it's totaly asynchronous. I've not run into it yet but be careful not to overrun your recieving end.
So if someone can fill in the list for us we would all be a little smarter.
I hope this was helpful to some, I wish I'd read it months ago writen by some one else. Maybe then my family wouldn't have threatened to put me on the cart. Sorry old movie reference for those that have never seen the funnist movie ever made.
If you get this far thanks for reading my rantings.
Since you are still beginning in the programming world, I'd suggest you get a better development environment like visual studio community edition with visual micro plug in (the plug in enables the use of arduino, just don't hit the "start trial" button when it start and just press cancel and it will work) or Eclipse with the Sloeber plug in: with these you get autocomplete if functions names, variables, methods and so on, it helps a lot when you don't remember some names
@rwoerz yes, the receive function used rhe same magic as setup and loop - they get called when they should and you don’t need to worry about it.
The receiving node does not need to be a repeater, but no node can receive messages while being asleep.
I think the documentation you are looking for is available at https://www.mysensors.org/download/sensor_api_20 in the ”Handling incoming radio messages” and ”Message manipulation” sections.
@gohan not to derail this thread, but how did you get autocomplete to work? I spent about an hour yesterday in vscode without getting the intellisense to work. It is unable to find standard things like digitalWrite, and unable to find the MySensors functions, even though I have indtalled Microsoft’s Arduino plugin and the MySensors library.
I am using the Visual Studio Community with Visual Micro plug in, I don't know about the VS Code
RWoerz last edited by
Gohan thanks for the link https://www.mysensors.org/download/sensor_api_20. I’ve been on that page many times so not sure how I missed it. That said and the fact I'm not a programmer some working examples for each would be most useful. I wrote tons of 8080 assembly code back in the day but my only official programming class was Fortran (you know the before time). C++ has not been an easy language to learn.
I’ve been looking for a replacement to the Arduino IDE for the past year. Every time I install one the steep learning curve keeps me from getting work done so I end up back where I started. I had much better tools back in the 70's working in assembly. At well over 1000 line of code and 21 tabs in just my Mega sketch I can use all the help I can get.
To be honest I’m about to give up on the whole Arduino thing so this project may be my last using this old 8 bit technology. Will Visual Studio Community Edition work with the newer micro controllers and maybe Python? Only my 5th or 6th programing language I'll have to learn.
Thanks again for the help. I’ll try and figure out why the getInt() isn’t working for me.
Visual studio can be extended with plug-ins like many others IDE. You need to take your time and find the IDE that suits you the best. I like Visual Studio because I used it for a few years and I'm used to it, but there may be some other solutions that work better with the projects you want to develop. I know the new IDE have tons of functionalities and I still haven't looked at the new ones, mostly because of lack of time.
Sergio Rius last edited by
@gohan +1 on vs+vsmicro.
I'm a be developer from is first version and I could only seriously start programming in Arduino when I found the plug-in.
Last week, also stumped into other free ide that reuses the vs distributable, while trying to get mysbootloader to compile. It seems a nice stand alone solution. I can't remember is name but I think visual micro comes from it.
Sergio Rius last edited by
@mfalkvidd Look in the ide for language inspection errors.
Sometimes while crawling the sources vs decides you have some invalid code files or dependencies, then it changes the file types to avoid live dependency resolution loop.
Then you loose auto completion, error detection, navigation, etc.
Specially if you use resharper plug-in.
Libraries like mysensors confuses the ide.