|
| 1 | +/*** |
| 2 | + * This program is free software; you can redistribute it and/or |
| 3 | + * modify it under the terms of the GNU General Public License |
| 4 | + * version 2 as published by the Free Software Foundation. |
| 5 | + * |
| 6 | + * DESCRIPTION |
| 7 | + * This sketch provides an example how to implement a Dimmable Light |
| 8 | + * It is pure virtual and it logs messages to the serial output |
| 9 | + * It can be used as a base sketch for actual hardware. |
| 10 | + * Stores the last light state and level in eeprom. |
| 11 | + * |
| 12 | + * Developed by GizMoCuz (Domoticz) |
| 13 | + * |
| 14 | + * REVISION HISTORY |
| 15 | + * Version 1.0 - January 30, 2015 |
| 16 | + * |
| 17 | + ***/ |
| 18 | + |
| 19 | +#include <SPI.h> |
| 20 | +#include <MySensor.h> |
| 21 | + |
| 22 | +#define CHILD_ID_LIGHT 1 |
| 23 | + |
| 24 | +#define EPROM_LIGHT_STATE 1 |
| 25 | +#define EPROM_DIMMER_LEVEL 2 |
| 26 | + |
| 27 | +#define LIGHT_OFF 0 |
| 28 | +#define LIGHT_ON 1 |
| 29 | + |
| 30 | +#define SN "Dimable Light" |
| 31 | +#define SV "1.0" |
| 32 | + |
| 33 | +int LastLightState=LIGHT_OFF; |
| 34 | +int LastDimValue=100; |
| 35 | + |
| 36 | +MySensor gw; |
| 37 | +MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT); |
| 38 | +MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER); |
| 39 | + |
| 40 | +void setup() |
| 41 | +{ |
| 42 | + gw.begin(incomingMessage, AUTO, false); |
| 43 | + |
| 44 | + // Send the Sketch Version Information to the Gateway |
| 45 | + gw.sendSketchInfo(SN, SV); |
| 46 | + |
| 47 | + gw.present(CHILD_ID_LIGHT, S_DIMMER ); |
| 48 | + |
| 49 | + //Retreive our last light state from the eprom |
| 50 | + int LightState=gw.loadState(EPROM_LIGHT_STATE); |
| 51 | + if (LightState<=1) { |
| 52 | + LastLightState=LightState; |
| 53 | + int DimValue=gw.loadState(EPROM_DIMMER_LEVEL); |
| 54 | + if ((DimValue>0)&&(DimValue<=100)) { |
| 55 | + //There should be no Dim value of 0, this would mean LIGHT_OFF |
| 56 | + LastDimValue=DimValue; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + //Here you actualy switch on/off the light with the last known dim level |
| 61 | + SetCurrentState2Hardware(); |
| 62 | + |
| 63 | + Serial.println( "Node ready to receive messages..." ); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() |
| 67 | +{ |
| 68 | + //delay(1000); // Removed by hek |
| 69 | + // Process incoming messages (like config and light state from controller) |
| 70 | + gw.process(); |
| 71 | +} |
| 72 | + |
| 73 | +void incomingMessage(const MyMessage &message) |
| 74 | +{ |
| 75 | + if (message.type == V_LIGHT) { |
| 76 | + Serial.println( "V_LIGHT command received..." ); |
| 77 | + |
| 78 | + int lstate= atoi( message.data ); |
| 79 | + if ((lstate<0)||(lstate>1)) { |
| 80 | + Serial.println( "V_LIGHT data invalid (should be 0/1)" ); |
| 81 | + return; |
| 82 | + } |
| 83 | + LastLightState=lstate; |
| 84 | + gw.saveState(EPROM_LIGHT_STATE, LastLightState); |
| 85 | + |
| 86 | + if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) { |
| 87 | + //In the case that the Light State = On, but the dimmer value is zero, |
| 88 | + //then something (probably the controller) did something wrong, |
| 89 | + //for the Dim value to 100% |
| 90 | + LastDimValue=100; |
| 91 | + gw.saveState(EPROM_DIMMER_LEVEL, LastDimValue); |
| 92 | + } |
| 93 | + |
| 94 | + //When receiving a V_LIGHT command we switch the light between OFF and the last received dimmer value |
| 95 | + //This means if you previously set the lights dimmer value to 50%, and turn the light ON |
| 96 | + //it will do so at 50% |
| 97 | + } |
| 98 | + else if (message.type == V_DIMMER) { |
| 99 | + Serial.println( "V_DIMMER command received..." ); |
| 100 | + int dimvalue= atoi( message.data ); |
| 101 | + if ((dimvalue<0)||(dimvalue>100)) { |
| 102 | + Serial.println( "V_DIMMER data invalid (should be 0..100)" ); |
| 103 | + return; |
| 104 | + } |
| 105 | + if (dimvalue==0) { |
| 106 | + LastLightState=LIGHT_OFF; |
| 107 | + } |
| 108 | + else { |
| 109 | + LastLightState=LIGHT_ON; |
| 110 | + LastDimValue=dimvalue; |
| 111 | + gw.saveState(EPROM_DIMMER_LEVEL, LastDimValue); |
| 112 | + } |
| 113 | + } |
| 114 | + else { |
| 115 | + Serial.println( "Invalid command received..." ); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + //Here you set the actual light state/level |
| 120 | + SetCurrentState2Hardware(); |
| 121 | +} |
| 122 | + |
| 123 | +void SetCurrentState2Hardware() |
| 124 | +{ |
| 125 | + if (LastLightState==LIGHT_OFF) { |
| 126 | + Serial.println( "Light state: OFF" ); |
| 127 | + } |
| 128 | + else { |
| 129 | + Serial.print( "Light state: ON, Level: " ); |
| 130 | + Serial.println( LastDimValue ); |
| 131 | + } |
| 132 | + |
| 133 | + //Send current state to the controller |
| 134 | + SendCurrentState2Controller(); |
| 135 | +} |
| 136 | + |
| 137 | +void SendCurrentState2Controller() |
| 138 | +{ |
| 139 | + if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) { |
| 140 | + gw.send(dimmerMsg.set(0)); |
| 141 | + } |
| 142 | + else { |
| 143 | + gw.send(dimmerMsg.set(LastDimValue)); |
| 144 | + } |
| 145 | +} |
| 146 | + |
0 commit comments