MySensors Library & Examples  2.3.2-36-g2e00bf6
SecureActuator.ino

This example implements a secure actuator in the form of a IO controlled electrical lock.
Multiple locks are supported as long as they are on subsequent IO pin indices. The first lock pin is defined by LOCK_1. The number of locks is controlled by NOF_LOCKS .
The sketch will require incoming messages to be signed and the use of signing backend is selected by MY_SIGNING_ATSHA204 or MY_SIGNING_SOFT. Hard or soft ATSHA204 signing is supported.
Whitelisting can be enabled through MY_SIGNING_NODE_WHITELISTING in which case a single entry is provided in this example which typically should map to the gateway of the network.

/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <[email protected]>
* Copyright (C) 2013-2020 Sensnology AB
* Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*/
#define MY_DEBUG
#define MY_DEBUG_VERBOSE_SIGNING
#define MY_NODE_LOCK_FEATURE
// Enable and select radio type attached
#define MY_RADIO_RF24
//#define MY_RADIO_NRF5_ESB
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95
// Select soft/hardware signing method
#define MY_SIGNING_SOFT
//#define MY_SIGNING_ATSHA204
// Enable node whitelisting
//#define MY_SIGNING_NODE_WHITELISTING {{.nodeId = GATEWAY_ADDRESS,.serial = {0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01}}}
// Enable this if you want destination node to sign all messages sent to this node.
#define MY_SIGNING_REQUEST_SIGNATURES
// SETTINGS FOR MY_SIGNING_SOFT
#define MY_SIGNING_SOFT_RANDOMSEED_PIN 7
// SETTINGS FOR MY_SIGNING_ATSHA204
#ifndef MY_SIGNING_ATSHA204_PIN
#define MY_SIGNING_ATSHA204_PIN 17
#endif
#include <MySensors.h>
#define LOCK_1 3
#define NOF_LOCKS 1
#define LOCK_LOCK 1
#define LOCK_UNLOCK 0
void setup()
{
for (int lock=1, pin=LOCK_1; lock<=NOF_LOCKS; lock++, pin++) {
// Set lock pins in output mode
pinMode(pin, OUTPUT);
// Set lock to last known state (using eeprom storage)
digitalWrite(pin, loadState(lock)?LOCK_LOCK:LOCK_UNLOCK);
}
}
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Secure Lock", "1.0");
// Fetch lock status
for (int lock=1, pin=LOCK_1; lock<=NOF_LOCKS; lock++, pin++) {
// Register all locks to gw (they will be created as child devices)
present(lock, S_LOCK, "SecureActuator", false);
}
}
void loop()
{
}
void receive(const MyMessage &message)
{
// We only expect one type of message from controller. But we better check anyway.
// And echoed messages are not accepted as control messages
if (message.getType()==V_LOCK_STATUS && message.getSensor()<=NOF_LOCKS && !message.isEcho()) {
// Change relay state
digitalWrite(message.getSensor()-1+LOCK_1, message.getBool()?LOCK_LOCK:LOCK_UNLOCK);
// Store state in eeprom
saveState(message.getSensor(), message.getBool());
// Write some debug info
Serial.print("Incoming change for lock:");
Serial.print(message.getSensor());
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
MyMessage::getSensor
uint8_t getSensor(void) const
Get sensor ID of message.
receive
void receive(const MyMessage &message)
Incoming message handler.
Definition: SecureActuator.ino:112
loop
void loop()
Sketch execution code.
Definition: SecureActuator.ino:103
loadState
uint8_t loadState(const uint8_t pos)
NOF_LOCKS
#define NOF_LOCKS
Total number of attached locks.
Definition: SecureActuator.ino:76
LOCK_1
#define LOCK_1
Arduino Digital I/O pin number for first lock (second on pin+1 etc)
Definition: SecureActuator.ino:75
presentation
void presentation()
Node presentation.
Definition: SecureActuator.ino:90
saveState
void saveState(const uint8_t pos, const uint8_t value)
MyMessage::getType
uint8_t getType(void) const
Get message type.
present
bool present(const uint8_t sensorId, const mysensors_sensor_t sensorType, const char *description="", const bool requestEcho=false)
setup
void setup()
Called after node initialises but before main loop.
Definition: SecureActuator.ino:80
MySensors.h
API declaration for MySensors.
MyMessage::isEcho
bool isEcho(void) const
Getter for echo-flag.
LOCK_LOCK
#define LOCK_LOCK
GPIO value to write to lock attached lock.
Definition: SecureActuator.ino:77
MyMessage::getBool
bool getBool(void) const
Get bool payload.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289
LOCK_UNLOCK
#define LOCK_UNLOCK
GPIO value to write to unlock attached lock.
Definition: SecureActuator.ino:78