Created
March 8, 2021 10:57
-
-
Save sivar2311/4a783a23a30493003c200d4c8ed9d051 to your computer and use it in GitHub Desktop.
Custom Dimmer Sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _DIMMERSENSOR_H_ | |
#define _DIMMERSENSOR_H_ | |
#include <SinricProDevice.h> | |
#include <Capabilities/RangeController.h> | |
#include <Capabilities/ContactSensor.h> | |
class DimmerSensor | |
: public SinricProDevice | |
, public RangeController<DimmerSensor> | |
, public ContactSensor<DimmerSensor> { | |
friend class RangeController<DimmerSensor>; | |
friend class ContactSensor<DimmerSensor>; | |
public: | |
DimmerSensor(const DeviceId &deviceId) : SinricProDevice(deviceId, "DimmerSensor") {}; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#define ENABLE_DEBUG | |
#ifdef ENABLE_DEBUG | |
#define DEBUG_ESP_PORT Serial | |
#define NODEBUG_WEBSOCKETS | |
#define NDEBUG | |
#endif | |
#include <Arduino.h> | |
#ifdef ESP8266 | |
#include <ESP8266WiFi.h> | |
#endif | |
#ifdef ESP32 | |
#include <WiFi.h> | |
#endif | |
#include <SinricPro.h> | |
#include "DimmerSensor.h" | |
#define APP_KEY "YourAppKey" | |
#define APP_SECRET "YourAppSecret" | |
#define DEVICE_ID "YourDeviceId" | |
#define SSID "YourWiFiSSID" | |
#define PASS "YourWiFiPass" | |
#define BAUD_RATE 115200 | |
DimmerSensor &dimmerSensor = SinricPro[DEVICE_ID]; | |
int lowerSetpoint = 300; // initialize lowerSetpoint to 300 - possible improvment: store this value in eeprom and load at bootup | |
int upperSetpoint = 500; // initialize upperSetpoint to 500 - possible improvment: store this value in eeprom and load at bootup | |
bool contactState = false; // keeps track of contactSensorState | |
int lightLevel; // stores last sensor value | |
void checkSensor(unsigned long sensorInterval = 1000, unsigned long reportInterval = 2 * 60 *1000) { | |
unsigned long currentMillis = millis(); | |
static int lastLightLevel; // remember last LightLevel value | |
static unsigned long lastReading; // remember last sensor reading time | |
static unsigned long lastReportMillis; // remember last reporting time to SinricServer | |
if (currentMillis - lastReading < sensorInterval) return; // lastReading < sensorInterval? -> nothing to do return | else continue... | |
lastReading = currentMillis; // update last readReading timestamp | |
lightLevel = analogRead(A0); // read LDR sensor value from A0 | |
// Print lightLevel, lowerSetpoint upperSetpoint and contactState to Serial | |
Serial.printf("Current lightLevel: %d (lowerSetpoint: %d | upperSetpoint: %d | Contact is %s)\r\n", | |
lightLevel, | |
lowerSetpoint, | |
upperSetpoint, | |
contactState ? "closed" : "open" | |
); | |
// check | |
// - if lightLevel is below lowerSetpoint and contactState is open | |
// - or lightLevel is above upperSetpoint and contactState is closed | |
if ((lightLevel <= lowerSetpoint && !contactState) || (lightLevel >= upperSetpoint && contactState)) { | |
contactState = !contactState; // invert contactState because value is below lowerSetpoint or above upperSetpoint | |
// Print (depending on contactState) | |
// - contactState : true "Light level (x) is below lowerSetpoint (y). Contact is closed now." | |
// - contactState : false "Light level (x) is above upperSetpoint (z). Contact is open now." | |
Serial.printf("Light level (%d) is %s (%d). Contact is %s now.\r\n", | |
lightLevel, | |
contactState ? "below lowerSetpoint" : "above upperSetpoint", // contactState true : "below lowerSetpoint", false : "above upperSetpoint" | |
contactState ? lowerSetpoint : upperSetpoint, // contactState true : lowerSetpoint value, false : upperSetpoint value | |
contactState ? "closed" : "open" // contactState true : "closed", false : "open" | |
); | |
dimmerSensor.sendContactEvent(contactState); // update contactSensor state on server | |
dimmerSensor.sendRangeValueEvent("lightLevel", lightLevel); // update lightLevel on server | |
} | |
// if minimum report time is reached AND the last reported lightLevel is different form curren lightLevel | |
if ((currentMillis - lastReportMillis >= reportInterval) && (lastLightLevel != lightLevel)) { | |
lastReportMillis = currentMillis; // update lastReport time | |
lastLightLevel = lightLevel; // update last known lightLevel | |
dimmerSensor.sendRangeValueEvent("lightLevel", lightLevel); // update current light level on server | |
} | |
} | |
bool onLowerSetpoint(const String &deviceId, const String &instance, int &newLowerSetpoint) { | |
lowerSetpoint = newLowerSetpoint; | |
Serial.printf("LowerSetpoint changed to %d\r\n", lowerSetpoint); | |
checkSensor(0, 0); // check the sensor NOW | |
return true; | |
} | |
bool onUpperSetpoint(const String &deviceId, const String &instance, int &newUpperSetpoint) { | |
upperSetpoint = newUpperSetpoint; | |
Serial.printf("UpperSetpoint changed to %d\r\n", upperSetpoint); | |
checkSensor(0,0); // check the sensor NOW | |
return true; | |
} | |
// this callback is only to avoid error messages if the slider for lightValue have been changed | |
bool onLightValue(const String &deviceId, const String &instance, int &newLightValue) { | |
newLightValue = lightLevel; // set it back to last measured light value | |
return true; | |
} | |
void setupWiFi() { | |
WiFi.begin(SSID, PASS); | |
Serial.printf("[WiFi]: Connecting to %s", SSID); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.printf("."); | |
delay(250); | |
} | |
Serial.printf("connected\r\n"); | |
} | |
void setupSinricPro() { | |
dimmerSensor.onRangeValue("lowerSetpoint", onLowerSetpoint); // attach onLowerSetpoint callback to 'lowerSetpoint' instance | |
dimmerSensor.onRangeValue("upperSetpoint", onUpperSetpoint); // attach onUpperSetpoint callback to 'upperSetpoint' instance | |
dimmerSensor.onRangeValue("lightLevel", onLightValue); // attach onLightLevel callback to 'lightLevel' instance | |
SinricPro.onConnected([]{ Serial.printf("[SinricPro]: Connected\r\n"); }); // print connected message after connection have been established to server | |
SinricPro.onDisconnected([]{ Serial.printf("[SinricPro]: Disconnected\r\n"); }); // print disconnected message if connection was lost | |
SinricPro.begin(APP_KEY, APP_SECRET); | |
}; | |
void setup() { | |
Serial.begin(BAUD_RATE); // initialize Serial monitor | |
pinMode(A0, INPUT); // initialize input pin for LDR | |
setupWiFi(); // connect to wifi | |
setupSinricPro(); // setup sinric pro device and connection | |
} | |
void loop() { | |
SinricPro.handle(); // handle incomming requests / update server status | |
checkSensor(); // check sensor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment