Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M5core2 - Add shudown/led1 command and battery level #333

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/dev/esp32/m5stackcore2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/dev/esp32/m5stackcore2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 48 additions & 1 deletion src/hasp/hasp_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 */

Expand Down
58 changes: 58 additions & 0 deletions src/mqtt/hasp_mqtt_ha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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

Expand Down