MySensors Library & Examples  2.3.2-38-gaa76d26
DimmableLight.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 - January 30, 2015 - Developed by GizMoCuz (Domoticz)
23  *
24  * DESCRIPTION
25  * This sketch provides an example how to implement a Dimmable Light
26  * It is pure virtual and it logs messages to the serial output
27  * It can be used as a base sketch for actual hardware.
28  * Stores the last light state and level in eeprom.
29  *
30  */
31 
32 // Enable debug prints
33 #define MY_DEBUG
34 
35 // Enable and select radio type attached
36 #define MY_RADIO_RF24
37 //#define MY_RADIO_NRF5_ESB
38 //#define MY_RADIO_RFM69
39 //#define MY_RADIO_RFM95
40 //#define MY_PJON
41 
42 #include <MySensors.h>
43 
44 #define CHILD_ID_LIGHT 1
45 
46 #define EPROM_LIGHT_STATE 1
47 #define EPROM_DIMMER_LEVEL 2
48 
49 #define LIGHT_OFF 0
50 #define LIGHT_ON 1
51 
52 #define SN "Dimmable Light"
53 #define SV "1.0"
54 
55 int16_t LastLightState=LIGHT_OFF;
56 int16_t LastDimValue=100;
57 
58 MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);
59 MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
60 
61 void setup()
62 {
63  //Retreive our last light state from the eprom
64  int LightState=loadState(EPROM_LIGHT_STATE);
65  if (LightState<=1) {
66  LastLightState=LightState;
67  int DimValue=loadState(EPROM_DIMMER_LEVEL);
68  if ((DimValue>0)&&(DimValue<=100)) {
69  //There should be no Dim value of 0, this would mean LIGHT_OFF
70  LastDimValue=DimValue;
71  }
72  }
73 
74  //Here you actually switch on/off the light with the last known dim level
75  SetCurrentState2Hardware();
76 
77  Serial.println( "Node ready to receive messages..." );
78 }
79 
81 {
82  // Send the Sketch Version Information to the Gateway
83  sendSketchInfo(SN, SV);
84 
85  present(CHILD_ID_LIGHT, S_DIMMER );
86 }
87 
88 void loop()
89 {
90 }
91 
92 void receive(const MyMessage &message)
93 {
94  if (message.getType() == V_LIGHT) {
95  Serial.println( "V_LIGHT command received..." );
96 
97  int lstate= atoi( message.data );
98  if ((lstate<0)||(lstate>1)) {
99  Serial.println( "V_LIGHT data invalid (should be 0/1)" );
100  return;
101  }
102  LastLightState=lstate;
103  saveState(EPROM_LIGHT_STATE, LastLightState);
104 
105  if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) {
106  //In the case that the Light State = On, but the dimmer value is zero,
107  //then something (probably the controller) did something wrong,
108  //for the Dim value to 100%
109  LastDimValue=100;
110  saveState(EPROM_DIMMER_LEVEL, LastDimValue);
111  }
112 
113  //When receiving a V_LIGHT command we switch the light between OFF and the last received dimmer value
114  //This means if you previously set the lights dimmer value to 50%, and turn the light ON
115  //it will do so at 50%
116  } else if (message.getType() == V_DIMMER) {
117  Serial.println( "V_DIMMER command received..." );
118  int dimvalue= atoi( message.data );
119  if ((dimvalue<0)||(dimvalue>100)) {
120  Serial.println( "V_DIMMER data invalid (should be 0..100)" );
121  return;
122  }
123  if (dimvalue==0) {
124  LastLightState=LIGHT_OFF;
125  } else {
126  LastLightState=LIGHT_ON;
127  LastDimValue=dimvalue;
128  saveState(EPROM_DIMMER_LEVEL, LastDimValue);
129  }
130  } else {
131  Serial.println( "Invalid command received..." );
132  return;
133  }
134 
135  //Here you set the actual light state/level
136  SetCurrentState2Hardware();
137 }
138 
139 void SetCurrentState2Hardware()
140 {
141  if (LastLightState==LIGHT_OFF) {
142  Serial.println( "Light state: OFF" );
143  } else {
144  Serial.print( "Light state: ON, Level: " );
145  Serial.println( LastDimValue );
146  }
147 
148  //Send current state to the controller
149  SendCurrentState2Controller();
150 }
151 
152 void SendCurrentState2Controller()
153 {
154  if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) {
155  send(dimmerMsg.set((int16_t)0));
156  } else {
157  send(dimmerMsg.set(LastDimValue));
158  }
159 }
sendSketchInfo
bool sendSketchInfo(const char *name, const char *version, const bool requestEcho=false)
receive
void receive(const MyMessage &message)
Callback for incoming messages.
Definition: DimmableLight.ino:92
loop
void loop()
Main loop.
Definition: DimmableLight.ino:88
loadState
uint8_t loadState(const uint8_t pos)
presentation
void presentation()
Node presentation.
Definition: DimmableLight.ino:80
saveState
void saveState(const uint8_t pos, const uint8_t value)
MyMessage::set
MyMessage & set(const void *payload, const size_t length)
Set entire payload.
send
bool send(MyMessage &msg, const bool requestEcho=false)
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: DimmableLight.ino:61
MyMessage::data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:660
MySensors.h
API declaration for MySensors.
MyMessage
MyMessage is used to create, manipulate, send and read MySensors messages.
Definition: MyMessage.h:289