OMOTE/Platformio/hardware/ESP32/mqtt_hal_esp32.cpp

107 lines
3.0 KiB
C++
Raw Normal View History

2024-03-10 14:27:46 -04:00
#include "WiFi.h"
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
#include <PubSubClient.h>
2024-03-10 14:27:46 -04:00
#include "mqtt_hal_esp32.h"
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
#include "secrets.h"
2024-03-10 14:27:46 -04:00
#if (ENABLE_WIFI_AND_MQTT == 1)
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
WiFiClient espClient;
PubSubClient mqttClient(espClient);
2024-03-10 14:27:46 -04:00
bool isWifiConnected = false;
showWiFiconnected_cb thisShowWiFiconnected_cb = NULL;
void set_showWiFiconnected_cb_HAL(showWiFiconnected_cb pShowWiFiconnected_cb) {
thisShowWiFiconnected_cb = pShowWiFiconnected_cb;
}
bool getIsWifiConnected_HAL() {
return isWifiConnected;
}
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
// WiFi status event
void WiFiEvent(WiFiEvent_t event){
//Serial.printf("[WiFi-event] event: %d\r\n", event);
if(event == ARDUINO_EVENT_WIFI_STA_GOT_IP){
// connection to MQTT server will be done in checkMQTTconnection()
// mqttClient.setServer(MQTT_SERVER, 1883); // MQTT initialization
// mqttClient.connect("OMOTE"); // Connect using a client id
}
// Set status bar icon based on WiFi status
if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP || event == ARDUINO_EVENT_WIFI_STA_GOT_IP6) {
2024-03-10 14:27:46 -04:00
isWifiConnected = true;
thisShowWiFiconnected_cb(true);
Serial.printf("WiFi connected, IP address: %s\r\n", WiFi.localIP().toString().c_str());
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
} else if (event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
2024-03-10 14:27:46 -04:00
isWifiConnected = false;
thisShowWiFiconnected_cb(false);
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
// automatically try to reconnect
Serial.printf("WiFi got disconnected. Will try to reconnect.\r\n");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
} else {
// e.g. ARDUINO_EVENT_WIFI_STA_CONNECTED or many others
// connected is not enough, will wait for IP
2024-03-10 14:27:46 -04:00
isWifiConnected = false;
thisShowWiFiconnected_cb(false);
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
}
}
2024-03-10 14:27:46 -04:00
void init_mqtt_HAL(void) {
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
// Setup WiFi
WiFi.setHostname("OMOTE"); //define hostname
WiFi.onEvent(WiFiEvent);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
WiFi.setSleep(true);
}
bool checkMQTTconnection() {
if (WiFi.isConnected()) {
if (mqttClient.connected()) {
return true;
} else {
// try to connect to mqtt server
mqttClient.setServer(MQTT_SERVER, MQTT_SERVER_PORT); // MQTT initialization
if (mqttClient.connect(MQTT_CLIENTNAME, MQTT_USER, MQTT_PASS)) {
Serial.printf(" Successfully connected to MQTT broker\r\n");
} else {
Serial.printf(" MQTT connection failed (but WiFi is available). Will try later ...\r\n");
}
return mqttClient.connected();
}
} else {
Serial.printf(" No connection to MQTT server, because WiFi ist not connected.\r\n");
return false;
}
}
2024-03-10 14:27:46 -04:00
bool publishMQTTMessage_HAL(const char *topic, const char *payload){
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
if (checkMQTTconnection()) {
// Serial.printf("Sending mqtt payload to topic \"%s\": %s\r\n", topic, payload);
if (mqttClient.publish(topic, payload)) {
// Serial.printf("Publish ok\r\n");
return true;
}
else {
Serial.printf("Publish failed\r\n");
}
} else {
Serial.printf(" Cannot publish mqtt message, because checkMQTTconnection failed (WiFi or mqtt is not connected)\r\n");
}
return false;
}
2024-03-10 14:27:46 -04:00
void wifiStop_HAL() {
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
}
Modular approach - first version for main branch (#60) * first version of "modular-approach" * changed keyboard commands for HOME and BACK * Update README.md * only some typos in comments * readability * comment for what the mqtt keyboard is used * removed numbering of tab variables. not needed anymore * changed the default keyboard from "´mqtt keyboard" to "BLE keyboard" * updated to latest version of keypad library from Github, changes for inverted logic are explicitely marked * added comment for key repeatModes * added comment for MQTT keyboard * setting timout via GUI now works, not only dropdown without functionality * BLE indicator added; separation of BLE/WiFi activation from activation of devices using it * report battery level to BLE device * Dynamic keyboard commands, so you can safely deactivate BLE and/or WiFi and not break the example code * reorganized files into folders * moved lv_conf.h into the gui folder * added devices for appleTV and smarthome * assets.c broken up and placed them where they are used * added support for IR RC5 * reorganization of files and folder * added comment * renamed assets files * introduction of gui_registry * removed unnecessary functions from sleep.h * use gui_registry.h only once * some files renamed for better understandability * minor renaming * some more renaming * check if WiFi label was instantiated before using it * introduction of a scene registry * save prefs directly after timeout is changed * renaming of preferencesStorage * comment added * only readability * detailled definition of key layout for the diferrent scenes * made code compile for "device_smarthome" when WiFi is deactivated * fixed access violation when no scene was active * added support for IR DENON commands * increased lvgl heap from 32K to 48K
2024-02-12 13:57:51 -05:00
#endif