diff --git a/examples/serial-test/serial-test.ino b/examples/serial-test/serial-test.ino new file mode 100644 index 0000000..cccee7e --- /dev/null +++ b/examples/serial-test/serial-test.ino @@ -0,0 +1,32 @@ +#include +#include + +SolarCharger solar; + +void setup() { + Wire.begin(); + Serial.begin(9600); +} + +void loop() { + solar.update(); + + if (solar.isConnected()) { + Serial.print("Solar Panel Voltage: "); + Serial.println(solar.getSolarPanelVoltage()); + Serial.print("Battery Voltage: "); + Serial.println(solar.getBatteryVoltage()); + Serial.print("Battery Level: "); + Serial.println(solar.getBatteryLevel()); + Serial.print("Charging: "); + Serial.println(solar.isCharging() ? "Yes" : "No"); + Serial.print("Fast Charging: "); + Serial.println(solar.isFastCharging() ? "Yes" : "No"); + Serial.print("Battery Temperature: "); + Serial.println(solar.getBatteryTemperature()); + } else { + Serial.println("Solar charger not connected."); + } + + delay(1000); +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..21c0d13 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=SolarChargerSB041 +version=1.0.0 +author=Dein Name +maintainer=Dein Name +sentence=Eine Arduino-Bibliothek für das Auslesen des Solarladereglers SB-041. +paragraph=Diese Bibliothek ermöglicht das einfache Auslesen von Daten wie Batterie- und Solarpanel-Spannung, Ladezustand und Temperatur. +category=Sensors +url=https://github.com/dein-repo +architectures=* diff --git a/src/SolarChargerSB041.cpp b/src/SolarChargerSB041.cpp new file mode 100644 index 0000000..4d0bbbb --- /dev/null +++ b/src/SolarChargerSB041.cpp @@ -0,0 +1,64 @@ +#include "SolarChargerSB041.h" + +SolarChargerSB041::SolarChargerSB041(uint8_t address) + : address(address), + connected(false), + solarPanelVoltage(-1), + batteryVoltage(-1), + batteryLevel(-1), + charging(false), + fastCharging(false), + batteryTemperature(-1) {} + +void SolarChargerSB041::update() { + Wire.beginTransmission(address); + byte error = Wire.endTransmission(); + if (error == 0 || error == 2) { + connected = (error == 0); + Wire.requestFrom((uint8_t)address, (uint8_t)4); + + uint8_t vbat_raw = Wire.read(); + batteryVoltage = 0.02 * vbat_raw; + + uint8_t vin_raw = Wire.read(); + solarPanelVoltage = 0.1 * vin_raw; + + uint8_t flags = Wire.read(); + charging = flags & 1; + if (flags & 32) + batteryLevel = 4; + else if (flags & 16) + batteryLevel = 3; + else if (flags & 8) + batteryLevel = 2; + else if (flags & 4) + batteryLevel = 1; + else + batteryLevel = 0; + + fastCharging = flags & 64; + charging = flags & 128; + + batteryTemperature = (int8_t)(Wire.read()); + } else { + connected = false; + fastCharging = false; + charging = false; + batteryVoltage = -1; + solarPanelVoltage = -1; + batteryLevel = -1; + batteryTemperature = -1; + } +} + +bool SolarChargerSB041::isConnected() const { return connected; } +float SolarChargerSB041::getSolarPanelVoltage() const { + return solarPanelVoltage; +} +float SolarChargerSB041::getBatteryVoltage() const { return batteryVoltage; } +int SolarChargerSB041::getBatteryLevel() const { return batteryLevel; } +bool SolarChargerSB041::isCharging() const { return charging; } +bool SolarChargerSB041::isFastCharging() const { return fastCharging; } +float SolarChargerSB041::getBatteryTemperature() const { + return batteryTemperature; +} diff --git a/src/SolarChargerSB041.h b/src/SolarChargerSB041.h new file mode 100644 index 0000000..45032be --- /dev/null +++ b/src/SolarChargerSB041.h @@ -0,0 +1,60 @@ +#ifndef SOLARCHARGER_H +#define SOLARCHARGER_H + +#include + +class SolarCharger { + public: + SolarCharger(uint8_t address = 0x32); +#ifndef SOLARCHARGER_H +#define SOLARCHARGER_H + +#include + + class SolarCharger { + public: + SolarCharger(uint8_t address = 0x32); + + void update(); + bool isConnected() const; + float getSolarPanelVoltage() const; + float getBatteryVoltage() const; + int getBatteryLevel() const; + bool isCharging() const; + bool isFastCharging() const; + float getBatteryTemperature() const; + + private: + uint8_t address; + bool connected; + float solarPanelVoltage; + float batteryVoltage; + int batteryLevel; + bool charging; + bool fastCharging; + float batteryTemperature; + }; + +#endif // SOLARCHARGER_H + + void update(); + bool isConnected() const; + float getSolarPanelVoltage() const; + float getBatteryVoltage() const; + int getBatteryLevel() const; + bool isCharging() const; + bool isFastCharging() const; + float getBatteryTemperature() const; + + private: + uint8_t address; + bool connected; + float solarPanelVoltage; + float batteryVoltage; + int batteryLevel; + bool charging; + bool fastCharging; + float batteryTemperature; +}; + +#endif // SOLARCHARGER_H \ No newline at end of file