Skip to content

Commit

Permalink
Merge pull request #54 from Gifford47/development
Browse files Browse the repository at this point in the history
Maintenance PR
  • Loading branch information
Gifford47 authored Jan 12, 2024
2 parents 04f1679 + eac6632 commit edda64c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 65 deletions.
68 changes: 33 additions & 35 deletions HCPBridgeESP32/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ default_envs = esp32

[common]
dht22libs =
adafruit/DHT sensor library@^1.4.4
bmelibs =
adafruit/DHT sensor library@^1.4.4
bmelibs =
adafruit/Adafruit BME280 Library
ds18x20libs =
milesburton/DallasTemperature
Expand All @@ -31,14 +31,16 @@ board_build.flash_mode = qio
monitor_speed = 9600
monitor_filters = esp32_exception_decoder
lib_ldf_mode = deep+
lib_deps =
lib_deps =
esphome/ESPAsyncWebServer-esphome
https://github.com/HeMan/AsyncTCP#CI-and-fix ;async tcp issue fix 2.1.0
bblanchon/ArduinoJson
plerup/EspSoftwareSerial
ayushsharma82/AsyncElegantOTA
ottowinter/AsyncMqttClient-esphome
paulstoffregen/OneWire
https://github.com/emelianov/modbus-esp8266
emelianov/modbus-esp8266
https://github.com/dok-net/ghostl ; << Fix missing circular_queue.h
extra_scripts = pre:../WebUI/webpage/compress.py
;build_flags =
; -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
Expand All @@ -47,66 +49,62 @@ extra_scripts = pre:../WebUI/webpage/compress.py
[env:esp32]

[env:esp32_dht22]
lib_deps = ${env:esp32.lib_deps} ${common.dht22libs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_DHT22
lib_deps = ${env:esp32.lib_deps} ${common.dht22libs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_DHT22

[env:esp32s3_tynet]
board = adafruit_feather_esp32s3
board_build.f_flash = 40000000L
build_flags = ${env:esp32.build_flags}
build_flags = ${env:esp32.build_flags}

[env:esp32s3_tynet_DS18X20]
extends = env:esp32s3_tynet
lib_deps = ${env:esp32.lib_deps} ${common.ds18x20libs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_DS18X20
lib_deps = ${env:esp32.lib_deps} ${common.ds18x20libs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_DS18X20

[env:esp32s3_tynet_BME280]
extends = env:esp32s3_tynet
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME

[env:esp32s3_tynet_HCSR04]
extends = env:esp32s3_tynet
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_HCSR04
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_HCSR04

[env:esp32s3_tynet_BME280_HCSR04]
extends = env:esp32s3_tynet
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME -D USE_HCSR04
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME -D USE_HCSR04


;ESP32 DevKit
[env:DS18X20]
lib_deps = ${env:esp32.lib_deps} ${common.ds18x20libs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_DS18X20
lib_deps = ${env:esp32.lib_deps} ${common.ds18x20libs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_DS18X20

[env:BME280]
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME

[env:HCSR04]
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_HCSR04
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_HCSR04

[env:BME280_HCSR04]
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME -D USE_HCSR04

lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_BME -D USE_HCSR04

[env:HCSR501]
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_HCSR501
build_flags = ${env:esp32.build_flags} -D SENSORS -D USE_HCSR501

[env:m5stack-stamps3]
platform_packages = framework-arduinoespressif32 @ https://github.com/bsergei/arduino-esp32.git#issue-8185
board = m5stack-stamps3
build_flags = ${env:esp32.build_flags} -D M5STACK
board = m5stack-stamps3
build_flags = ${env:esp32.build_flags} -D M5STACK

[env:m5stack-stamps3_BME280]
extends = env:m5stack-stamps3
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:m5stack-stamps3.build_flags} -D SENSORS -D USE_BME

[env:m5stack-stamps3_BME280_HCSR04]
extends = env:m5stack-stamps3
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:m5stack-stamps3.build_flags} -D SENSORS -D USE_BME -D USE_HCSR04
extends = env:m5stack-stamps3_BME280
build_flags = ${env:m5stack-stamps3_BME280.build_flags} -D USE_HCSR04

[env:m5stack-stamps3_BME280]
extends = env:m5stack-stamps3
lib_deps = ${env:esp32.lib_deps} ${common.bmelibs}
build_flags = ${env:m5stack-stamps3.build_flags} -D SENSORS -D USE_BME

2 changes: 1 addition & 1 deletion HCPBridgeESP32/src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@
const char *HA_ONLINE = "online";
const char *HA_OFFLINE = "offline";
#endif
const char *HA_VERSION = "0.0.6.0";
const char *HA_VERSION = "0.0.6.1";

#endif
2 changes: 1 addition & 1 deletion HCPBridgeESP32/src/hoermann.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
}

String toStatusJson() {
DynamicJsonDocument root(1024);
JsonDocument root;
root["valid"] = this->isValid();
root["targetPosition"] = (int)(this->targetPosition*100);
root["currentPosition"] = (int)(this->currentPosition*100);
Expand Down
65 changes: 39 additions & 26 deletions HCPBridgeESP32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ AsyncWebServer server(80);

#ifdef SENSORS
int sensor_prox_tresh = 0;
double sensor_temp_thresh = 0;
float sensor_temp_thresh = 0;
int sensor_hum_thresh = 0;
int sensor_pres_thresh = 0;
int sensor_last_update = 0;
int sensor_force_update_intervall = 7200000; // force sending sensor updates after amount of time since last update
#endif


Expand Down Expand Up @@ -158,7 +160,7 @@ void setuptMqttStrings(){
mqttStrings.st_cmd_topic_var = mqttStrings.st_cmd_topic + "/%s";
mqttStrings.st_cmd_topic_subs = mqttStrings.st_cmd_topic + "/#";
mqttStrings.st_pos_topic = ftopic + "/position";
mqttStrings.st_setpos_topic = ftopic + "/set_position";
mqttStrings.st_setpos_topic = mqttStrings.st_cmd_topic + "/set_position";
mqttStrings.st_lamp_topic = mqttStrings.st_cmd_topic + "/lamp";
mqttStrings.st_door_topic = mqttStrings.st_cmd_topic + "/door";
mqttStrings.st_vent_topic = mqttStrings.st_cmd_topic + "/vent";
Expand Down Expand Up @@ -278,7 +280,7 @@ void updateDoorStatus(bool forceUpate = false)
// onyl send updates when state changed
if (hoermannEngine->state->changed || forceUpate){
hoermannEngine->state->clearChanged();
DynamicJsonDocument doc(1024);
JsonDocument doc;
char payload[1024];
const char *venting = HA_CLOSE;

Expand All @@ -299,23 +301,28 @@ void updateDoorStatus(bool forceUpate = false)
mqttClient.publish(mqttStrings.pos_topic, 1, true, payload);
}
}

void updateSensors(bool forceUpate = false){
#ifdef SENSORS
if (millis()-sensor_last_update >= sensor_force_update_intervall) {
forceUpate = true;
}

if (new_sensor_data || forceUpate) {
new_sensor_data = false;
DynamicJsonDocument doc(1024); //2048 needed because of BME280 float values!
JsonDocument doc; //2048 needed because of BME280 float values!
char payload[1024];
char buf[20];
#ifdef USE_DS18X20
dtostrf(ds18x20_temp,2,2,buf); // convert to string
dtostrf(ds18x20_temp,2,1,buf); // convert to string
//Serial.println("Temp: "+ (String)buf);
doc["temp"] = buf;
#endif
#ifdef USE_BME
dtostrf(bme_temp,2,2,buf); // convert to string
dtostrf(bme_temp,2,1,buf); // convert to string
//Serial.println("Temp: "+ (String)buf);
doc["temp"] = buf;
dtostrf(bme_hum,2,2,buf); // convert to string
dtostrf(bme_hum,2,1,buf); // convert to string
//Serial.println("Hum: "+ (String)buf);
doc["hum"] = buf;
dtostrf(bme_pres,2,1,buf); // convert to string
Expand All @@ -335,6 +342,7 @@ void updateSensors(bool forceUpate = false){
#endif
serializeJson(doc, payload);
mqttClient.publish(mqttStrings.sensor_topic, 0, false, payload); //uint16_t publish(const char* topic, uint8_t qos, bool retain, const char* payload = nullptr, size_t length = 0)
sensor_last_update = millis();
}
#endif
}
Expand Down Expand Up @@ -393,7 +401,7 @@ void setWill()

void sendDebug(char *key, String value)
{
DynamicJsonDocument doc(1024);
JsonDocument doc;
char payload[1024];
doc["reset-reason"] = esp_reset_reason();
doc["debug"] = hoermannEngine->state->debugMessage;
Expand All @@ -413,7 +421,7 @@ void sendDiscoveryMessageForBinarySensor(const char name[], const char topic[],
char vtemp[64];
sprintf(vtemp, "{{ value_json.%s }}", key);

DynamicJsonDocument doc(1024);
JsonDocument doc;

doc["name"] = name;
doc["state_topic"] = topic;
Expand All @@ -439,7 +447,7 @@ void sendDiscoveryMessageForAVSensor(const JsonDocument& device)

char uid[64];
sprintf(uid, "%s_sensor_availability", localPrefs->getString(preference_gd_id));
DynamicJsonDocument doc(1024);
JsonDocument doc;

doc["name"] = localPrefs->getString(preference_gd_avail);
doc["state_topic"] = mqttStrings.availability_topic;
Expand All @@ -462,9 +470,17 @@ void sendDiscoveryMessageForSensor(const char name[], const char topic[], const
sprintf(uid, "%s_sensor_%s", localPrefs->getString(preference_gd_id), key);

char vtemp[64];
sprintf(vtemp, "{{ value_json.%s }}", key);
//small workaround to get the value as float
if (key == "hum" || key == "temp" || key == "pres"){
sprintf(vtemp, "{{ value_json.%s | float }}", key);
} else {
sprintf(vtemp, "{{ value_json.%s }}", key);
}




DynamicJsonDocument doc(1024);
JsonDocument doc;

doc["name"] = name;
doc["state_topic"] = topic;
Expand Down Expand Up @@ -496,7 +512,7 @@ void sendDiscoveryMessageForDebug(const char name[], const char key[], const Jso
char vtemp[64];
sprintf(vtemp, "{{ value_json.%s }}", key);

DynamicJsonDocument doc(1024);
JsonDocument doc;

doc["name"] = name;
doc["state_topic"] = mqttStrings.debug_topic;
Expand Down Expand Up @@ -533,7 +549,7 @@ void sendDiscoveryMessageForSwitch(const char name[], const char discovery[], co
sprintf(uid, "%s_switch_%s",localPrefs->getString(preference_gd_id), topic);
}

DynamicJsonDocument doc(1024);
JsonDocument doc;

doc["name"] = name;
doc["state_topic"] = mqttStrings.state_topic;
Expand Down Expand Up @@ -567,7 +583,7 @@ void sendDiscoveryMessageForCover(const char name[], const char topic[], const J
char uid[64];
sprintf(uid, "%s_cover_%s", localPrefs->getString(preference_gd_id), topic);

DynamicJsonDocument doc(1024);
JsonDocument doc;
//if it didn't work try without state topic.
doc["name"] = name;
doc["state_topic"] = mqttStrings.state_topic;
Expand Down Expand Up @@ -606,8 +622,7 @@ void sendDiscoveryMessageForCover(const char name[], const char topic[], const J
void sendDiscoveryMessage()
{
//declare json object here for device instead of creating in each methode. 150 bytes should be enough
const int capacity = JSON_OBJECT_SIZE(6);
StaticJsonDocument<capacity> device;
JsonDocument device;
device["identifiers"] = localPrefs->getString(preference_gd_name);
device["name"] = localPrefs->getString(preference_gd_name);
device["sw_version"] = HA_VERSION;
Expand Down Expand Up @@ -699,7 +714,7 @@ void SensorCheck(void *parameter){
hcsr501stat = digitalRead(SR501PIN);
if (hcsr501stat != hcsr501_laststat) {
hcsr501_laststat = hcsr501stat;
DynamicJsonDocument doc(1024);
JsonDocument doc;
char payload[1024];
if (hcsr501stat) {
doc["motion"] = HA_ON;
Expand Down Expand Up @@ -727,7 +742,7 @@ void SensorCheck(void *parameter){
//bme_status = bme.begin(); // check sensor. adreess can be 0x76 or 0x77
}
if (!bme_status) {
DynamicJsonDocument doc(1024); //2048 needed because of BME280 float values!
JsonDocument doc; //2048 needed because of BME280 float values!
// char payload[1024];
// doc["bme_status"] = "Could not find a valid BME280 sensor!"; // see: https://github.com/adafruit/Adafruit_BME280_Library/blob/master/examples/bme280test/bme280test.ino#L49
// serializeJson(doc, payload);
Expand Down Expand Up @@ -1025,7 +1040,7 @@ void setup()
server.on("/status", HTTP_GET, [](AsyncWebServerRequest *request){
//const SHCIState &doorstate = emulator.getState();
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument root(1024);
JsonDocument root;
//response->print(hoermannEngine->state->toStatusJson());
root["doorstate"] = hoermannEngine->state->translatedState;
root["valid"] = hoermannEngine->state->valid;
Expand Down Expand Up @@ -1102,7 +1117,7 @@ void setup()
{
Serial.println("GET SYSINFO");
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument root(1024);
JsonDocument root;
root["freemem"] = ESP.getFreeHeap();
root["hostname"] = WiFi.getHostname();
root["ip"] = WiFi.localIP().toString();
Expand All @@ -1117,8 +1132,7 @@ void setup()
{
Serial.println("GET CONFIG");
AsyncResponseStream *response = request->beginResponseStream("application/json");
const int capacity = JSON_OBJECT_SIZE(65); //Strings counts twice
StaticJsonDocument<capacity> conf;
JsonDocument conf;
prefHandler.getConf(conf);
serializeJson(conf, *response);
request->send(response); });
Expand All @@ -1129,8 +1143,7 @@ void setup()
// Handle setting config request
if (request->url() == "/config")
{
const int capacity = JSON_OBJECT_SIZE(70); //Strings counts twice
StaticJsonDocument<capacity> doc;
JsonDocument doc;
deserializeJson(doc, data);
prefHandler.saveConf(doc);

Expand All @@ -1140,7 +1153,7 @@ void setup()
{
Serial.println("GET reset");
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument root(1024);
JsonDocument root;
root["reset"] = "OK";
serializeJson(root, *response);
request->send(response);
Expand Down
4 changes: 3 additions & 1 deletion HCPBridgeESP32/src/preferencesKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ class PreferenceHandler{

void getConf(JsonDocument& conf){
char gd_id[64];
char gd_name[64];
char mqtt_server[64];
char mqtt_user[64];
char wifi_ssid[64];
Expand All @@ -391,7 +392,7 @@ class PreferenceHandler{
char gs_park_avail[64];

strcpy(gd_id, preferences->getString(preference_gd_id).c_str());
preferences->putString(preference_gd_name, DEVICENAME);
strcpy(gd_name, preferences->getString(preference_gd_name).c_str());
strcpy(mqtt_server, preferences->getString(preference_mqtt_server).c_str());
strcpy(mqtt_user, preferences->getString(preference_mqtt_user).c_str());
strcpy(wifi_ssid, preferences->getString(preference_wifi_ssid).c_str());
Expand All @@ -410,6 +411,7 @@ class PreferenceHandler{
strcpy(gs_park_avail, preferences->getString(preference_gs_park_avail).c_str());

conf[preference_gd_id] = gd_id;
conf[preference_gd_name] = gd_name;
conf[preference_wifi_ap_mode] = this->preferences->getBool(preference_wifi_ap_mode);
conf[preference_wifi_ssid] = wifi_ssid;
conf[preference_mqtt_server] = mqtt_server;
Expand Down
Loading

0 comments on commit edda64c

Please sign in to comment.