@SuperKris
Sorry for the lingo.
From your reply I think you've understood very well so far.
If you want to run on batteries you should sleep as much as possible. So then you should add a call to the sleep method, i.e. gw.sleep([TIME IN MILLISECONDS])
. Read more about that in the API. But the problem is then, that you can't receive messages from the controller while sleeping. There are possible workarounds, like waking up every now and then to let the node check for messages from the controller. But in the 1.5 releases you have to write all code for that yourself, so I wouldn't recommend it for a beginner. In the dev branch version (2.0), which is under continuous development, there is support for smart sleep, which simplifies the process of sleeping, waking and checking for controller messages before sleeping again. The controller needs to support this though.
Anyway, if you can, it's best to start with a non battery driven node, if you expect messages from the controller after setup. That way you don't need to sleep, and can call gw.process each lap in the loop, like you said.
The arduino node knows all possible message types from the mysensors library that is imported in the beginning of the sketch. message.type
is the type attribute of the message argument that gets passed into the function. Sorry for the lingo again. Each message that is received will have a message subtype. This is part of the mysensors message protocol. When the message comes in to the node, the incomingMessage function will be called, from the gw.process method, and the message object which holds different attributes of the message is passed into the incomingMessage function as an argument. The argument is the word inside the parenthesis after the function name, simply put, in this case message
.
In the end of the library API page on the site, you will find info about message manipulation. Among other things there are "getters" for getting the different attributes of the message object. For example message.getBool()
.
You should add
message.getBool()?HIGH:LOW
instead of just HIGH
to your modified incomingMessage function. The BOOL?HIGH:LOW
part is a short form of making an IF
statement. That way you can send a 1 as payload to switch the motor pin HIGH and a 0 to switch it to LOW.
How have you tested this? Have you tried sending a message to the node of subtype V_LIGHT with a payload of either "1" or "0"? If you're using a controller it probably supports this if you present the correct sensor/actuator type. Hook up your node to a computer with USB / serial connection and start the serial monitor in the Arduino IDE. Then you can watch the messages from/to the node.
Also test just switching the motor pin HIGH/LOW directly in the loop with a wait or sleep of some seconds in between, so you know the pin is acting properly. Of course you need a readout for the pin somehow. You can use a LED and a resistor if you don't want to connect anything larger while testing.
Edit: Don't forget to add the name of the incoming message function as first argument to the begin method. Read about initializing the library in the API on the site, section "Starting up the library".