diff --git a/src/dev/esp32/m5stackcore2.cpp b/src/dev/esp32/m5stackcore2.cpp index f43e32e0e..cec01cf0c 100644 --- a/src/dev/esp32/m5stackcore2.cpp +++ b/src/dev/esp32/m5stackcore2.cpp @@ -85,12 +85,22 @@ void M5StackCore2::get_sensors(JsonDocument& doc) JsonObject sensor = doc.createNestedObject(F("AXP192")); sensor[F("BattVoltage")] = Axp.GetBatVoltage(); sensor[F("BattPower")] = Axp.GetBatPower(); - // sensor[F("Batt%")] = Axp.getBattPercentage(); + sensor[F("BatteryLevel")] = Axp.GetBatteryLevel(); sensor[F("BattChargeCurrent")] = Axp.GetBatChargeCurrent(); sensor[F("Temperature")] = Axp.GetTempInAXP192(); sensor[F("Charging")] = Axp.isCharging(); } +void M5StackCore2::set_led(bool led) +{ + Axp.SetLed(led); +} + +void M5StackCore2::shutdown() +{ + Axp.PowerOff(); +} + } // namespace dev dev::M5StackCore2 haspDevice; diff --git a/src/dev/esp32/m5stackcore2.h b/src/dev/esp32/m5stackcore2.h index 3dc223d05..43dafec03 100644 --- a/src/dev/esp32/m5stackcore2.h +++ b/src/dev/esp32/m5stackcore2.h @@ -20,7 +20,8 @@ class M5StackCore2 : public Esp32Device { void set_backlight_power(bool power); bool get_backlight_power(); void update_backlight(); - + void set_led(bool led); + void shutdown(); private: uint8_t _backlight_level; uint8_t _backlight_power; diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 17da218b6..5f387c394 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -47,7 +47,7 @@ uint16_t dispatchSecondsToNextTeleperiod = 0; uint16_t dispatchSecondsToNextSensordata = 0; uint16_t dispatchSecondsToNextDiscovery = 0; uint8_t nCommands = 0; -haspCommand_t commands[28]; +haspCommand_t commands[31]; moodlight_t moodlight = {.brightness = 255}; uint8_t saved_jsonl_page = 0; @@ -1369,6 +1369,48 @@ void dispatch_service(const char*, const char* payload, uint8_t source) #endif } +#if defined(M5STACK) +void dispatch_m5stack_shutdown(const char*, const char*, uint8_t source) +{ + #if HASP_USE_CONFIG > 0 + configWrite(); + #endif + #if HASP_USE_MQTT > 0 && defined(ARDUINO) + mqttStop(); // Stop the MQTT Client first + #endif + #if HASP_USE_TELNET > 0 + telnetStop(); + #endif + #if HASP_USE_CONFIG > 0 + debugStop(); + #endif + #if HASP_USE_WIFI > 0 + wifiStop(); + #endif + LOG_VERBOSE(TAG_MSGR, F("-------------------------------------")); + LOG_TRACE(TAG_MSGR, F(D_DISPATCH_REBOOT)); + + #if defined(WINDOWS) || defined(POSIX) + fflush(stdout); + #else + Serial.flush(); + #endif + haspDevice.shutdown(); + } + + void dispatch_m5stack_led1(const char*, const char* led1, uint8_t source) +{ + if(strlen(led1) == 0) { + return; + } + if(Parser::is_true(led1)) { + haspDevice.set_led(1); + } else { + haspDevice.set_led(0); + } +} + +#endif /******************************************* Commands builder *******************************************/ static void dispatch_add_command(const char* p_cmdstr, void (*func)(const char*, const char*, uint8_t)) @@ -1426,6 +1468,11 @@ void dispatchSetup() #endif #if HASP_USE_CONFIG > 0 dispatch_add_command(PSTR("setupap"), oobeFakeSetup); +#endif +#if defined(M5STACK) + dispatch_add_command(PSTR("shutdown"), dispatch_m5stack_shutdown); + dispatch_add_command(PSTR("poweroff"), dispatch_m5stack_shutdown); + dispatch_add_command(PSTR("led1"), dispatch_m5stack_led1); #endif /* WARNING: remember to expand the commands array when adding new commands */ diff --git a/src/mqtt/hasp_mqtt_ha.cpp b/src/mqtt/hasp_mqtt_ha.cpp index bf6edb1c0..b489dcc60 100644 --- a/src/mqtt/hasp_mqtt_ha.cpp +++ b/src/mqtt/hasp_mqtt_ha.cpp @@ -287,6 +287,60 @@ void mqtt_ha_register_activepage() mqtt_ha_send_json(buffer, doc); } +#if defined(M5STACK) +void mqtt_ha_register_isconnected() +{ + StaticJsonDocument<1024> doc; + char item[16]; + snprintf_P(item, sizeof(item), PSTR("charging")); + + // start from static keys and values that do not change + deserializeJson(doc, F("{" + "\"dev_cla\":\"power\"," + "\"avty_t\":\"~LWT\"," + "\"pl_on\": true," + "\"pl_off\": false," + "\"pl_avail\":\"online\"," + "\"pl_not_avail\":\"offline\"," + "\"val_tpl\": \"{{ value_json.AXP192.Charging }}\"," + "\"stat_t\":\"~" MQTT_TOPIC_STATE "/sensors\"}")); + mqtt_ha_add_device_ids(doc); + mqtt_ha_add_unique_id(doc, item); + + char buffer[128]; + snprintf_P(buffer, sizeof(buffer), PSTR("%s/binary_sensor/%s/%s/config"), discovery_prefix, + haspDevice.get_hostname(), item); + mqtt_ha_send_json(buffer, doc); +} + +void mqtt_ha_register_batterylevel() +{ + StaticJsonDocument<1024> doc; + char item[16]; + snprintf_P(item, sizeof(item), PSTR("battery")); + + // start from static keys and values that do not change + deserializeJson(doc, F("{" + "\"dev_cla\":\"battery\"," + "\"avty_t\":\"~LWT\"," + "\"pl_on\": true," + "\"pl_off\": false," + "\"pl_avail\":\"online\"," + "\"stat_cla\":\"measurement\"," + "\"unit_of_meas\":\"%\"," + "\"pl_not_avail\":\"offline\"," + "\"val_tpl\": \"{{ value_json.AXP192.BatteryLevel|round }}\"," + "\"stat_t\":\"~" MQTT_TOPIC_STATE "/sensors\"}")); + mqtt_ha_add_device_ids(doc); + mqtt_ha_add_unique_id(doc, item); + + char buffer[128]; + snprintf_P(buffer, sizeof(buffer), PSTR("%s/sensor/%s/%s/config"), discovery_prefix, + haspDevice.get_hostname(), item); + mqtt_ha_send_json(buffer, doc); +} +#endif + void mqtt_ha_register_auto_discovery() { LOG_TRACE(TAG_MQTT_PUB, F(D_MQTT_HA_AUTO_DISCOVERY)); @@ -297,6 +351,10 @@ void mqtt_ha_register_auto_discovery() mqtt_ha_register_moodlight(); mqtt_ha_register_idle(); mqtt_ha_register_connectivity(); +#if defined(M5STACK) + mqtt_ha_register_isconnected(); + mqtt_ha_register_batterylevel(); +#endif } #endif