SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
axp2602_gauge_udp.ino
Go to the documentation of this file.
1
30#include <GaugeDrv.hpp>
31
32#ifdef ARDUINO_ARCH_ESP32
33#include <WiFi.h>
34
35
36#ifndef SENSOR_SDA
37#define SENSOR_SDA 3
38#endif
39
40#ifndef SENSOR_SCL
41#define SENSOR_SCL 2
42#endif
43
44
45//IP address to send UDP data to:
46// either use the ip address of the server or
47// a network broadcast address
48const char *udpAddress = "Your_UDP_IP";
49const int udpPort = 3333; // Replace with your UDP port
50const char *ssid = "Your_SSID";
51const char *password = "Your_PASSWORD";
52
53WiFiUDP udp;
55
56void setup()
57{
58 Serial.begin(115200);
59 // while (!Serial);
60
61
62 WiFi.begin(ssid, password);
63 while (WiFi.status() != WL_CONNECTED) {
64 delay(1000);
65 Serial.println("Connecting to WiFi...");
66 }
67 Serial.println("Connected to WiFi!");
68 Serial.print("IP Address: ");
69 Serial.println(WiFi.localIP());
70
71 udp.begin(WiFi.localIP(), udpPort);
72 Serial.println("UDP started");
73
74 if (!gauge.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
75 Serial.println("Failed to AXP2602 - check your wiring!");
76 while (1) {
77 delay(1000);
78 }
79 }
80 Serial.println("Init AXP2602 Sensor success!");
81
82 /*
83 * Gauge Work mode:
84 * // It has relatively high power consumption, but can be used for both charging and discharging.
85 * - OPERATING_MODE_HIGH_PRECISION
86 *
87 * // In default mode, it can be used for both charging and discharging.
88 * - OPERATING_MODE_NORMAL
89 *
90 * // With relatively low power consumption, it can only be used in low-power scenarios during discharge.
91 * // In other scenarios, software switching to normal mode or high-precision mode is required.
92 * - OPERATING_MODE_LOW_POWER
93 */
94 // gauge.setOperatingMode(GaugeAXP2602::OPERATING_MODE_HIGH_PRECISION);
95
96 /*
97 * The sampling resistor size is 10 milliohms by default.
98 * SENSE_RESISTOR_10_MOHM
99 * SENSE_RESISTOR_5_MOHM
100 * SENSE_RESISTOR_20_MOHM
101 * SENSE_RESISTOR_40_MOHM
102 */
103 // gauge.setCurrentSenseResistor(GaugeAXP2602::SENSE_RESISTOR_10_MOHM);
104
105 // gauge.setBatteryDetection(true); // Enabled by default
106
107 // gauge.setCurrentMeasurement(true); // Enabled by default
108
109 // gauge.setThermalDieMeasurement(true); // Enabled by default
110
111 // gauge.setLowBatterySOCThreshold(60); // Set low battery SOC threshold to 60%
112
115
116 /*
117
118 Replace with the battery curve you want to use.
119
120 static uint8_t BATTER_PARAMS[] = {
121 0x01, 0xf5, 0x40, 0x00, 0x1b, 0x1e, 0x28, 0x0f, 0x0c, 0x1e, 0x32, 0x02, 0x14, 0x05, 0x0a, 0x04,
122 0x74, 0xfc, 0xf4, 0x0d, 0x43, 0x10, 0x52, 0xfb, 0xa6, 0x01, 0xea, 0x04, 0x64, 0x06, 0x52, 0x06,
123 0x18, 0x0a, 0xe7, 0x0f, 0x9f, 0x0f, 0x51, 0x09, 0xf7, 0x0e, 0x89, 0x0e, 0x71, 0x04, 0x58, 0x04,
124 0x43, 0x09, 0x32, 0x0e, 0x1c, 0x0e, 0x14, 0x09, 0x04, 0x0d, 0xe9, 0x0d, 0xde, 0x03, 0xc8, 0x03,
125 0xb3, 0x08, 0x9d, 0x0d, 0x79, 0x0d, 0x3a, 0x07, 0xf5, 0x9e, 0x56, 0x47, 0x36, 0x20, 0x24, 0x17,
126 0xc5, 0x98, 0x7e, 0x66, 0x4e, 0x44, 0x38, 0x1a, 0x12, 0x0a, 0xf6, 0x00, 0x00, 0xf6, 0x00, 0xf6,
127 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xf6, 0x00, 0x00, 0xf6, 0x00, 0xf6,
128 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xfb, 0x00, 0x00, 0xf6, 0x00, 0x00, 0xf6, 0x00, 0xf6
129 };
130
131
132 if (gauge.writeGaugeData(BATTER_PARAMS, sizeof(BATTER_PARAMS))) {
133 Serial.println("Battery calibration data write success");
134 } else {
135 Serial.println("Battery calibration data write failed");
136 }
137
138 */
139
140}
141
142void loop()
143{
144 uint32_t startMeasTime = millis();
145
146 if (gauge.refresh()) {
147
148 uint32_t endMesTime = millis();
149
150 udp.beginPacket(udpAddress, udpPort);
151
152 udp.print("Polling time: "); udp.print(endMesTime - startMeasTime); udp.println(" ms");
153 udp.print("\t- Sleep: "); udp.print(gauge.isSleepModeEnabled() ? "Enabled" : "Disabled"); udp.println();
154 udp.print("\t- Temperature:"); udp.print(gauge.getTemperature() ); udp.println(" ℃");
155 udp.print("\t- BatteryVoltage:"); udp.print(gauge.getVoltage()); udp.println(" mV");
156 udp.print("\t- InstantaneousCurrent:"); udp.print(gauge.getCurrent()); udp.println(" mAh");
157 udp.print("\t- InstantaneousCurrentRaw:"); udp.print(gauge.getCurrentRaw()); udp.println(" RAW");
158 udp.print("\t- ThermalDieTemperature:"); udp.print(gauge.getThermalDieTemperature()); udp.println(" ℃");
159 udp.print("\t- ThermalDieTemperatureRaw:"); udp.print(gauge.getThermalDieTemperatureRaw()); udp.println(" RAW");
160 udp.print("\t- ChargingPower:"); udp.print(gauge.getChargingPower()); udp.println(" W");
161 udp.print("\t- DischargingPower:"); udp.print(gauge.getDischargingPower()); udp.println(" W");
162 udp.print("\t- AbsolutePower:"); udp.print(gauge.getAbsolutePower()); udp.println(" W");
163 udp.print("\t- isCharging:"); udp.print(gauge.isCharging() ? "Yes" : "No"); udp.println();
164 udp.print("\t- isDischarging:"); udp.print(gauge.isDischarging() ? "Yes" : "No"); udp.println();
165 udp.print("\t- TimeToEmpty:"); udp.print(gauge.getTimeToEmpty()); udp.println(" minutes");
166 udp.print("\t- TimeToFull:"); udp.print(gauge.getTimeToFull()); udp.println(" minutes");
167 udp.print("\t- StateOfCharge:"); udp.print(gauge.getStateOfCharge()); udp.println(" %");
168 udp.print("\t- Battery Status:"); udp.print(gauge.getBatteryStatus()); udp.println(" RAW");
169
170
171 Serial.print("Polling time: "); Serial.print(endMesTime - startMeasTime); Serial.println(" ms");
172 Serial.print("\t- Sleep: "); Serial.print(gauge.isSleepModeEnabled() ? "Enabled" : "Disabled"); Serial.println();
173 Serial.print("\t- Temperature:"); Serial.print(gauge.getTemperature()); Serial.println(" ℃");
174 Serial.print("\t- BatteryVoltage:"); Serial.print(gauge.getVoltage()); Serial.println(" mV");
175 Serial.print("\t- InstantaneousCurrent:"); Serial.print(gauge.getCurrent()); Serial.println(" mAh");
176 Serial.print("\t- InstantaneousCurrentRaw:"); Serial.print(gauge.getCurrentRaw()); Serial.println(" RAW");
177 Serial.print("\t- ThermalDieTemperature:"); Serial.print(gauge.getThermalDieTemperature()); Serial.println(" ℃");
178 Serial.print("\t- ThermalDieTemperatureRaw:"); Serial.print(gauge.getThermalDieTemperatureRaw()); Serial.println(" RAW");
179 Serial.print("\t- ChargingPower:"); Serial.print(gauge.getChargingPower()); Serial.println(" W");
180 Serial.print("\t- DischargingPower:"); Serial.print(gauge.getDischargingPower()); Serial.println(" W");
181 Serial.print("\t- AbsolutePower:"); Serial.print(gauge.getAbsolutePower()); Serial.println(" W");
182 Serial.print("\t- isCharging:"); Serial.print(gauge.isCharging() ? "Yes" : "No"); Serial.println();
183 Serial.print("\t- isDischarging:"); Serial.print(gauge.isDischarging() ? "Yes" : "No"); Serial.println();
184 Serial.print("\t- TimeToEmpty:"); Serial.print(gauge.getTimeToEmpty()); Serial.println(" minutes");
185 Serial.print("\t- TimeToFull:"); Serial.print(gauge.getTimeToFull()); Serial.println(" minutes");
186 Serial.print("\t- StateOfCharge:"); Serial.print(gauge.getStateOfCharge()); Serial.println(" %");
187 Serial.print("\t- Battery Status:"); Serial.print(gauge.getBatteryStatus()); Serial.println(" RAW");
188
189
191
192 switch (irqStatus) {
194 Serial.println("Low battery detected!");
195 udp.println("Low battery detected!");
196 break;
198 Serial.println("Watchdog timer timeout!");
199 udp.println("Watchdog timer timeout!");
200 break;
202 Serial.println("Over temperature detected!");
203 udp.println("Over temperature detected!");
204 break;
206 Serial.println("State of Charge updated!");
207 udp.println("State of Charge updated!");
208 break;
209 default:
210 break;
211 }
212
213 if (irqStatus != GaugeAXP2602::IRQ_STATUS_NONE) {
215 }
216
217 udp.endPacket();
218
219 Serial.println("===============================================");
220
221 }
222 delay(3000);
223}
224#else
225void setup()
226{
227 Serial.begin(115200);
228}
229
230void loop()
231{
232 Serial.println("This example only supports ESP32 platform.");
233 delay(5000);
234}
235#endif
@license MIT License
#define SENSOR_SCL
#define SENSOR_SDA
GaugeAXP2602 gauge
void setup()
void loop()
uint16_t getStateOfCharge() override
Get the battery percentage.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback)
Begin with custom callback functions.
uint16_t getTimeToFull() override
Get the battery time to full.
bool isSleepModeEnabled()
Check if the AXP2602 chip is in sleep mode.
uint16_t getTimeToEmpty() override
Get the battery time to empty.
bool isDischarging() override
Checks if the battery is discharging.
float getTemperature() override
Get the temperature.
uint16_t getVoltage() override
Get the battery voltage.
float getCurrent() override
Get the actual current value (considering the sampling resistor)
uint8_t getBatteryStatus()
Get the battery health status.
float getThermalDieTemperature()
Get the actual die temperature.
uint16_t getThermalDieTemperatureRaw()
Get the thermal die temperature.
float getChargingPower()
Get charging power (positive or 0)
int16_t getCurrentRaw()
Get the battery current.
float getDischargingPower()
Get discharging power (positive or 0)
bool refresh() override
Refresh the AXP2602 chip data.
float getAbsolutePower()
Get the absolute power (always positive)
IRQStatus getIRQStatus()
Get the IRQ status.
void clearIRQStatus()
Clear the IRQ status.
bool isCharging() override
Checks if the battery is charging.