Skip to content

Commit

Permalink
Merge pull request #18 from gdoor-org/feature/ha-discovery
Browse files Browse the repository at this point in the history
Add: MQTT HomeAssistant auto discovery feature.
  • Loading branch information
DaSchaef authored Jun 16, 2024
2 parents e4702a3 + 7ca95ad commit 4a99174
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 58 deletions.
35 changes: 25 additions & 10 deletions firmware/esp32/gdoor/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -20,6 +20,8 @@
#include "src/wifi_helper.h"
#include "src/printer_helper.h"

GDOOR_DATA_PROTOCOL gdoor_data_idle(NULL, true);

boolean debug = false; // Global variable to indicate if we are in debug mode (true)
const char* mqtt_topic_bus_rx = NULL;

Expand Down Expand Up @@ -48,8 +50,8 @@ boolean parse(String &input) {
* outputs valid bus messages.
* @param busmessage The bus message to be send out to the user.
*/
void output(GDOOR_DATA_PROTOCOL &busmessage, const char* topic) {
if(debug || busmessage.raw->valid) {
void output(GDOOR_DATA_PROTOCOL &busmessage, const char* topic, bool force=false) {
if(force || debug || (busmessage.raw != NULL && busmessage.raw->valid)) {
MQTT_HELPER::printer.print("{");
MQTT_HELPER::printer.print(busmessage);
MQTT_HELPER::printer.println("}");
Expand All @@ -60,19 +62,23 @@ void output(GDOOR_DATA_PROTOCOL &busmessage, const char* topic) {
void setup() {
Serial.begin(115200);
Serial.setTimeout(1);
JSONDEBUG("GDOOR Setup start");
JSONDEBUG("GDoor Setup start");

WIFI_HELPER::setup();
MQTT_HELPER::setup(WIFI_HELPER::mqtt_server(),
WIFI_HELPER::mqtt_port(),
WIFI_HELPER::mqtt_user(),
WIFI_HELPER::mqtt_password(),
WIFI_HELPER::mqtt_topic_bus_tx(),
WIFI_HELPER::mqtt_topic_bus_rx());

GDOOR::setRxThreshold(PIN_RX_THRESH, WIFI_HELPER::rx_sensitivity());
GDOOR::setup(PIN_TX, PIN_TX_EN, WIFI_HELPER::rx_pin());

MQTT_HELPER::setup(WIFI_HELPER::mqtt_server(), WIFI_HELPER::mqtt_port(), WIFI_HELPER::mqtt_user(), WIFI_HELPER::mqtt_password(), WIFI_HELPER::mqtt_topic_bus_tx());

mqtt_topic_bus_rx = WIFI_HELPER::mqtt_topic_bus_rx();
debug = WIFI_HELPER::debug();

JSONDEBUG("GDOOR Setup done");
JSONDEBUG("GDoor Setup done");
JSONDEBUG("RX Pin: ");
JSONDEBUG(WIFI_HELPER::rx_pin());
JSONDEBUG("RX Sensitivity: ");
Expand All @@ -84,19 +90,28 @@ void loop() {
MQTT_HELPER::loop();
GDOOR::loop();
GDOOR_DATA* rx_data = GDOOR::read();

// Output bus idle message on new MQTT connections to set a defined state
if(MQTT_HELPER::isNewConnection()) {
output(gdoor_data_idle, mqtt_topic_bus_rx, true);
}
if(rx_data != NULL) {
JSONDEBUG("Received data from bus");
GDOOR_DATA_PROTOCOL busmessage = GDOOR_DATA_PROTOCOL(rx_data);
output(busmessage, mqtt_topic_bus_rx);
JSONDEBUG("Output bus data via Serial and MQTT, done");
JSONDEBUG("Output bus data via Serial and MQTT, done");
// Output idle message after bus message, to reset values so that
//home automation can trigger again
output(gdoor_data_idle, mqtt_topic_bus_rx, true);

} else if (!GDOOR::active()) { // Neither RX nor TX active,
String str_received("");
if (Serial.available() > 0) { // let's check the serial port if something is in buffer
str_received = Serial.readString();
str_received.trim();
} else {
str_received = MQTT_HELPER::receive();
}
str_received.trim();

if(str_received.length() > 0) {
if(!parse(str_received)) { //Check if received string is a command
Expand Down
15 changes: 15 additions & 0 deletions firmware/esp32/gdoor/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "GDoor",
"version": "dev",
"builds": [
{
"chipFamily": "ESP32",
"parts": [
{
"path": "firmware_merged.bin",
"offset": 0
}
]
}
]
}
2 changes: 1 addition & 1 deletion firmware/esp32/gdoor/merge_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def merge_bin(source, target, env):
)

# Add a post action that runs esptoolpy to merge available flash images
env.AddPostAction(APP_BIN , merge_bin)
env.AddPostAction(APP_BIN, merge_bin)
9 changes: 5 additions & 4 deletions firmware/esp32/gdoor/src/defines.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -17,7 +17,8 @@
#ifndef DEFINES_H
#define DEFINES_H

// GDOOR
// GDoor
#define GDOOR_VERSION "dev"
#define MAX_WORDLEN 25

// RX Statemachine
Expand All @@ -40,7 +41,7 @@
#define STATE_SENDING 0x01

// WIFI
#define DEFAULT_WIFI_SSID "GDOOR"
#define DEFAULT_WIFI_SSID "GDoor"
#define DEFAULT_WIFI_PASSWORD "12345678"

// MQTT
Expand Down
6 changes: 3 additions & 3 deletions firmware/esp32/gdoor/src/gdoor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -18,7 +18,7 @@

namespace GDOOR {
/*
* Setup everything needed for GDOOR.
* Setup everything needed for GDoor.
* @param int txpin Pin number where PWM is created when sending out data
* @param int txenpin Pin number where output buffer is turned on/off
* @param int rxpin Pin number where pulses from bus are received
Expand Down
4 changes: 2 additions & 2 deletions firmware/esp32/gdoor/src/gdoor.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
19 changes: 12 additions & 7 deletions firmware/esp32/gdoor/src/gdoor_data.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -131,11 +131,16 @@ bool GDOOR_DATA::parse(uint16_t *counts, uint16_t len) {
* and stores a human readable form in its class elements.
*
* @param data GDOOR_DATA element, with parsed bus data.
* @param idle true: generate idle message
*/
GDOOR_DATA_PROTOCOL::GDOOR_DATA_PROTOCOL(GDOOR_DATA* data) {
this->type = "TYPE_UNKOWN";
this->action = "ACTION_UNKOWN";

GDOOR_DATA_PROTOCOL::GDOOR_DATA_PROTOCOL(GDOOR_DATA* data, bool idle) {
if(idle) {
this->type = "TYPE_GDOOR";
this->action = "BUS_IDLE";
} else {
this->type = "TYPE_UNKOWN";
this->action = "ACTION_UNKOWN";
}
this->raw = data;

this->source[0] = 0x00;
Expand All @@ -149,7 +154,7 @@ GDOOR_DATA_PROTOCOL::GDOOR_DATA_PROTOCOL(GDOOR_DATA* data) {
this->parameters[0] = 0x00;
this->parameters[1] = 0x00;

if(data->valid && data->len >= 9) {
if(data != NULL && data->valid && data->len >= 9) {
if(GDOOR_DATA_HWTYPE.find(data->data[8]) != GDOOR_DATA_HWTYPE.end()){
this->type = GDOOR_DATA_HWTYPE.at(data->data[8]);
}
Expand Down
6 changes: 3 additions & 3 deletions firmware/esp32/gdoor/src/gdoor_data.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -59,7 +59,7 @@ class GDOOR_DATA_PROTOCOL : public Printable { // Class/Struct to collect bus hi
uint8_t source[3];
uint8_t destination[3];

GDOOR_DATA_PROTOCOL(GDOOR_DATA* data);
GDOOR_DATA_PROTOCOL(GDOOR_DATA* data, bool idle = false);

virtual size_t printTo(Print& p) const {
size_t r = 0;
Expand Down
6 changes: 3 additions & 3 deletions firmware/esp32/gdoor/src/gdoor_rx.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -103,7 +103,7 @@ namespace GDOOR_RX {


/*
* Function called by user to setup everything needed for GDOOR.
* Function called by user to setup everything needed for GDoor.
* @param int rxpin Pin number where pulses from bus are received
*/
void setup(uint8_t rxpin) {
Expand Down
4 changes: 2 additions & 2 deletions firmware/esp32/gdoor/src/gdoor_rx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
6 changes: 3 additions & 3 deletions firmware/esp32/gdoor/src/gdoor_tx.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -130,7 +130,7 @@ namespace GDOOR_TX {
}

/*
* Function called by user to setup everything needed for GDOOR.
* Function called by user to setup everything needed for GDoor.
* @param int txpin Pin number where PWM is created when sending out data
* @param int txenpin Pin number where output buffer is turned on/off
*/
Expand Down
4 changes: 2 additions & 2 deletions firmware/esp32/gdoor/src/gdoor_tx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
4 changes: 2 additions & 2 deletions firmware/esp32/gdoor/src/gdoor_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
4 changes: 2 additions & 2 deletions firmware/esp32/gdoor/src/gdoor_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of the GDOOR distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDOOR Authors.
* This file is part of the GDoor distribution (https://github.com/gdoor-org).
* Copyright (c) 2024 GDoor authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
Loading

0 comments on commit 4a99174

Please sign in to comment.