wip
This commit is contained in:
parent
1d04b22eb2
commit
4af7a079d8
|
@ -1,16 +1,7 @@
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg import esphome.config_validation as cv from esphome.core import coroutine_with_priority
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.core import coroutine_with_priority
|
|
||||||
|
|
||||||
ratgdo_ns = cg.esphome_ns.namespace("ratgdo")
|
ratgdo_ns = cg.esphome_ns.namespace("ratgdo")
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(cv.Schema({}), )
|
||||||
cv.Schema({}),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
@coroutine_with_priority(1.0) async def to_code(config) :cg.add_library("bblanchon/ArduinoJson", "6.18.5") cg.add_define("USE_JSON") cg.add_global(ratgdo_ns.using)
|
||||||
@coroutine_with_priority(1.0)
|
|
||||||
async def to_code(config):
|
|
||||||
cg.add_library("bblanchon/ArduinoJson", "6.18.5")
|
|
||||||
cg.add_define("USE_JSON")
|
|
||||||
cg.add_global(ratgdo_ns.using)
|
|
||||||
|
|
|
@ -11,488 +11,507 @@
|
||||||
* GNU GENERAL PUBLIC LICENSE
|
* GNU GENERAL PUBLIC LICENSE
|
||||||
************************************/
|
************************************/
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
#include "ratgdo.h"
|
#include "ratgdo.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
namespace esphome
|
namespace esphome {
|
||||||
{
|
namespace ratgdo {
|
||||||
namespace ratgdo
|
|
||||||
{
|
|
||||||
|
|
||||||
static const char *const TAG = "ratgdo";
|
static const char *const TAG = "ratgdo";
|
||||||
|
|
||||||
void RATGDOComponent::setup()
|
void RATGDOComponent::setup() {
|
||||||
{
|
pinMode(TRIGGER_OPEN, INPUT_PULLUP);
|
||||||
pinMode(TRIGGER_OPEN, INPUT_PULLUP);
|
pinMode(TRIGGER_CLOSE, INPUT_PULLUP);
|
||||||
pinMode(TRIGGER_CLOSE, INPUT_PULLUP);
|
pinMode(TRIGGER_LIGHT, INPUT_PULLUP);
|
||||||
pinMode(TRIGGER_LIGHT, INPUT_PULLUP);
|
pinMode(STATUS_DOOR, OUTPUT);
|
||||||
pinMode(STATUS_DOOR, OUTPUT);
|
pinMode(STATUS_OBST, OUTPUT);
|
||||||
pinMode(STATUS_OBST, OUTPUT);
|
pinMode(INPUT_RPM1,
|
||||||
pinMode(INPUT_RPM1, INPUT_PULLUP); // set to pullup to add support for reed switches
|
INPUT_PULLUP); // set to pullup to add support for reed switches
|
||||||
pinMode(INPUT_RPM2, INPUT_PULLUP); // make sure pin doesn't float when using reed switch and fire interrupt by mistake
|
pinMode(INPUT_RPM2,
|
||||||
pinMode(INPUT_OBST, INPUT);
|
INPUT_PULLUP); // make sure pin doesn't float when using reed switch
|
||||||
|
// and fire interrupt by mistake
|
||||||
|
pinMode(INPUT_OBST, INPUT);
|
||||||
|
|
||||||
attachInterrupt(TRIGGER_OPEN,isrDoorOpen,CHANGE);
|
attachInterrupt(TRIGGER_OPEN, isrDoorOpen, CHANGE);
|
||||||
attachInterrupt(TRIGGER_CLOSE,isrDoorClose,CHANGE);
|
attachInterrupt(TRIGGER_CLOSE, isrDoorClose, CHANGE);
|
||||||
attachInterrupt(TRIGGER_LIGHT,isrLight,CHANGE);
|
attachInterrupt(TRIGGER_LIGHT, isrLight, CHANGE);
|
||||||
attachInterrupt(INPUT_OBST,isrObstruction,CHANGE);
|
attachInterrupt(INPUT_OBST, isrObstruction, CHANGE);
|
||||||
attachInterrupt(INPUT_RPM1,isrRPM1,RISING);
|
attachInterrupt(INPUT_RPM1, isrRPM1, RISING);
|
||||||
attachInterrupt(INPUT_RPM2,isrRPM2,RISING);
|
attachInterrupt(INPUT_RPM2, isrRPM2, RISING);
|
||||||
|
|
||||||
LittleFS.begin();
|
LittleFS.begin();
|
||||||
|
|
||||||
readCounterFromFlash();
|
readCounterFromFlash();
|
||||||
|
|
||||||
if(useRollingCodes){
|
if (useRollingCodes) {
|
||||||
//if(rollingCodeCounter == 0) rollingCodeCounter = 1;
|
// if(rollingCodeCounter == 0) rollingCodeCounter = 1;
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Syncing rolling code counter after reboot...");
|
ESP_LOGD(TAG, "Syncing rolling code counter after reboot...");
|
||||||
sync(); // if rolling codes are being used (rolling code counter > 0), send reboot/sync to the opener on startup
|
sync(); // if rolling codes are being used (rolling code counter > 0), send
|
||||||
}else{
|
// reboot/sync to the opener on startup
|
||||||
ESP_LOGD(TAG, "Rolling codes are disabled.");
|
} else {
|
||||||
}
|
ESP_LOGD(TAG, "Rolling codes are disabled.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RATGDOComponent::loop(){
|
void RATGDOComponent::loop() {
|
||||||
obstructionLoop();
|
obstructionLoop();
|
||||||
doorStateLoop();
|
doorStateLoop();
|
||||||
dryContactLoop();
|
dryContactLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace ratgdo
|
||||||
} // namespace ratgdo
|
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
|
/*************************** DETECTING THE DOOR STATE
|
||||||
|
* ***************************/
|
||||||
|
void doorStateLoop() {
|
||||||
|
static bool rotaryEncoderDetected = false;
|
||||||
|
static int lastDoorPositionCounter = 0;
|
||||||
|
static int lastDirectionChangeCounter = 0;
|
||||||
|
static int lastCounterMillis = 0;
|
||||||
|
|
||||||
|
// Handle reed switch
|
||||||
|
// This may need to be debounced, but so far in testing I haven't detected any
|
||||||
|
// bounces
|
||||||
|
if (!rotaryEncoderDetected) {
|
||||||
|
if (digitalRead(INPUT_RPM1) == LOW) {
|
||||||
|
if (doorState != "reed_closed") {
|
||||||
|
ESP_LOGD(TAG, "Reed switch closed");
|
||||||
|
doorState = "reed_closed";
|
||||||
|
if (isConfigFileOk) {
|
||||||
|
bootstrapManager.publish(overallStatusTopic.c_str(), "reed_closed",
|
||||||
|
true);
|
||||||
|
bootstrapManager.publish(doorStatusTopic.c_str(), "reed_closed",
|
||||||
|
true);
|
||||||
|
}
|
||||||
|
digitalWrite(STATUS_DOOR, HIGH);
|
||||||
|
}
|
||||||
|
} else if (doorState != "reed_open") {
|
||||||
|
ESP_LOGD(TAG, "Reed switch open");
|
||||||
|
doorState = "reed_open";
|
||||||
|
if (isConfigFileOk) {
|
||||||
|
bootstrapManager.publish(overallStatusTopic.c_str(), "reed_open", true);
|
||||||
|
bootstrapManager.publish(doorStatusTopic.c_str(), "reed_open", true);
|
||||||
|
}
|
||||||
|
digitalWrite(STATUS_DOOR, LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end reed switch handling
|
||||||
|
|
||||||
/*************************** DETECTING THE DOOR STATE ***************************/
|
// If the previous and the current state of the RPM2 Signal are different,
|
||||||
void doorStateLoop(){
|
// that means there is a rotary encoder detected and the door is moving
|
||||||
static bool rotaryEncoderDetected = false;
|
if (doorPositionCounter != lastDoorPositionCounter) {
|
||||||
static int lastDoorPositionCounter = 0;
|
rotaryEncoderDetected = true; // this disables the reed switch handler
|
||||||
static int lastDirectionChangeCounter = 0;
|
lastCounterMillis = millis();
|
||||||
static int lastCounterMillis = 0;
|
|
||||||
|
|
||||||
// Handle reed switch
|
ESP_LOGD(TAG, "Door Position: %d", doorPositionCounter);
|
||||||
// This may need to be debounced, but so far in testing I haven't detected any bounces
|
}
|
||||||
if(!rotaryEncoderDetected){
|
|
||||||
if(digitalRead(INPUT_RPM1) == LOW){
|
|
||||||
if(doorState != "reed_closed"){
|
|
||||||
ESP_LOGD(TAG, "Reed switch closed");
|
|
||||||
doorState = "reed_closed";
|
|
||||||
if(isConfigFileOk){
|
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), "reed_closed", true);
|
|
||||||
bootstrapManager.publish(doorStatusTopic.c_str(), "reed_closed", true);
|
|
||||||
}
|
|
||||||
digitalWrite(STATUS_DOOR,HIGH);
|
|
||||||
}
|
|
||||||
}else if(doorState != "reed_open"){
|
|
||||||
ESP_LOGD(TAG, "Reed switch open");
|
|
||||||
doorState = "reed_open";
|
|
||||||
if(isConfigFileOk){
|
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), "reed_open", true);
|
|
||||||
bootstrapManager.publish(doorStatusTopic.c_str(), "reed_open", true);
|
|
||||||
}
|
|
||||||
digitalWrite(STATUS_DOOR,LOW);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// end reed switch handling
|
|
||||||
|
|
||||||
// If the previous and the current state of the RPM2 Signal are different, that means there is a rotary encoder detected and the door is moving
|
// Wait 5 pulses before updating to door opening status
|
||||||
if(doorPositionCounter != lastDoorPositionCounter){
|
if (doorPositionCounter - lastDirectionChangeCounter > 5) {
|
||||||
rotaryEncoderDetected = true; // this disables the reed switch handler
|
if (doorState != "opening") {
|
||||||
lastCounterMillis = millis();
|
ESP_LOGD(TAG, "Door Opening...");
|
||||||
|
if (isConfigFileOk) {
|
||||||
|
bootstrapManager.publish(overallStatusTopic.c_str(), "opening", true);
|
||||||
|
bootstrapManager.publish(doorStatusTopic.c_str(), "opening", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastDirectionChangeCounter = doorPositionCounter;
|
||||||
|
doorState = "opening";
|
||||||
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Door Position: %d", doorPositionCounter);
|
if (lastDirectionChangeCounter - doorPositionCounter > 5) {
|
||||||
}
|
if (doorState != "closing") {
|
||||||
|
ESP_LOGD(TAG, "Door Closing...");
|
||||||
|
if (isConfigFileOk) {
|
||||||
|
bootstrapManager.publish(overallStatusTopic.c_str(), "closing", true);
|
||||||
|
bootstrapManager.publish(doorStatusTopic.c_str(), "closing", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastDirectionChangeCounter = doorPositionCounter;
|
||||||
|
doorState = "closing";
|
||||||
|
}
|
||||||
|
|
||||||
// Wait 5 pulses before updating to door opening status
|
// 250 millis after the last rotary encoder pulse, the door is stopped
|
||||||
if(doorPositionCounter - lastDirectionChangeCounter > 5){
|
if (millis() - lastCounterMillis > 250) {
|
||||||
if(doorState != "opening"){
|
// if the door was closing, and is now stopped, then the door is closed
|
||||||
ESP_LOGD(TAG,"Door Opening...");
|
if (doorState == "closing") {
|
||||||
if(isConfigFileOk){
|
doorState = "closed";
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), "opening", true);
|
ESP_LOGD(TAG, "Closed");
|
||||||
bootstrapManager.publish(doorStatusTopic.c_str(), "opening", true);
|
if (isConfigFileOk) {
|
||||||
}
|
bootstrapManager.publish(overallStatusTopic.c_str(), doorState.c_str(),
|
||||||
}
|
true);
|
||||||
lastDirectionChangeCounter = doorPositionCounter;
|
bootstrapManager.publish(doorStatusTopic.c_str(), doorState.c_str(),
|
||||||
doorState = "opening";
|
true);
|
||||||
}
|
}
|
||||||
|
digitalWrite(STATUS_DOOR, LOW);
|
||||||
|
}
|
||||||
|
|
||||||
if(lastDirectionChangeCounter - doorPositionCounter > 5){
|
// if the door was opening, and is now stopped, then the door is open
|
||||||
if(doorState != "closing"){
|
if (doorState == "opening") {
|
||||||
ESP_LOGD(TAG,"Door Closing...");
|
doorState = "open";
|
||||||
if(isConfigFileOk){
|
ESP_LOGD(TAG, "Open");
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), "closing", true);
|
if (isConfigFileOk) {
|
||||||
bootstrapManager.publish(doorStatusTopic.c_str(), "closing", true);
|
bootstrapManager.publish(overallStatusTopic.c_str(), doorState.c_str(),
|
||||||
}
|
true);
|
||||||
}
|
bootstrapManager.publish(doorStatusTopic.c_str(), doorState.c_str(),
|
||||||
lastDirectionChangeCounter = doorPositionCounter;
|
true);
|
||||||
doorState = "closing";
|
}
|
||||||
}
|
digitalWrite(STATUS_DOOR, HIGH);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 250 millis after the last rotary encoder pulse, the door is stopped
|
lastDoorPositionCounter = doorPositionCounter;
|
||||||
if(millis() - lastCounterMillis > 250){
|
|
||||||
// if the door was closing, and is now stopped, then the door is closed
|
|
||||||
if(doorState == "closing"){
|
|
||||||
doorState = "closed";
|
|
||||||
ESP_LOGD(TAG,"Closed");
|
|
||||||
if(isConfigFileOk){
|
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), doorState.c_str(), true);
|
|
||||||
bootstrapManager.publish(doorStatusTopic.c_str(), doorState.c_str(), true);
|
|
||||||
}
|
|
||||||
digitalWrite(STATUS_DOOR,LOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the door was opening, and is now stopped, then the door is open
|
|
||||||
if(doorState == "opening"){
|
|
||||||
doorState = "open";
|
|
||||||
ESP_LOGD(TAG,"Open");
|
|
||||||
if(isConfigFileOk){
|
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), doorState.c_str(), true);
|
|
||||||
bootstrapManager.publish(doorStatusTopic.c_str(), doorState.c_str(), true);
|
|
||||||
}
|
|
||||||
digitalWrite(STATUS_DOOR,HIGH);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lastDoorPositionCounter = doorPositionCounter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************** DRY CONTACT CONTROL OF LIGHT & DOOR ***************************/
|
/*************************** DRY CONTACT CONTROL OF LIGHT & DOOR
|
||||||
void IRAM_ATTR isrDebounce(const char *type){
|
* ***************************/
|
||||||
static unsigned long lastOpenDoorTime = 0;
|
void IRAM_ATTR isrDebounce(const char *type) {
|
||||||
static unsigned long lastCloseDoorTime = 0;
|
static unsigned long lastOpenDoorTime = 0;
|
||||||
static unsigned long lastToggleLightTime = 0;
|
static unsigned long lastCloseDoorTime = 0;
|
||||||
unsigned long currentMillis = millis();
|
static unsigned long lastToggleLightTime = 0;
|
||||||
|
unsigned long currentMillis = millis();
|
||||||
|
|
||||||
// Prevent ISR during the first 2 seconds after reboot
|
// Prevent ISR during the first 2 seconds after reboot
|
||||||
if(currentMillis < 2000) return;
|
if (currentMillis < 2000)
|
||||||
|
return;
|
||||||
|
|
||||||
if(strcmp(type, "openDoor") == 0){
|
if (strcmp(type, "openDoor") == 0) {
|
||||||
if(digitalRead(TRIGGER_OPEN) == LOW){
|
if (digitalRead(TRIGGER_OPEN) == LOW) {
|
||||||
// save the time of the falling edge
|
// save the time of the falling edge
|
||||||
lastOpenDoorTime = currentMillis;
|
lastOpenDoorTime = currentMillis;
|
||||||
}else if(currentMillis - lastOpenDoorTime > 500 && currentMillis - lastOpenDoorTime < 10000){
|
} else if (currentMillis - lastOpenDoorTime > 500 &&
|
||||||
// now see if the rising edge was between 500ms and 10 seconds after the falling edge
|
currentMillis - lastOpenDoorTime < 10000) {
|
||||||
dryContactDoorOpen = true;
|
// now see if the rising edge was between 500ms and 10 seconds after the
|
||||||
}
|
// falling edge
|
||||||
}
|
dryContactDoorOpen = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(strcmp(type, "closeDoor") == 0){
|
if (strcmp(type, "closeDoor") == 0) {
|
||||||
if(digitalRead(TRIGGER_CLOSE) == LOW){
|
if (digitalRead(TRIGGER_CLOSE) == LOW) {
|
||||||
// save the time of the falling edge
|
// save the time of the falling edge
|
||||||
lastCloseDoorTime = currentMillis;
|
lastCloseDoorTime = currentMillis;
|
||||||
}else if(currentMillis - lastCloseDoorTime > 500 && currentMillis - lastCloseDoorTime < 10000){
|
} else if (currentMillis - lastCloseDoorTime > 500 &&
|
||||||
// now see if the rising edge was between 500ms and 10 seconds after the falling edge
|
currentMillis - lastCloseDoorTime < 10000) {
|
||||||
dryContactDoorClose = true;
|
// now see if the rising edge was between 500ms and 10 seconds after the
|
||||||
}
|
// falling edge
|
||||||
}
|
dryContactDoorClose = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(strcmp(type, "toggleLight") == 0){
|
if (strcmp(type, "toggleLight") == 0) {
|
||||||
if(digitalRead(TRIGGER_LIGHT) == LOW){
|
if (digitalRead(TRIGGER_LIGHT) == LOW) {
|
||||||
// save the time of the falling edge
|
// save the time of the falling edge
|
||||||
lastToggleLightTime = currentMillis;
|
lastToggleLightTime = currentMillis;
|
||||||
}else if(currentMillis - lastToggleLightTime > 500 && currentMillis - lastToggleLightTime < 10000){
|
} else if (currentMillis - lastToggleLightTime > 500 &&
|
||||||
// now see if the rising edge was between 500ms and 10 seconds after the falling edge
|
currentMillis - lastToggleLightTime < 10000) {
|
||||||
dryContactToggleLight = true;
|
// now see if the rising edge was between 500ms and 10 seconds after the
|
||||||
}
|
// falling edge
|
||||||
}
|
dryContactToggleLight = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void IRAM_ATTR isrDoorOpen(){
|
void IRAM_ATTR isrDoorOpen() { isrDebounce("openDoor"); }
|
||||||
isrDebounce("openDoor");
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRAM_ATTR isrDoorClose(){
|
void IRAM_ATTR isrDoorClose() { isrDebounce("closeDoor"); }
|
||||||
isrDebounce("closeDoor");
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRAM_ATTR isrLight(){
|
void IRAM_ATTR isrLight() { isrDebounce("toggleLight"); }
|
||||||
isrDebounce("toggleLight");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fire on RISING edge of RPM1
|
// Fire on RISING edge of RPM1
|
||||||
void IRAM_ATTR isrRPM1(){
|
void IRAM_ATTR isrRPM1() { rpm1Pulsed = true; }
|
||||||
rpm1Pulsed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fire on RISING edge of RPM2
|
// Fire on RISING edge of RPM2
|
||||||
// When RPM1 HIGH on RPM2 rising edge, door closing:
|
// When RPM1 HIGH on RPM2 rising edge, door closing:
|
||||||
// RPM1: __|--|___
|
// RPM1: __|--|___
|
||||||
// RPM2: ___|--|__
|
// RPM2: ___|--|__
|
||||||
|
|
||||||
// When RPM1 LOW on RPM2 rising edge, door opening:
|
// When RPM1 LOW on RPM2 rising edge, door opening:
|
||||||
// RPM1: ___|--|__
|
// RPM1: ___|--|__
|
||||||
// RPM2: __|--|___
|
// RPM2: __|--|___
|
||||||
void IRAM_ATTR isrRPM2(){
|
void IRAM_ATTR isrRPM2() {
|
||||||
// The encoder updates faster than the ESP wants to process, so by sampling every 5ms we get a more reliable curve
|
// The encoder updates faster than the ESP wants to process, so by sampling
|
||||||
// The counter is behind the actual pulse counter, but it doesn't matter since we only need a reliable linear counter
|
// every 5ms we get a more reliable curve The counter is behind the actual
|
||||||
// to determine the door direction
|
// pulse counter, but it doesn't matter since we only need a reliable linear
|
||||||
static unsigned long lastPulse = 0;
|
// counter to determine the door direction
|
||||||
unsigned long currentMillis = millis();
|
static unsigned long lastPulse = 0;
|
||||||
|
unsigned long currentMillis = millis();
|
||||||
|
|
||||||
if(currentMillis - lastPulse < 5){
|
if (currentMillis - lastPulse < 5) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// In rare situations, the rotary encoder can be parked so that RPM2 continuously fires this ISR.
|
// In rare situations, the rotary encoder can be parked so that RPM2
|
||||||
// This causes the door counter to change value even though the door isn't moving
|
// continuously fires this ISR. This causes the door counter to change value
|
||||||
// To solve this, check to see if RPM1 pulsed. If not, do nothing. If yes, reset the pulsed flag
|
// even though the door isn't moving To solve this, check to see if RPM1
|
||||||
if(rpm1Pulsed){
|
// pulsed. If not, do nothing. If yes, reset the pulsed flag
|
||||||
rpm1Pulsed = false;
|
if (rpm1Pulsed) {
|
||||||
}else{
|
rpm1Pulsed = false;
|
||||||
return;
|
} else {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
lastPulse = millis();
|
lastPulse = millis();
|
||||||
|
|
||||||
// If the RPM1 state is different from the RPM2 state, then the door is opening
|
// If the RPM1 state is different from the RPM2 state, then the door is
|
||||||
if(digitalRead(INPUT_RPM1)){
|
// opening
|
||||||
doorPositionCounter--;
|
if (digitalRead(INPUT_RPM1)) {
|
||||||
}else{
|
doorPositionCounter--;
|
||||||
doorPositionCounter++;
|
} else {
|
||||||
}
|
doorPositionCounter++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle changes to the dry contact state
|
// handle changes to the dry contact state
|
||||||
void dryContactLoop(){
|
void dryContactLoop() {
|
||||||
if(dryContactDoorOpen){
|
if (dryContactDoorOpen) {
|
||||||
ESP_LOGD(TAG,"Dry Contact: open the door");
|
ESP_LOGD(TAG, "Dry Contact: open the door");
|
||||||
dryContactDoorOpen = false;
|
dryContactDoorOpen = false;
|
||||||
openDoor();
|
openDoor();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dryContactDoorClose){
|
if (dryContactDoorClose) {
|
||||||
ESP_LOGD(TAG,"Dry Contact: close the door");
|
ESP_LOGD(TAG, "Dry Contact: close the door");
|
||||||
dryContactDoorClose = false;
|
dryContactDoorClose = false;
|
||||||
closeDoor();
|
closeDoor();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(dryContactToggleLight){
|
if (dryContactToggleLight) {
|
||||||
ESP_LOGD(TAG,"Dry Contact: toggle the light");
|
ESP_LOGD(TAG, "Dry Contact: toggle the light");
|
||||||
dryContactToggleLight = false;
|
dryContactToggleLight = false;
|
||||||
toggleLight();
|
toggleLight();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************** OBSTRUCTION DETECTION ***************************/
|
/*************************** OBSTRUCTION DETECTION ***************************/
|
||||||
void IRAM_ATTR isrObstruction(){
|
void IRAM_ATTR isrObstruction() {
|
||||||
if(digitalRead(INPUT_OBST)){
|
if (digitalRead(INPUT_OBST)) {
|
||||||
lastObstructionHigh = millis();
|
lastObstructionHigh = millis();
|
||||||
}else{
|
} else {
|
||||||
obstructionLowCount++;
|
obstructionLowCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void obstructionLoop(){
|
void obstructionLoop() {
|
||||||
long currentMillis = millis();
|
long currentMillis = millis();
|
||||||
static unsigned long lastMillis = 0;
|
static unsigned long lastMillis = 0;
|
||||||
|
|
||||||
// the obstruction sensor has 3 states: clear (HIGH with LOW pulse every 7ms), obstructed (HIGH), asleep (LOW)
|
// the obstruction sensor has 3 states: clear (HIGH with LOW pulse every 7ms),
|
||||||
// the transitions between awake and asleep are tricky because the voltage drops slowly when falling asleep
|
// obstructed (HIGH), asleep (LOW) the transitions between awake and asleep
|
||||||
// and is high without pulses when waking up
|
// are tricky because the voltage drops slowly when falling asleep and is high
|
||||||
|
// without pulses when waking up
|
||||||
|
|
||||||
// If at least 3 low pulses are counted within 50ms, the door is awake, not obstructed and we don't have to check anything else
|
// If at least 3 low pulses are counted within 50ms, the door is awake, not
|
||||||
|
// obstructed and we don't have to check anything else
|
||||||
|
|
||||||
// Every 50ms
|
// Every 50ms
|
||||||
if(currentMillis - lastMillis > 50){
|
if (currentMillis - lastMillis > 50) {
|
||||||
// check to see if we got between 3 and 8 low pulses on the line
|
// check to see if we got between 3 and 8 low pulses on the line
|
||||||
if(obstructionLowCount >= 3 && obstructionLowCount <= 8){
|
if (obstructionLowCount >= 3 && obstructionLowCount <= 8) {
|
||||||
obstructionCleared();
|
obstructionCleared();
|
||||||
|
|
||||||
// if there have been no pulses the line is steady high or low
|
// if there have been no pulses the line is steady high or low
|
||||||
}else if(obstructionLowCount == 0){
|
} else if (obstructionLowCount == 0) {
|
||||||
// if the line is high and the last high pulse was more than 70ms ago, then there is an obstruction present
|
// if the line is high and the last high pulse was more than 70ms ago,
|
||||||
if(digitalRead(INPUT_OBST) && currentMillis - lastObstructionHigh > 70){
|
// then there is an obstruction present
|
||||||
obstructionDetected();
|
if (digitalRead(INPUT_OBST) && currentMillis - lastObstructionHigh > 70) {
|
||||||
}else{
|
obstructionDetected();
|
||||||
// asleep
|
} else {
|
||||||
}
|
// asleep
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lastMillis = currentMillis;
|
lastMillis = currentMillis;
|
||||||
obstructionLowCount = 0;
|
obstructionLowCount = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void obstructionDetected(){
|
void obstructionDetected() {
|
||||||
static unsigned long lastInterruptTime = 0;
|
static unsigned long lastInterruptTime = 0;
|
||||||
unsigned long interruptTime = millis();
|
unsigned long interruptTime = millis();
|
||||||
// Anything less than 100ms is a bounce and is ignored
|
// Anything less than 100ms is a bounce and is ignored
|
||||||
if(interruptTime - lastInterruptTime > 250){
|
if (interruptTime - lastInterruptTime > 250) {
|
||||||
doorIsObstructed = true;
|
doorIsObstructed = true;
|
||||||
digitalWrite(STATUS_OBST,HIGH);
|
digitalWrite(STATUS_OBST, HIGH);
|
||||||
|
|
||||||
ESP_LOGD(TAG,"Obstruction Detected");
|
ESP_LOGD(TAG, "Obstruction Detected");
|
||||||
|
|
||||||
if(isConfigFileOk){
|
if (isConfigFileOk) {
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), "obstructed", true);
|
bootstrapManager.publish(overallStatusTopic.c_str(), "obstructed", true);
|
||||||
bootstrapManager.publish(obstructionStatusTopic.c_str(), "obstructed", true);
|
bootstrapManager.publish(obstructionStatusTopic.c_str(), "obstructed",
|
||||||
}
|
true);
|
||||||
}
|
}
|
||||||
lastInterruptTime = interruptTime;
|
}
|
||||||
|
lastInterruptTime = interruptTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
void obstructionCleared(){
|
void obstructionCleared() {
|
||||||
if(doorIsObstructed){
|
if (doorIsObstructed) {
|
||||||
doorIsObstructed = false;
|
doorIsObstructed = false;
|
||||||
digitalWrite(STATUS_OBST,LOW);
|
digitalWrite(STATUS_OBST, LOW);
|
||||||
|
|
||||||
ESP_LOGD(TAG,"Obstruction Cleared");
|
ESP_LOGD(TAG, "Obstruction Cleared");
|
||||||
|
|
||||||
if(isConfigFileOk){
|
if (isConfigFileOk) {
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), "clear", true);
|
bootstrapManager.publish(overallStatusTopic.c_str(), "clear", true);
|
||||||
bootstrapManager.publish(obstructionStatusTopic.c_str(), "clear", true);
|
bootstrapManager.publish(obstructionStatusTopic.c_str(), "clear", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendDoorStatus(){
|
void sendDoorStatus() {
|
||||||
ESP_LOGD(TAG,"Door state %s", doorState);
|
ESP_LOGD(TAG, "Door state %s", doorState);
|
||||||
|
|
||||||
if(isConfigFileOk){
|
if (isConfigFileOk) {
|
||||||
bootstrapManager.publish(overallStatusTopic.c_str(), doorState.c_str(), true);
|
bootstrapManager.publish(overallStatusTopic.c_str(), doorState.c_str(),
|
||||||
bootstrapManager.publish(doorStatusTopic.c_str(), doorState.c_str(), true);
|
true);
|
||||||
}
|
bootstrapManager.publish(doorStatusTopic.c_str(), doorState.c_str(), true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCurrentCounter(){
|
void sendCurrentCounter() {
|
||||||
String msg = String(rollingCodeCounter);
|
String msg = String(rollingCodeCounter);
|
||||||
ESP_LOGD(TAG, "Current counter %d", rollingCodeCounter);
|
ESP_LOGD(TAG, "Current counter %d", rollingCodeCounter);
|
||||||
if(isConfigFileOk){
|
if (isConfigFileOk) {
|
||||||
bootstrapManager.publish(rollingCodeTopic.c_str(), msg.c_str(), true);
|
bootstrapManager.publish(rollingCodeTopic.c_str(), msg.c_str(), true);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/********************************** MANAGE HARDWARE BUTTON *****************************************/
|
|
||||||
void manageHardwareButton(){
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/********************************** MANAGE HARDWARE BUTTON
|
||||||
|
* *****************************************/
|
||||||
|
void manageHardwareButton() {}
|
||||||
|
|
||||||
/************************* DOOR COMMUNICATION *************************/
|
/************************* DOOR COMMUNICATION *************************/
|
||||||
/*
|
/*
|
||||||
* Transmit a message to the door opener over uart1
|
* Transmit a message to the door opener over uart1
|
||||||
* The TX1 pin is controlling a transistor, so the logic is inverted
|
* The TX1 pin is controlling a transistor, so the logic is inverted
|
||||||
* A HIGH state on TX1 will pull the 12v line LOW
|
* A HIGH state on TX1 will pull the 12v line LOW
|
||||||
*
|
*
|
||||||
* The opener requires a specific duration low/high pulse before it will accept a message
|
* The opener requires a specific duration low/high pulse before it will accept
|
||||||
|
* a message
|
||||||
*/
|
*/
|
||||||
void transmit(byte* payload, unsigned int length){
|
void transmit(byte *payload, unsigned int length) {
|
||||||
digitalWrite(OUTPUT_GDO, HIGH); // pull the line high for 1305 micros so the door opener responds to the message
|
digitalWrite(OUTPUT_GDO, HIGH); // pull the line high for 1305 micros so the
|
||||||
delayMicroseconds(1305);
|
// door opener responds to the message
|
||||||
digitalWrite(OUTPUT_GDO, LOW); // bring the line low
|
delayMicroseconds(1305);
|
||||||
|
digitalWrite(OUTPUT_GDO, LOW); // bring the line low
|
||||||
|
|
||||||
delayMicroseconds(1260); // "LOW" pulse duration before the message start
|
delayMicroseconds(1260); // "LOW" pulse duration before the message start
|
||||||
swSerial.write(payload, length);
|
swSerial.write(payload, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sync(){
|
void sync() {
|
||||||
if(!useRollingCodes) return;
|
if (!useRollingCodes)
|
||||||
|
return;
|
||||||
|
|
||||||
getRollingCode("reboot1");
|
getRollingCode("reboot1");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
|
|
||||||
getRollingCode("reboot2");
|
getRollingCode("reboot2");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
|
|
||||||
getRollingCode("reboot3");
|
getRollingCode("reboot3");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
|
|
||||||
getRollingCode("reboot4");
|
getRollingCode("reboot4");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
|
|
||||||
getRollingCode("reboot5");
|
getRollingCode("reboot5");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
|
|
||||||
getRollingCode("reboot6");
|
getRollingCode("reboot6");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
|
|
||||||
writeCounterToFlash();
|
writeCounterToFlash();
|
||||||
}
|
}
|
||||||
|
|
||||||
void openDoor(){
|
void openDoor() {
|
||||||
if(doorState == "open" || doorState == "opening"){
|
if (doorState == "open" || doorState == "opening") {
|
||||||
ESP_LOGD(TAG, "The door is already %s", doorState);
|
ESP_LOGD(TAG, "The door is already %s", doorState);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
doorState = "opening"; // It takes a couple of pulses to detect opening/closing. by setting here, we can avoid bouncing from rapidly repeated commands
|
doorState = "opening"; // It takes a couple of pulses to detect
|
||||||
|
// opening/closing. by setting here, we can avoid
|
||||||
|
// bouncing from rapidly repeated commands
|
||||||
|
|
||||||
if(useRollingCodes){
|
if (useRollingCodes) {
|
||||||
getRollingCode("door1");
|
getRollingCode("door1");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
|
|
||||||
delay(40);
|
delay(40);
|
||||||
|
|
||||||
getRollingCode("door2");
|
getRollingCode("door2");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
|
|
||||||
writeCounterToFlash();
|
writeCounterToFlash();
|
||||||
}else{
|
} else {
|
||||||
for(int i=0; i<4; i++){
|
for (int i = 0; i < 4; i++) {
|
||||||
ESP_LOGD(TAG, "sync_code[%d]", i);
|
ESP_LOGD(TAG, "sync_code[%d]", i);
|
||||||
|
|
||||||
transmit(SYNC_CODE[i],CODE_LENGTH);
|
transmit(SYNC_CODE[i], CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "door_code")
|
ESP_LOGD(TAG, "door_code")
|
||||||
transmit(DOOR_CODE,CODE_LENGTH);
|
transmit(DOOR_CODE, CODE_LENGTH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void closeDoor(){
|
void closeDoor() {
|
||||||
if(doorState == "closed" || doorState == "closing"){
|
if (doorState == "closed" || doorState == "closing") {
|
||||||
ESP_LOGD(TAG, "The door is already %s", doorState);
|
ESP_LOGD(TAG, "The door is already %s", doorState);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
doorState = "closing"; // It takes a couple of pulses to detect opening/closing. by setting here, we can avoid bouncing from rapidly repeated commands
|
doorState = "closing"; // It takes a couple of pulses to detect
|
||||||
|
// opening/closing. by setting here, we can avoid
|
||||||
|
// bouncing from rapidly repeated commands
|
||||||
|
|
||||||
if(useRollingCodes){
|
if (useRollingCodes) {
|
||||||
getRollingCode("door1");
|
getRollingCode("door1");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
|
|
||||||
delay(40);
|
delay(40);
|
||||||
|
|
||||||
getRollingCode("door2");
|
getRollingCode("door2");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
|
|
||||||
writeCounterToFlash();
|
|
||||||
}else{
|
|
||||||
for(int i=0; i<4; i++){
|
|
||||||
ESP_LOGD(TAG, "sync_code[%d]", i);
|
|
||||||
|
|
||||||
|
writeCounterToFlash();
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
ESP_LOGD(TAG, "sync_code[%d]", i);
|
||||||
|
|
||||||
transmit(SYNC_CODE[i],CODE_LENGTH);
|
transmit(SYNC_CODE[i], CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "door_code")
|
ESP_LOGD(TAG, "door_code")
|
||||||
transmit(DOOR_CODE,CODE_LENGTH);
|
transmit(DOOR_CODE, CODE_LENGTH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void toggleLight(){
|
void toggleLight() {
|
||||||
if(useRollingCodes){
|
if (useRollingCodes) {
|
||||||
getRollingCode("light");
|
getRollingCode("light");
|
||||||
transmit(rollingCode,CODE_LENGTH);
|
transmit(rollingCode, CODE_LENGTH);
|
||||||
writeCounterToFlash();
|
writeCounterToFlash();
|
||||||
}else{
|
} else {
|
||||||
for(int i=0; i<4; i++){
|
for (int i = 0; i < 4; i++) {
|
||||||
ESP_LOGD(TAG, "sync_code[%d]", i);
|
ESP_LOGD(TAG, "sync_code[%d]", i);
|
||||||
|
|
||||||
transmit(SYNC_CODE[i],CODE_LENGTH);
|
transmit(SYNC_CODE[i], CODE_LENGTH);
|
||||||
delay(45);
|
delay(45);
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "light_code")
|
ESP_LOGD(TAG, "light_code")
|
||||||
transmit(LIGHT_CODE,CODE_LENGTH);
|
transmit(LIGHT_CODE, CODE_LENGTH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,63 +14,82 @@
|
||||||
#ifndef _RATGDO_H
|
#ifndef _RATGDO_H
|
||||||
#define _RATGDO_H
|
#define _RATGDO_H
|
||||||
|
|
||||||
|
|
||||||
#include "BootstrapManager.h" // Must use the https://github.com/PaulWieland/arduinoImprovBootstrapper fork, ratgdo branch
|
#include "BootstrapManager.h" // Must use the https://github.com/PaulWieland/arduinoImprovBootstrapper fork, ratgdo branch
|
||||||
#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial
|
#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial
|
||||||
#include "rolling_code.h"
|
|
||||||
#include "home_assistant.h"
|
#include "home_assistant.h"
|
||||||
|
#include "rolling_code.h"
|
||||||
|
|
||||||
SoftwareSerial swSerial;
|
SoftwareSerial swSerial;
|
||||||
|
|
||||||
/********************************** BOOTSTRAP MANAGER *****************************************/
|
/********************************** BOOTSTRAP MANAGER
|
||||||
|
* *****************************************/
|
||||||
BootstrapManager bootstrapManager;
|
BootstrapManager bootstrapManager;
|
||||||
|
|
||||||
/********************************** PIN DEFINITIONS *****************************************/
|
/********************************** PIN DEFINITIONS
|
||||||
#define OUTPUT_GDO D4 // red control terminal / GarageDoorOpener (UART1 TX) pin is D4 on D1 Mini
|
* *****************************************/
|
||||||
#define TRIGGER_OPEN D5 // dry contact for opening door
|
#define OUTPUT_GDO \
|
||||||
|
D4 // red control terminal / GarageDoorOpener (UART1 TX) pin is D4 on D1 Mini
|
||||||
|
#define TRIGGER_OPEN D5 // dry contact for opening door
|
||||||
#define TRIGGER_CLOSE D6 // dry contact for closing door
|
#define TRIGGER_CLOSE D6 // dry contact for closing door
|
||||||
#define TRIGGER_LIGHT D3 // dry contact for triggering light (no discrete light commands, so toggle only)
|
#define TRIGGER_LIGHT \
|
||||||
|
D3 // dry contact for triggering light (no discrete light commands, so toggle
|
||||||
|
// only)
|
||||||
#define STATUS_DOOR D0 // output door status, HIGH for open, LOW for closed
|
#define STATUS_DOOR D0 // output door status, HIGH for open, LOW for closed
|
||||||
#define STATUS_OBST D8 // output for obstruction status, HIGH for obstructed, LOW for clear
|
#define STATUS_OBST \
|
||||||
#define INPUT_RPM1 D1 // RPM1 rotary encoder input OR reed switch if not soldering to the door opener logic board
|
D8 // output for obstruction status, HIGH for obstructed, LOW for clear
|
||||||
#define INPUT_RPM2 D2 // RPM2 rotary encoder input OR not used if using reed switch
|
#define INPUT_RPM1 \
|
||||||
|
D1 // RPM1 rotary encoder input OR reed switch if not soldering to the door
|
||||||
|
// opener logic board
|
||||||
|
#define INPUT_RPM2 \
|
||||||
|
D2 // RPM2 rotary encoder input OR not used if using reed switch
|
||||||
#define INPUT_OBST D7 // black obstruction sensor terminal
|
#define INPUT_OBST D7 // black obstruction sensor terminal
|
||||||
|
|
||||||
|
/********************************** MQTT TOPICS
|
||||||
/********************************** MQTT TOPICS *****************************************/
|
* *****************************************/
|
||||||
String doorCommandTopic = ""; // will be mqttTopicPrefix/deviceName/command
|
String doorCommandTopic = ""; // will be mqttTopicPrefix/deviceName/command
|
||||||
String setCounterTopic = ""; // will be mqttTopicPrefix/deviceName/set_code_counter
|
String setCounterTopic =
|
||||||
|
""; // will be mqttTopicPrefix/deviceName/set_code_counter
|
||||||
|
|
||||||
String doorCommand = ""; // will be [open|close|light]
|
String doorCommand = ""; // will be [open|close|light]
|
||||||
String overallStatusTopic = ""; // legacy from 1.0. Will be mqttTopicPrefix/deviceName/status
|
String overallStatusTopic =
|
||||||
|
""; // legacy from 1.0. Will be mqttTopicPrefix/deviceName/status
|
||||||
String availabilityStatusTopic = ""; // online|offline
|
String availabilityStatusTopic = ""; // online|offline
|
||||||
String obstructionStatusTopic = ""; // obstructed|clear
|
String obstructionStatusTopic = ""; // obstructed|clear
|
||||||
String doorStatusTopic = ""; // open|opening|closing|closed|reed_open|reed_closed
|
String doorStatusTopic =
|
||||||
String rollingCodeTopic = ""; // broadcast the current rolling code count for debugging purposes
|
""; // open|opening|closing|closed|reed_open|reed_closed
|
||||||
|
String rollingCodeTopic =
|
||||||
|
""; // broadcast the current rolling code count for debugging purposes
|
||||||
|
|
||||||
/********************************** GLOBAL VARS *****************************************/
|
/********************************** GLOBAL VARS
|
||||||
|
* *****************************************/
|
||||||
bool setupComplete = false;
|
bool setupComplete = false;
|
||||||
unsigned int rollingCodeCounter;
|
unsigned int rollingCodeCounter;
|
||||||
byte rollingCode[CODE_LENGTH];
|
byte rollingCode[CODE_LENGTH];
|
||||||
String doorState = "unknown"; // will be [online|offline|opening|open|closing|closed|obstructed|clear|reed_open|reed_closed]
|
String doorState =
|
||||||
|
"unknown"; // will be
|
||||||
|
// [online|offline|opening|open|closing|closed|obstructed|clear|reed_open|reed_closed]
|
||||||
|
|
||||||
unsigned int obstructionLowCount = 0; // count obstruction low pulses
|
unsigned int obstructionLowCount = 0; // count obstruction low pulses
|
||||||
unsigned long lastObstructionHigh = 0; // count time between high pulses from the obst ISR
|
unsigned long lastObstructionHigh =
|
||||||
|
0; // count time between high pulses from the obst ISR
|
||||||
|
|
||||||
bool doorIsObstructed = false;
|
bool doorIsObstructed = false;
|
||||||
bool dryContactDoorOpen = false;
|
bool dryContactDoorOpen = false;
|
||||||
bool dryContactDoorClose = false;
|
bool dryContactDoorClose = false;
|
||||||
bool dryContactToggleLight = false;
|
bool dryContactToggleLight = false;
|
||||||
int doorPositionCounter = 0; // calculate the door's movement and position
|
int doorPositionCounter = 0; // calculate the door's movement and position
|
||||||
bool rpm1Pulsed = false; // did rpm1 get a pulse or not - eliminates an issue when the sensor is parked on a high pulse which fires rpm2 isr
|
bool rpm1Pulsed =
|
||||||
|
false; // did rpm1 get a pulse or not - eliminates an issue when the sensor
|
||||||
|
// is parked on a high pulse which fires rpm2 isr
|
||||||
|
|
||||||
/********************************** FUNCTION DECLARATION *****************************************/
|
/********************************** FUNCTION DECLARATION
|
||||||
|
* *****************************************/
|
||||||
void callback(char *topic, byte *payload, unsigned int length);
|
void callback(char *topic, byte *payload, unsigned int length);
|
||||||
void manageDisconnections();
|
void manageDisconnections();
|
||||||
void manageQueueSubscription();
|
void manageQueueSubscription();
|
||||||
void manageHardwareButton();
|
void manageHardwareButton();
|
||||||
|
|
||||||
void transmit(byte* payload, unsigned int length);
|
void transmit(byte *payload, unsigned int length);
|
||||||
void sync();
|
void sync();
|
||||||
void openDoor();
|
void openDoor();
|
||||||
void closeDoor();
|
void closeDoor();
|
||||||
|
@ -85,7 +104,8 @@ void sendDoorStatus();
|
||||||
void doorStateLoop();
|
void doorStateLoop();
|
||||||
void dryContactLoop();
|
void dryContactLoop();
|
||||||
|
|
||||||
/********************************** INTERRUPT SERVICE ROUTINES ***********************************/
|
/********************************** INTERRUPT SERVICE ROUTINES
|
||||||
|
* ***********************************/
|
||||||
void IRAM_ATTR isrDebounce(const char *type);
|
void IRAM_ATTR isrDebounce(const char *type);
|
||||||
void IRAM_ATTR isrDoorOpen();
|
void IRAM_ATTR isrDoorOpen();
|
||||||
void IRAM_ATTR isrDoorClose();
|
void IRAM_ATTR isrDoorClose();
|
||||||
|
@ -95,14 +115,20 @@ void IRAM_ATTR isrRPM1();
|
||||||
void IRAM_ATTR isrRPM2();
|
void IRAM_ATTR isrRPM2();
|
||||||
|
|
||||||
/*** Static Codes ***/
|
/*** Static Codes ***/
|
||||||
byte SYNC1[] = {0x55,0x01,0x00,0x61,0x12,0x49,0x2c,0x92,0x5b,0x24,0x96,0x86,0x0b,0x65,0x96,0xd9,0x8f,0x26,0x4a};
|
byte SYNC1[] = {0x55, 0x01, 0x00, 0x61, 0x12, 0x49, 0x2c, 0x92, 0x5b, 0x24,
|
||||||
byte SYNC2[] = {0x55,0x01,0x00,0x08,0x34,0x93,0x49,0xb4,0x92,0x4d,0x20,0x26,0x1b,0x4d,0xb4,0xdb,0xad,0x76,0x93};
|
0x96, 0x86, 0x0b, 0x65, 0x96, 0xd9, 0x8f, 0x26, 0x4a};
|
||||||
byte SYNC3[] = {0x55,0x01,0x00,0x06,0x1b,0x2c,0xbf,0x4b,0x6d,0xb6,0x4b,0x18,0x20,0x92,0x09,0x20,0xf2,0x11,0x2c};
|
byte SYNC2[] = {0x55, 0x01, 0x00, 0x08, 0x34, 0x93, 0x49, 0xb4, 0x92, 0x4d,
|
||||||
byte SYNC4[] = {0x55,0x01,0x00,0x95,0x29,0x36,0x91,0x29,0x36,0x9a,0x69,0x05,0x2f,0xbe,0xdf,0x6d,0x16,0xcb,0xe7};
|
0x20, 0x26, 0x1b, 0x4d, 0xb4, 0xdb, 0xad, 0x76, 0x93};
|
||||||
byte* SYNC_CODE[] = {SYNC1,SYNC2,SYNC3,SYNC4};
|
byte SYNC3[] = {0x55, 0x01, 0x00, 0x06, 0x1b, 0x2c, 0xbf, 0x4b, 0x6d, 0xb6,
|
||||||
|
0x4b, 0x18, 0x20, 0x92, 0x09, 0x20, 0xf2, 0x11, 0x2c};
|
||||||
|
byte SYNC4[] = {0x55, 0x01, 0x00, 0x95, 0x29, 0x36, 0x91, 0x29, 0x36, 0x9a,
|
||||||
|
0x69, 0x05, 0x2f, 0xbe, 0xdf, 0x6d, 0x16, 0xcb, 0xe7};
|
||||||
|
byte *SYNC_CODE[] = {SYNC1, SYNC2, SYNC3, SYNC4};
|
||||||
|
|
||||||
byte DOOR_CODE[] = {0x55,0x01,0x00,0x94,0x3f,0xef,0xbc,0xfb,0x7f,0xbe,0xfc,0xa6,0x1a,0x4d,0xa6,0xda,0x8d,0x36,0xb3};
|
byte DOOR_CODE[] = {0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb, 0x7f, 0xbe,
|
||||||
|
0xfc, 0xa6, 0x1a, 0x4d, 0xa6, 0xda, 0x8d, 0x36, 0xb3};
|
||||||
|
|
||||||
byte LIGHT_CODE[] = {0x55,0x01,0x00,0x94,0x3f,0xef,0xbc,0xfb,0x7f,0xbe,0xff,0xa6,0x1a,0x4d,0xa6,0xda,0x8d,0x76,0xb1};
|
byte LIGHT_CODE[] = {0x55, 0x01, 0x00, 0x94, 0x3f, 0xef, 0xbc, 0xfb, 0x7f, 0xbe,
|
||||||
|
0xff, 0xa6, 0x1a, 0x4d, 0xa6, 0xda, 0x8d, 0x76, 0xb1};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -1,97 +1,99 @@
|
||||||
#include "common.h"
|
|
||||||
#include "rolling_code.h"
|
#include "rolling_code.h"
|
||||||
|
#include "common.h"
|
||||||
#include "secplus.h"
|
#include "secplus.h"
|
||||||
|
|
||||||
void readCounterFromFlash(){
|
void readCounterFromFlash() {
|
||||||
//Open the file
|
// Open the file
|
||||||
File file = LittleFS.open("/rollingcode.txt", "r");
|
File file = LittleFS.open("/rollingcode.txt", "r");
|
||||||
|
|
||||||
//Check if the file exists
|
// Check if the file exists
|
||||||
if(!file){
|
if (!file) {
|
||||||
Serial.println("rollingcode.txt doesn't exist. creating...");
|
Serial.println("rollingcode.txt doesn't exist. creating...");
|
||||||
|
|
||||||
writeCounterToFlash();
|
writeCounterToFlash();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
rollingCodeCounter = file.parseInt();
|
rollingCodeCounter = file.parseInt();
|
||||||
|
|
||||||
//Close the file
|
// Close the file
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeCounterToFlash(){
|
void writeCounterToFlash() {
|
||||||
//Open the file
|
// Open the file
|
||||||
File file = LittleFS.open("/rollingcode.txt", "w");
|
File file = LittleFS.open("/rollingcode.txt", "w");
|
||||||
|
|
||||||
//Write to the file
|
// Write to the file
|
||||||
file.print(rollingCodeCounter);
|
file.print(rollingCodeCounter);
|
||||||
delay(1);
|
delay(1);
|
||||||
//Close the file
|
// Close the file
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
Serial.println("Write successful");
|
Serial.println("Write successful");
|
||||||
}
|
}
|
||||||
|
|
||||||
void getRollingCode(const char *command){
|
void getRollingCode(const char *command) {
|
||||||
Serial.print("rolling code for ");
|
Serial.print("rolling code for ");
|
||||||
Serial.print(rollingCodeCounter);
|
Serial.print(rollingCodeCounter);
|
||||||
Serial.print("|");
|
Serial.print("|");
|
||||||
Serial.print(command);
|
Serial.print(command);
|
||||||
Serial.print(" : ");
|
Serial.print(" : ");
|
||||||
|
|
||||||
uint64_t id = 0x539;
|
uint64_t id = 0x539;
|
||||||
uint64_t fixed = 0;
|
uint64_t fixed = 0;
|
||||||
uint32_t data = 0;
|
uint32_t data = 0;
|
||||||
|
|
||||||
if(strcmp(command,"reboot1") == 0){
|
if (strcmp(command, "reboot1") == 0) {
|
||||||
fixed = 0x400000000;
|
fixed = 0x400000000;
|
||||||
data = 0x0000618b;
|
data = 0x0000618b;
|
||||||
}else if(strcmp(command,"reboot2") == 0){
|
} else if (strcmp(command, "reboot2") == 0) {
|
||||||
fixed = 0;
|
fixed = 0;
|
||||||
data = 0x01009080;
|
data = 0x01009080;
|
||||||
}else if(strcmp(command,"reboot3") == 0){
|
} else if (strcmp(command, "reboot3") == 0) {
|
||||||
fixed = 0;
|
fixed = 0;
|
||||||
data = 0x0000b1a0;
|
data = 0x0000b1a0;
|
||||||
}else if(strcmp(command,"reboot4") == 0){
|
} else if (strcmp(command, "reboot4") == 0) {
|
||||||
fixed = 0;
|
fixed = 0;
|
||||||
data = 0x01009080;
|
data = 0x01009080;
|
||||||
}else if(strcmp(command,"reboot5") == 0){
|
} else if (strcmp(command, "reboot5") == 0) {
|
||||||
fixed = 0x300000000;
|
fixed = 0x300000000;
|
||||||
data = 0x00008092;
|
data = 0x00008092;
|
||||||
}else if(strcmp(command,"reboot6") == 0){
|
} else if (strcmp(command, "reboot6") == 0) {
|
||||||
fixed = 0x300000000;
|
fixed = 0x300000000;
|
||||||
data = 0x00008092;
|
data = 0x00008092;
|
||||||
}else if(strcmp(command,"door1") == 0){
|
} else if (strcmp(command, "door1") == 0) {
|
||||||
fixed = 0x200000000;
|
fixed = 0x200000000;
|
||||||
data = 0x01018280;
|
data = 0x01018280;
|
||||||
}else if(strcmp(command,"door2") == 0){
|
} else if (strcmp(command, "door2") == 0) {
|
||||||
fixed = 0x200000000;
|
fixed = 0x200000000;
|
||||||
data = 0x01009280;
|
data = 0x01009280;
|
||||||
}else if(strcmp(command,"light") == 0){
|
} else if (strcmp(command, "light") == 0) {
|
||||||
fixed = 0x200000000;
|
fixed = 0x200000000;
|
||||||
data = 0x00009281;
|
data = 0x00009281;
|
||||||
}else{
|
} else {
|
||||||
Serial.println("ERROR: Invalid command");
|
Serial.println("ERROR: Invalid command");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fixed = fixed | id;
|
fixed = fixed | id;
|
||||||
|
|
||||||
encode_wireline(rollingCodeCounter, fixed, data, rollingCode);
|
encode_wireline(rollingCodeCounter, fixed, data, rollingCode);
|
||||||
|
|
||||||
printRollingCode();
|
printRollingCode();
|
||||||
|
|
||||||
if(strcmp(command,"door1") != 0){ // door2 is created with same counter and should always be called after door1
|
if (strcmp(command, "door1") != 0) { // door2 is created with same counter and
|
||||||
rollingCodeCounter = (rollingCodeCounter + 1) & 0xfffffff;
|
// should always be called after door1
|
||||||
}
|
rollingCodeCounter = (rollingCodeCounter + 1) & 0xfffffff;
|
||||||
return;
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printRollingCode(){
|
void printRollingCode() {
|
||||||
for(int i = 0; i < CODE_LENGTH; i++){
|
for (int i = 0; i < CODE_LENGTH; i++) {
|
||||||
if(rollingCode[i] <= 0x0f) Serial.print("0");
|
if (rollingCode[i] <= 0x0f)
|
||||||
Serial.print(rollingCode[i],HEX);
|
Serial.print("0");
|
||||||
}
|
Serial.print(rollingCode[i], HEX);
|
||||||
Serial.println("");
|
}
|
||||||
|
Serial.println("");
|
||||||
}
|
}
|
|
@ -1,18 +1,22 @@
|
||||||
#ifndef _RATGDO_ROLLING_CODE_H
|
#ifndef _RATGDO_ROLLING_CODE_H
|
||||||
#define _RATGDO_ROLLING_CODE_H
|
#define _RATGDO_ROLLING_CODE_H
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <LittleFS.h>
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include "BootstrapManager.h"
|
#include "BootstrapManager.h"
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <LittleFS.h>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "secplus.h"
|
#include "secplus.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
void readCounterFromFlash(); // get the rolling code counter from setup.json & return it
|
void readCounterFromFlash(); // get the rolling code counter from setup.json &
|
||||||
void writeCounterToFlash(); // write the counter back to setup.json
|
// return it
|
||||||
void getRollingCode(const char *command); // get the next rolling code for type [reboot1,reboot2,reboot3,reboot4,reboot5,door1,light]
|
void writeCounterToFlash(); // write the counter back to setup.json
|
||||||
|
void getRollingCode(
|
||||||
|
const char
|
||||||
|
*command); // get the next rolling code for type
|
||||||
|
// [reboot1,reboot2,reboot3,reboot4,reboot5,door1,light]
|
||||||
void printRollingCode();
|
void printRollingCode();
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue