Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
122 lines (103 sloc) 3.5 KB
* 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 <>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list:
* Documentation:
* Support Forum:
* 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.
* Version 1.0 - Henrik Ekblad
* Example sketch for a "light switch" where you can control light or something
* else from both HA controller and a local physical button
* (connected between digital pin 3 and GND).
* This node also works as a repeader for other nodes
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enabled repeater feature for this node
#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>
#define RELAY_PIN 4 // Arduino Digital I/O pin number for relay
#define BUTTON_PIN 3 // Arduino Digital I/O pin number for button
#define CHILD_ID 1 // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0
Bounce debouncer = Bounce();
int oldValue=0;
bool state;
MyMessage msg(CHILD_ID,V_LIGHT);
void setup()
// Setup the button
// Activate internal pull-up
// After setting up the button, setup debouncer
// Make sure relays are off when starting up
digitalWrite(RELAY_PIN, RELAY_OFF);
// Then set relay pins in output mode
// Set relay to last known state (using eeprom storage)
state = loadState(CHILD_ID);
digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay & Button", "1.0");
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID, S_LIGHT);
* Example on how to asynchronously check for new messages from gw
void loop()
// Get the update value
int value =;
if (value != oldValue && value==0) {
send(msg.set(state?false:true), true); // Send new state and request ack back
oldValue = value;
void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.isAck()) {
Serial.println("This is an ack from gateway");
if (message.type == V_LIGHT) {
// Change relay state
state = message.getBool();
digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
// Store state in eeprom
saveState(CHILD_ID, state);
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(", New status: ");