2.0.x allow configuration (old MyConfig stuff) to be done directly in the sketch using simple c #defines. You can still make modification in MyConfig if you want to override the default values once and for all.
It is important to do these defines before including MySensors.h, otherwise they will go unnoticed when the library evaluates which parts to include.
The most common thing you need to do is to decide which radio to use (or not to use). We currently support three different transport layers. Only activate ONE transport per node.
// Activate one of these
#define MY_RADIO_NRF24
#define MY_RADIO_RFM69
#define MY_RS485
In addition to the transport you can also allow the node to be a gateway (communicating to the controller). We currently offer a few variants here as well. Note that ESP8266 is always considered to be a gateway node (but it can still communicate with a sub-network of none-esp sensor nodes). If you want to enable gateway functionality enable one of these:
// Enable serial gateway
#define MY_GATEWAY_SERIAL
// Enable gateway ethernet for a ENC28J60 module
#define MY_GATEWAY_ENC28J60
// Enable gateway for a W5100 ethernet module
#define MY_GATEWAY_W5100
// Enable gateway for a ENC28J60 ethernet for a module
#define MY_GATEWAY_ENC28J60
// Enable a MQTT client gateway for W5100 or ESP node
#define MY_GATEWAY_MQTT_CLIENT
Each type of gateway has additional configuration available (ip settings etc). Please look at the prepared example for more details.
To upgrade a sketch old 1.5 sketch to 2.0 there is just a few steps to go through.
-
Remove the MySensors gw;
class initialization and gw.begin()
-call. It's no longer needed. Use #defines to set radio channels or other settings. They are all documented in MyConfig.h.
-
The library now calls process() automatically. So you must remove it from you main loop().
-
Remove the gw.
part before any library call. It's no longer needed as library functions is available in the main scoop. For instance gw.send(msg)
should just be send(msg)
.
-
The controller can now re-request presentation information from a node. This means we have to move all node presentation to a new special function presentation() {}
. This will be called at node startup or when controller requests a new presentation.
So if your old node had this:
void setup()
{
gw.sendSketchInfo("Distance Sensor", "1.0");
gw.present(CHILD_ID, S_DISTANCE);
}
Do like this instead:
void presentation()
{
sendSketchInfo("Distance Sensor", "1.0");
present(CHILD_ID, S_DISTANCE);
}
If you need to do initialization before the MySensors library starts up, define a
void before() {
// This will execute before MySensors starts up
}
You can still use setup() which is executed AFTER mysensors has been initialised.
void setup() {
}
To handle received messages, define the following function in your sketch
void receive(const MyMessage &message) {
// Handle incoming message
}
If your node requests time using requestTime()
. The following function is used to pick up the response.
void receiveTime(unsigned long ts) {
}
Here follows a small sample of the defines you can use in your sketch
#define MY_DEBUG // Enables debug messages in the serial log
#define MY_REPEATER_FEATURE // Enables repeater functionality for a radio node
#define MY_BAUD_RATE 9600 // Sets the serial baud rate for console and serial gateway
#define MY_NODE_ID 42 // Sets a static id for a node
#define MY_PARENT_NODE_ID xx // Sets a static parent node id
#define MY_OTA_FIRMWARE_FEATURE // Enables OTA firmware updates
#define MY_RF24_CE_PIN 9 // Radio specific settings for RF24
#define MY_RF24_CS_PIN 10 // Radio specific settings for RF24 (you'll find similar config for RFM69)
#define MY_INCLUSION_MODE_FEATURE // Enables inclusion mode (for a gateway)
#define MY_INCLUSION_BUTTON_FEATURE // Eables inclusion mode button (for a gateway)
#define MY_LEDS_BLINKING_FEATURE // Enables transmission led feature for a node or gateway
#define MY_SIGNING_ATSHA204 // Enables hardware signing using ATSHA204
#define MY_SIGNING_SOFT // Enables software signing
Security related settings (HMAC and AES keys among other things) are now configured using the SecurityPersonalizer sketch. Secrets are stored in eeprom for all software based security features (including rfm69 encryption).
There are many more things you can tweak using defines and we've tried to include most of them in relevant examples. The MyConfig.h/keywords.txt shows you the full list of defines available.