MySensors Library & Examples  2.3.2
RelayActuator.ino
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6  * network topology allowing messages to be routed to nodes.
7  *
8  * Created by Henrik Ekblad <[email protected]>
9  * Copyright (C) 2013-2019 Sensnology AB
10  * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11  *
12  * Documentation: http://www.mysensors.org
13  * Support Forum: http://forum.mysensors.org
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  *******************************
20  *
21  * REVISION HISTORY
22  * Version 1.0 - Henrik Ekblad
23  *
24  * DESCRIPTION
25  * Example sketch showing how to control physical relays.
26  * This example will remember relay state after power failure.
27  * http://www.mysensors.org/build/relay
28  */
29 
30 // Enable debug prints to serial monitor
31 #define MY_DEBUG
32 
33 // Enable and select radio type attached
34 #define MY_RADIO_RF24
35 //#define MY_RADIO_NRF5_ESB
36 //#define MY_RADIO_RFM69
37 //#define MY_RADIO_RFM95
38 
39 // Enable repeater functionality for this node
40 #define MY_REPEATER_FEATURE
41 
42 #include <MySensors.h>
43 
44 #define RELAY_PIN 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
45 #define NUMBER_OF_RELAYS 1 // Total number of attached relays
46 #define RELAY_ON 1 // GPIO value to write to turn on attached relay
47 #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
48 
49 
50 void before()
51 {
52  for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
53  // Then set relay pins in output mode
54  pinMode(pin, OUTPUT);
55  // Set relay to last known state (using eeprom storage)
56  digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
57  }
58 }
59 
60 void setup()
61 {
62 
63 }
64 
66 {
67  // Send the sketch version information to the gateway and Controller
68  sendSketchInfo("Relay", "1.0");
69 
70  for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
71  // Register all sensors to gw (they will be created as child devices)
72  present(sensor, S_BINARY);
73  }
74 }
75 
76 
77 void loop()
78 {
79 
80 }
81 
82 void receive(const MyMessage &message)
83 {
84  // We only expect one type of message from controller. But we better check anyway.
85  if (message.getType()==V_STATUS) {
86  // Change relay state
87  digitalWrite(message.getSensor()-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
88  // Store state in eeprom
89  saveState(message.getSensor(), message.getBool());
90  // Write some debug info
91  Serial.print("Incoming change for sensor:");
92  Serial.print(message.getSensor());
93  Serial.print(", New status: ");
94  Serial.println(message.getBool());
95  }
96 }
97 
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.
sensor
uint8_t sensor
8 bit - Id of sensor that this message concerns.
Definition: MyMessage.h:354
receive
void receive(const MyMessage &message)
Callback for incoming messages.
Definition: RelayActuator.ino:82
loop
void loop()
Main loop.
Definition: RelayActuator.ino:77
loadState
uint8_t loadState(const uint8_t pos)
presentation
void presentation()
Node presentation.
Definition: RelayActuator.ino:65
saveState
void saveState(const uint8_t pos, const uint8_t value)
before
void before()
Called before node initialises.
Definition: RelayActuator.ino:50
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: RelayActuator.ino:60
MySensors.h
API declaration for MySensors.
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