<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[M5 Station Battery version doesn&#x27;t last this long]]></title><description><![CDATA[<p dir="auto">Hi all,</p>
<p dir="auto">I'm using the M5 Station Battery Version. Attached i have the Co2/temp/Hum sensor.<br />
The case is to connect to a WIFI, make a connection to MQTT Broker then read the actual values from the sensor. After this i put the device in deep sleep mode with following command "M5.Axp.DeepSleep(SLEEP_SEC(60));".<br />
This tasks take about 5 seconds then the Station goes to deep sleep.</p>
<p dir="auto">This all works fine when is plugged in to power supply but when i run the Station without supply the battery doesn't even last over night. Further i can see in the MQTT Broker that not always the 60 seconds take place.</p>
<p dir="auto">So i published the batterylevel too, that i can see what going on but there is always the value 100 (fully charged).<br />
The batteries are the original ones and the Station was plugged during working hours through USB Port.</p>
<p dir="auto">Does everyone know if I'm doing things wrong?</p>
<p dir="auto">Here is the code.</p>
<p dir="auto">#include &lt;TinyGPSPlus.h&gt;<br />
#include &lt;ETH.h&gt;<br />
#include &lt;dummy.h&gt;<br />
#include &lt;EEPROM.h&gt;<br />
#include &lt;M5Station.h&gt;<br />
#include &lt;I2C_AXP192.h&gt;<br />
#include &lt;WiFi.h&gt;<br />
#include &lt;PubSubClient.h&gt;<br />
#include &lt;SensirionI2CScd4x.h&gt;</p>
<p dir="auto">// Hardware definitions<br />
SensirionI2CScd4x Co2Sensor;<br />
// TinyGPSPlus gps;<br />
// HardwareSerial GPSRAW(2);</p>
<p dir="auto">// Network details<br />
const char *ssid = "ssid";<br />
const char *wifi_password = "password";</p>
<p dir="auto">// MQTT broker<br />
const char *mqtt_server = "IP Address";<br />
const char *mqtt_topic_temp = "M5/Temperature";<br />
const char *mqtt_topic_co2 = "M5/co2";<br />
const char *mqtt_topic_humidity = "M5/Humidity";<br />
const char *mqtt_topic_batterylevel = "M5/Batterylevel";<br />
const char *mqtt_topic_batteryPower = "M5/BatteryPower";<br />
const char *mqtt_topic_batterycurrent = "M5/Batterycurrent";<br />
const char *mqtt_topic_batteryvoltage = "M5/Batteryvoltage";<br />
const char *mqtt_username = "username";<br />
const char *mqtt_password = "password";<br />
const char *mqtt_client_id = "KRL";</p>
<p dir="auto">// Variables</p>
<p dir="auto">uint8_t batterylevel = M5.Axp.GetBatteryLevel();<br />
float batterypower = M5.Axp.GetBatPower();<br />
float batteryvoltage = M5.Axp.GetBatVoltage();<br />
float batterycurrent = M5.Axp.GetBatCurrent();</p>
<p dir="auto">WiFiClient wifi_client;<br />
PubSubClient client(mqtt_server, 1883, wifi_client);</p>
<p dir="auto">void setup()<br />
{<br />
M5.begin(115200);<br />
M5.Axp.begin();<br />
delay(1000);</p>
<pre><code>M5.Lcd.setTextFont(4);
M5.Lcd.drawString(String(batterylevel), 110, 0);
M5.Lcd.drawString(String(batterypower), 160, 20);
M5.Lcd.drawString(String(batteryvoltage), 160, 40);
M5.Lcd.drawString(String(batterycurrent), 160, 60);
uint16_t error;
char errorMessage[256];
Co2Sensor.begin(Wire);
// stop potentially previously started measurement
error = Co2Sensor.stopPeriodicMeasurement();
if (error)
{
    Serial.print("Error trying to execute stopPeriodicMeasurement(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
}

// Start Measurement
error = Co2Sensor.startPeriodicMeasurement();
if (error)
{
    Serial.print("Error trying to execute startPeriodicMeasurement(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
}
Serial.println("Waiting for first measurement... (5 sec)");

connect_to_wifi();
connect_to_mqtt();
delay(3000);
get_data_and_publish();
disconnect_wifi_MQTT();
sleepy();
</code></pre>
<p dir="auto">}<br />
void loop()<br />
{<br />
}<br />
void sleepy()<br />
{</p>
<pre><code>Serial.println("Going to sleep...");
delay(1000);
M5.Axp.DeepSleep(SLEEP_SEC(60));
</code></pre>
<p dir="auto">}<br />
void disconnect_wifi_MQTT()<br />
{</p>
<pre><code>WiFi.disconnect();
delay(1000);
client.disconnect();
Serial.println("WIFI and MQTT disconnected");
</code></pre>
<p dir="auto">}<br />
void get_data_and_publish()<br />
{<br />
uint16_t error;<br />
char errorMessage[256];<br />
delay(100);<br />
// Read Measurement<br />
uint16_t co2_measured = 0;<br />
float temperature_measured = 0.0f;<br />
float humidity_measured = 0.0f;<br />
bool isDataReady = false;<br />
Serial.println("Start measuring....");<br />
error = Co2Sensor.getDataReadyFlag(isDataReady);<br />
if (error)<br />
{<br />
M5.Lcd.print("Error trying to execute readMeasurement(): ");<br />
errorToString(error, errorMessage, 256);<br />
Serial.println(errorMessage);<br />
return;<br />
}<br />
if (!isDataReady)<br />
{<br />
Serial.println("Data isnt ready!");<br />
get_data_and_publish();<br />
return;<br />
}<br />
error = Co2Sensor.readMeasurement(co2_measured, temperature_measured, humidity_measured);<br />
if (error)<br />
{<br />
M5.Lcd.print("Error trying to execute readMeasurement(): ");<br />
errorToString(error, errorMessage, 256);<br />
Serial.println(errorMessage);<br />
}<br />
else if (co2_measured == 0)<br />
{<br />
Serial.println("Invalid sample detected, skipping.");<br />
}<br />
else<br />
{<br />
Serial.println("Show Measurement");<br />
M5.Lcd.setCursor(0, 35);<br />
M5.Lcd.printf("Co2:%d\n", co2_measured);<br />
M5.Lcd.printf("Temp.:%f\n", temperature_measured);<br />
M5.Lcd.printf("Humidity:%f\n", humidity_measured);<br />
String valuesToSend_temp = String(temperature_measured);<br />
String valuesToSend_co2 = String(co2_measured);<br />
String valuesToSend_humidity = String(humidity_measured);<br />
String valuesToSend_batterylevel = String(batterylevel);<br />
String valuesToSend_batteryPower = String(batterypower);<br />
String valuesToSend_batteryvoltage = String(batteryvoltage);<br />
String valuesToSend_batterycurrent = String(batterycurrent);<br />
client.publish(mqtt_topic_temp, valuesToSend_temp.c_str());<br />
client.publish(mqtt_topic_co2, valuesToSend_co2.c_str());<br />
client.publish(mqtt_topic_humidity, valuesToSend_humidity.c_str());<br />
client.publish(mqtt_topic_batterylevel, valuesToSend_batterylevel.c_str());<br />
client.publish(mqtt_topic_batteryPower, valuesToSend_batteryPower.c_str());<br />
client.publish(mqtt_topic_batteryvoltage, valuesToSend_batteryvoltage.c_str());<br />
client.publish(mqtt_topic_batterycurrent, valuesToSend_batterycurrent.c_str());<br />
Serial.println("Messages published...");<br />
client.loop();<br />
delay(2000);<br />
}<br />
}</p>
<p dir="auto">void connect_to_wifi()<br />
{<br />
WiFi.persistent(false);<br />
WiFi.disconnect();<br />
WiFi.mode(WIFI_STA);<br />
// Connect to the network<br />
WiFi.begin(ssid, wifi_password);<br />
Serial.println("Connecting to WiFi");<br />
int retry_count = 0;<br />
while (WiFi.status() != WL_CONNECTED &amp;&amp; retry_count &lt; 15)<br />
{<br />
delay(1000);<br />
retry_count++;<br />
Serial.print(".");<br />
}<br />
if (WiFi.status() != WL_CONNECTED)<br />
{<br />
ESP.restart();<br />
}<br />
Serial.println("");<br />
Serial.println(WiFi.localIP());<br />
}<br />
void connect_to_mqtt()<br />
{<br />
int retry_count = 0;<br />
while (!client.connected() &amp;&amp; retry_count &lt; 15)<br />
{<br />
// Connect to MQTT broker<br />
if (client.connect(mqtt_client_id, mqtt_username, mqtt_password))<br />
{<br />
Serial.println("Connected to MQTT Broker!");<br />
}<br />
else<br />
{<br />
Serial.println("Connection to MQTT Broker failed...");<br />
Serial.println(client.state());<br />
retry_count++;<br />
delay(1000);<br />
}<br />
}<br />
if (!client.connected())<br />
{<br />
ESP.restart();<br />
}<br />
}</p>
]]></description><link>https://community.m5stack.com/topic/4912/m5-station-battery-version-doesn-t-last-this-long</link><generator>RSS for Node</generator><lastBuildDate>Sun, 08 Mar 2026 00:14:21 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/4912.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 22 Dec 2022 11:26:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to M5 Station Battery version doesn&#x27;t last this long on Thu, 22 Dec 2022 15:56:32 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="mention plugin-mentions-user plugin-mentions-a" href="https://community.m5stack.com/uid/26963">@FLOSCH</a></p>
<p dir="auto">the reason battery level, power, voltage and current are incorrect is that the calls to <code>GetBatteryLevel()</code>, <code>GetBatPower()</code>, <code>GetBatVoltage()</code> and <code>GetBatCurrent()</code> are happening <strong>before</strong> I2C is initialized and therefore return invalid values. You'll need to do this calls inside <code>setup()</code> and <strong>after</strong> <code>M5.begin()</code>.</p>
<p dir="auto">Thanks<br />
Felix</p>
]]></description><link>https://community.m5stack.com/post/19631</link><guid isPermaLink="true">https://community.m5stack.com/post/19631</guid><dc:creator><![CDATA[felmue]]></dc:creator><pubDate>Thu, 22 Dec 2022 15:56:32 GMT</pubDate></item></channel></rss>