MySensors Library & Examples  2.3.2-36-g2e00bf6
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-2020 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 //#define MY_PJON
39 
40 // Enable repeater functionality for this node
41 #define MY_REPEATER_FEATURE
42 
43 #include <MySensors.h>
44 
45 #define RELAY_PIN 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
46 #define NUMBER_OF_RELAYS 1 // Total number of attached relays
47 #define RELAY_ON 1 // GPIO value to write to turn on attached relay
48 #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
49 
50 
51 void before()
52 {
53  for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
54  // Then set relay pins in output mode
55  pinMode(pin, OUTPUT);
56  // Set relay to last known state (using eeprom storage)
57  digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
58  }
59 }
60 
61 void setup()
62 {
63 
64 }
65 
67 {
68  // Send the sketch version information to the gateway and Controller
69  sendSketchInfo("Relay", "1.0");
70 
71  for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
72  // Register all sensors to gw (they will be created as child devices)
73  present(sensor, S_BINARY);
74  }
75 }
76 
77 
78 void loop()
79 {
80 
81 }
82 
83 void receive(const MyMessage &message)
84 {
85  // We only expect one type of message from controller. But we better check anyway.
86  if (message.getType()==V_STATUS) {
87  // Change relay state
88  digitalWrite(message.getSensor()-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
89  // Store state in eeprom
90  saveState(message.getSensor(), message.getBool());
91  // Write some debug info
92  Serial.print("Incoming change for sensor:");
93  Serial.print(message.getSensor());
94  Serial.print(", New status: ");
95  Serial.println(message.getBool());
96  }
97 }
98 
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:83
loop
void loop()
Main loop.
Definition: RelayActuator.ino:78
loadState
uint8_t loadState(const uint8_t pos)
presentation
void presentation()
Node presentation.
Definition: RelayActuator.ino:66
saveState
void saveState(const uint8_t pos, const uint8_t value)
before
void before()
Called before node initialises.
Definition: RelayActuator.ino:51
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:61
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