Implemented digital pins for lamp and switch

This commit is contained in:
2021-07-14 00:59:18 +02:00
parent b5b1de6657
commit eab140a454
2 changed files with 213 additions and 157 deletions

View File

@@ -40,6 +40,11 @@ using namespace std::chrono_literals;
namespace { namespace {
constexpr const char * const TAG = "MAIN"; constexpr const char * const TAG = "MAIN";
constexpr const bool invertLamp = true;
constexpr const bool invertSwitch = true;
constexpr const gpio_num_t pins_lamp = GPIO_NUM_25;
constexpr const gpio_num_t pins_switch = GPIO_NUM_35;
constexpr const gpio_num_t pins_sda = GPIO_NUM_16; constexpr const gpio_num_t pins_sda = GPIO_NUM_16;
constexpr const gpio_num_t pins_scl = GPIO_NUM_17; constexpr const gpio_num_t pins_scl = GPIO_NUM_17;
constexpr const gpio_num_t pins_dht = GPIO_NUM_33; constexpr const gpio_num_t pins_dht = GPIO_NUM_33;
@@ -75,13 +80,13 @@ espcpputils::mqtt_client mqttClient;
std::atomic<bool> lampState; std::atomic<bool> lampState;
std::atomic<bool> switchState; std::atomic<bool> switchState;
uint8_t switchDebounce{};
//espchrono::millis_clock::time_point lastLampToggle; //espchrono::millis_clock::time_point lastLampToggle;
//espchrono::millis_clock::time_point lastSwitchToggle; //espchrono::millis_clock::time_point lastSwitchToggle;
Adafruit_TSL2561_Unified tsl{TSL2561_ADDR_FLOAT, 12345}; Adafruit_TSL2561_Unified tsl{TSL2561_ADDR_FLOAT, 12345};
bool tslInitialized{};
struct TslValue struct TslValue
{ {
espchrono::millis_clock::time_point timestamp; espchrono::millis_clock::time_point timestamp;
@@ -92,7 +97,6 @@ espchrono::millis_clock::time_point last_tsl2561_lux_pub;
Adafruit_BMP085_Unified bmp{10085}; Adafruit_BMP085_Unified bmp{10085};
bool bmpInitialized{};
struct BmpValue struct BmpValue
{ {
espchrono::millis_clock::time_point timestamp; espchrono::millis_clock::time_point timestamp;
@@ -107,7 +111,6 @@ espchrono::millis_clock::time_point last_bmp085_altitude_pub;
DHT dht(pins_dht, DHT11); DHT dht(pins_dht, DHT11);
bool dhtInitialized{};
struct DhtValue struct DhtValue
{ {
espchrono::millis_clock::time_point timestamp; espchrono::millis_clock::time_point timestamp;
@@ -129,6 +132,11 @@ esp_err_t webserver_reboot_handler(httpd_req_t *req);
void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data); void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
int mqttVerbosePub(std::string_view topic, std::string_view value, int qos, int retain); int mqttVerbosePub(std::string_view topic, std::string_view value, int qos, int retain);
void verboseDigitalWrite(uint8_t pin, uint8_t val);
void writeLamp(bool state);
bool readSwitch();
} // namespace } // namespace
extern "C" void app_main() extern "C" void app_main()
@@ -186,7 +194,7 @@ extern "C" void app_main()
.wifiEnabled = true, .wifiEnabled = true,
.hostname = "deckenlampe1", .hostname = "deckenlampe1",
.wifis = std::array<wifi_stack::wifi_entry, 10> { .wifis = std::array<wifi_stack::wifi_entry, 10> {
wifi_stack::wifi_entry { .ssid = "McDonalds Free WiFi", .key = "Passwort_123" }, wifi_stack::wifi_entry { .ssid = "ScheissAP", .key = "Passwort_123" },
wifi_stack::wifi_entry { .ssid = {}, .key = {} }, wifi_stack::wifi_entry { .ssid = {}, .key = {} },
wifi_stack::wifi_entry { .ssid = {}, .key = {} }, wifi_stack::wifi_entry { .ssid = {}, .key = {} },
wifi_stack::wifi_entry { .ssid = {}, .key = {} }, wifi_stack::wifi_entry { .ssid = {}, .key = {} },
@@ -223,6 +231,11 @@ extern "C" void app_main()
wifi_stack::init(wifiConfig); wifi_stack::init(wifiConfig);
pinMode(pins_lamp, OUTPUT);
writeLamp(lampState);
pinMode(pins_switch, INPUT);
httpd_handle_t httpdHandle{}; httpd_handle_t httpdHandle{};
{ {
@@ -360,12 +373,15 @@ extern "C" void app_main()
} }
} }
espchrono::millis_clock::time_point lastValuesRead;
{ {
ESP_LOGI(TAG, "calling Wire.begin()..."); ESP_LOGI(TAG, "calling Wire.begin()...");
const auto result = Wire.begin(pins_sda, pins_scl); const auto result = Wire.begin(pins_sda, pins_scl);
ESP_LOGI(TAG, "finished with %s", result ? "true" : "false"); ESP_LOGI(TAG, "finished with %s", result ? "true" : "false");
} }
bool tslInitialized{};
{ {
ESP_LOGI(TAG, "calling tsl.begin()..."); ESP_LOGI(TAG, "calling tsl.begin()...");
tslInitialized = tsl.begin(true); tslInitialized = tsl.begin(true);
@@ -402,6 +418,7 @@ extern "C" void app_main()
} }
} }
bool bmpInitialized{};
{ {
ESP_LOGI(TAG, "calling bmp.begin()..."); ESP_LOGI(TAG, "calling bmp.begin()...");
bmpInitialized = bmp.begin(); bmpInitialized = bmp.begin();
@@ -422,6 +439,7 @@ extern "C" void app_main()
} }
} }
bool dhtInitialized{};
{ {
ESP_LOGI(TAG, "calling dht.begin()..."); ESP_LOGI(TAG, "calling dht.begin()...");
dhtInitialized = dht.begin(); dhtInitialized = dht.begin();
@@ -436,6 +454,7 @@ extern "C" void app_main()
// { // {
// lastLampToggle = espchrono::millis_clock::now(); // lastLampToggle = espchrono::millis_clock::now();
// lampState = !lampState; // lampState = !lampState;
// writeLamp(lampState);
// if (mqttClient && mqttConnected) // if (mqttClient && mqttConnected)
// mqttVerbosePub(topic_lamp_status, lampState ? "ON" : "OFF", 0, 1); // mqttVerbosePub(topic_lamp_status, lampState ? "ON" : "OFF", 0, 1);
@@ -450,6 +469,9 @@ extern "C" void app_main()
// mqttVerbosePub(topic_switch_status, switchState ? "ON" : "OFF", 0, 1); // mqttVerbosePub(topic_switch_status, switchState ? "ON" : "OFF", 0, 1);
// } // }
if (espchrono::ago(lastValuesRead) >= 2s) {
lastValuesRead = espchrono::millis_clock::now();
if (tslInitialized) if (tslInitialized)
{ {
if (std::optional<sensors_event_t> event = tsl.getEvent()) if (std::optional<sensors_event_t> event = tsl.getEvent())
@@ -611,6 +633,30 @@ extern "C" void app_main()
lastDhtValue = std::nullopt; lastDhtValue = std::nullopt;
} }
} }
}
{
const auto newState = readSwitch();
if (newState == switchState.load())
switchDebounce = 0;
else {
switchDebounce++;
if (switchDebounce >= 10) {
switchDebounce = 0;
switchState = newState;
if (mqttClient && mqttConnected)
mqttVerbosePub(topic_switch_status, switchState ? "ON" : "OFF", 0, 1);
lampState = !lampState;
writeLamp(lampState);
if (mqttClient && mqttConnected)
mqttVerbosePub(topic_lamp_status, lampState ? "ON" : "OFF", 0, 1);
}
}
}
#if defined(CONFIG_ESP_TASK_WDT_PANIC) || defined(CONFIG_ESP_TASK_WDT) #if defined(CONFIG_ESP_TASK_WDT_PANIC) || defined(CONFIG_ESP_TASK_WDT)
if (const auto result = esp_task_wdt_reset(); result != ESP_OK) if (const auto result = esp_task_wdt_reset(); result != ESP_OK)
@@ -627,8 +673,6 @@ extern "C" void app_main()
#endif #endif
vPortYield(); vPortYield();
vTaskDelay(std::chrono::ceil<espcpputils::ticks>(2000ms).count());
} }
} }
@@ -688,6 +732,7 @@ esp_err_t webserver_root_handler(httpd_req_t *req)
esp_err_t webserver_on_handler(httpd_req_t *req) esp_err_t webserver_on_handler(httpd_req_t *req)
{ {
const bool state = (lampState = true); const bool state = (lampState = true);
writeLamp(state);
if (mqttClient && mqttConnected) if (mqttClient && mqttConnected)
mqttVerbosePub(topic_lamp_status, state ? "ON" : "OFF", 0, 1); mqttVerbosePub(topic_lamp_status, state ? "ON" : "OFF", 0, 1);
@@ -702,6 +747,7 @@ esp_err_t webserver_on_handler(httpd_req_t *req)
esp_err_t webserver_off_handler(httpd_req_t *req) esp_err_t webserver_off_handler(httpd_req_t *req)
{ {
const bool state = (lampState = false); const bool state = (lampState = false);
writeLamp(state);
if (mqttClient && mqttConnected) if (mqttClient && mqttConnected)
mqttVerbosePub(topic_lamp_status, state ? "ON" : "OFF", 0, 1); mqttVerbosePub(topic_lamp_status, state ? "ON" : "OFF", 0, 1);
@@ -716,6 +762,7 @@ esp_err_t webserver_off_handler(httpd_req_t *req)
esp_err_t webserver_toggle_handler(httpd_req_t *req) esp_err_t webserver_toggle_handler(httpd_req_t *req)
{ {
const bool state = (lampState = !lampState); const bool state = (lampState = !lampState);
writeLamp(state);
if (mqttClient && mqttConnected) if (mqttClient && mqttConnected)
mqttVerbosePub(topic_lamp_status, state ? "ON" : "OFF", 0, 1); mqttVerbosePub(topic_lamp_status, state ? "ON" : "OFF", 0, 1);
@@ -831,6 +878,7 @@ void mqttEventHandler(void *event_handler_arg, esp_event_base_t event_base, int3
if (topic == topic_lamp_set) if (topic == topic_lamp_set)
{ {
bool newState = (lampState = (value == "ON")); bool newState = (lampState = (value == "ON"));
writeLamp(newState);
if (mqttClient && mqttConnected) if (mqttClient && mqttConnected)
mqttVerbosePub(topic_lamp_status, newState ? "ON" : "OFF", 0, 1); mqttVerbosePub(topic_lamp_status, newState ? "ON" : "OFF", 0, 1);
} }
@@ -861,6 +909,28 @@ int mqttVerbosePub(std::string_view topic, std::string_view value, int qos, int
ESP_LOGI(TAG, "topic=\"%.*s\" value=\"%.*s\" succeeded pending_msg_id=%i", topic.size(), topic.data(), value.size(), value.data(), pending_msg_id); ESP_LOGI(TAG, "topic=\"%.*s\" value=\"%.*s\" succeeded pending_msg_id=%i", topic.size(), topic.data(), value.size(), value.data(), pending_msg_id);
return pending_msg_id; return pending_msg_id;
} }
void verboseDigitalWrite(uint8_t pin, uint8_t val)
{
ESP_LOGI(TAG, "pin=%hhu val=%hhu", pin, val);
digitalWrite(pin, val);
}
void writeLamp(bool state)
{
if (invertLamp)
state = !state;
verboseDigitalWrite(pins_lamp, state ? HIGH : LOW);
}
bool readSwitch()
{
bool state = digitalRead(pins_switch) == HIGH;
if (invertSwitch)
state = !state;
return state;
}
} // namespace } // namespace
auto espchrono::local_clock::timezone() noexcept -> time_zone auto espchrono::local_clock::timezone() noexcept -> time_zone

View File

@@ -542,16 +542,9 @@ CONFIG_ESP32_PHY_MAX_TX_POWER=20
# #
# Core dump # Core dump
# #
CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH=y # CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH is not set
# CONFIG_ESP_COREDUMP_ENABLE_TO_UART is not set # CONFIG_ESP_COREDUMP_ENABLE_TO_UART is not set
# CONFIG_ESP_COREDUMP_ENABLE_TO_NONE is not set CONFIG_ESP_COREDUMP_ENABLE_TO_NONE=y
# CONFIG_ESP_COREDUMP_DATA_FORMAT_BIN is not set
CONFIG_ESP_COREDUMP_DATA_FORMAT_ELF=y
# CONFIG_ESP_COREDUMP_CHECKSUM_CRC32 is not set
CONFIG_ESP_COREDUMP_CHECKSUM_SHA256=y
CONFIG_ESP_COREDUMP_ENABLE=y
CONFIG_ESP_COREDUMP_MAX_TASKS_NUM=64
CONFIG_ESP_COREDUMP_STACK_SIZE=0
# end of Core dump # end of Core dump
# #
@@ -1250,16 +1243,9 @@ CONFIG_ESP32S2_PANIC_PRINT_REBOOT=y
# CONFIG_ESP32S2_PANIC_SILENT_REBOOT is not set # CONFIG_ESP32S2_PANIC_SILENT_REBOOT is not set
# CONFIG_ESP32S2_PANIC_GDBSTUB is not set # CONFIG_ESP32S2_PANIC_GDBSTUB is not set
CONFIG_TIMER_TASK_STACK_SIZE=3584 CONFIG_TIMER_TASK_STACK_SIZE=3584
CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH=y # CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set
# CONFIG_ESP32_ENABLE_COREDUMP_TO_UART is not set # CONFIG_ESP32_ENABLE_COREDUMP_TO_UART is not set
# CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE is not set CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE=y
# CONFIG_ESP32_COREDUMP_DATA_FORMAT_BIN is not set
CONFIG_ESP32_COREDUMP_DATA_FORMAT_ELF=y
# CONFIG_ESP32_COREDUMP_CHECKSUM_CRC32 is not set
CONFIG_ESP32_COREDUMP_CHECKSUM_SHA256=y
CONFIG_ESP32_ENABLE_COREDUMP=y
CONFIG_ESP32_CORE_DUMP_MAX_TASKS_NUM=64
CONFIG_ESP32_CORE_DUMP_STACK_SIZE=0
CONFIG_MB_MASTER_TIMEOUT_MS_RESPOND=150 CONFIG_MB_MASTER_TIMEOUT_MS_RESPOND=150
CONFIG_MB_MASTER_DELAY_MS_CONVERT=200 CONFIG_MB_MASTER_DELAY_MS_CONVERT=200
CONFIG_MB_QUEUE_LENGTH=20 CONFIG_MB_QUEUE_LENGTH=20