From 1d19e23df60d8e0a011cc911b09cfb3e1d5b3bad Mon Sep 17 00:00:00 2001 From: Tobias Date: Sun, 28 Jul 2024 17:45:49 +0200 Subject: [PATCH] Added INA233 --- lib/INA233/INA233.cpp | 82 +++++++++++++++++++++++++++ lib/INA233/INA233.h | 26 +++++++++ lib/infinityPV_INA233_Arduino-library | 1 + platformio.ini | 2 +- src/main.cpp | 34 +++++++---- 5 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 lib/INA233/INA233.cpp create mode 100644 lib/INA233/INA233.h create mode 160000 lib/infinityPV_INA233_Arduino-library diff --git a/lib/INA233/INA233.cpp b/lib/INA233/INA233.cpp new file mode 100644 index 0000000..11ad259 --- /dev/null +++ b/lib/INA233/INA233.cpp @@ -0,0 +1,82 @@ +#include "INA233.h" + +INA233::INA233(const uint8_t address, TwoWire *wire) +{ + _address = address; + _wire = wire; + // not calibrated values by default. + _current_LSB = 0; + _maxCurrent = 0; + _shunt = 0; +} + +bool INA233::begin(const uint8_t sda, const uint8_t scl) +{ + _wire->begin(sda, scl); + if (! isConnected()) return false; + return true; +} + +bool INA233::isConnected() +{ + _wire->beginTransmission(_address); + return ( _wire->endTransmission() == 0); +} + +float INA233::getBusVoltage() { + uint8_t data[2]; + int16_t rawVoltage; + + // Request data from the PMBus device + Wire.beginTransmission(INA233::_address); + Wire.write(REGISTER_READ_VIN); + Wire.endTransmission(); + Wire.requestFrom(INA233::_address, 2); + + if (Wire.available() == 2) { + data[0] = Wire.read(); + data[1] = Wire.read(); + } else { + // Handle error (e.g., return 0.0 or a special error value) + return 0.0; + } + + // Combine the two bytes into a single 16-bit value + rawVoltage = (data[1] << 8) | data[0]; + + // Convert raw value to actual voltage using the appropriate scaling + // For example, assuming a linear scaling with a coefficient of 1.25 mV per LSB: + float voltage = rawVoltage * 1.25 / 1000.0; + + return voltage; +} + +float INA233::getShuntVoltage() { + uint8_t data[2]; + int16_t rawVoltage; + + // Request data from the PMBus device + Wire.beginTransmission(INA233::_address); + Wire.write(REGISTER_READ_VSHUNT); + Wire.endTransmission(); + Wire.requestFrom(INA233::_address, 2); + + if (Wire.available() == 2) { + data[0] = Wire.read(); + data[1] = Wire.read(); + } else { + // return 0 on error + return 0.0; + } + + // Combine the two bytes into a single 16-bit value + rawVoltage = (data[1] << 8) | data[0]; + + // convert it to volts + float voltage = rawVoltage * pow(10,-4); + return voltage; +} + +float INA233::getShuntVoltage_mV() { + return getShuntVoltage() * 1000; +} \ No newline at end of file diff --git a/lib/INA233/INA233.h b/lib/INA233/INA233.h new file mode 100644 index 0000000..a86e93e --- /dev/null +++ b/lib/INA233/INA233.h @@ -0,0 +1,26 @@ +#pragma once + +#include "Arduino.h" +#include "Wire.h" + + +#define REGISTER_READ_VIN 0x88 +#define REGISTER_READ_VSHUNT 0xd1 + +class INA233{ + public: + INA233(uint8_t addr, TwoWire *wire = &Wire); + bool begin(const uint8_t sda, const uint8_t scl); + float getBusVoltage(void); + float getShuntVoltage_mV(void); + float getShuntVoltage(void); + bool isConnected(void); + + private: + float _current_LSB; + float _shunt; + float _maxCurrent; + + uint8_t _address; + TwoWire * _wire; +}; diff --git a/lib/infinityPV_INA233_Arduino-library b/lib/infinityPV_INA233_Arduino-library new file mode 160000 index 0000000..bc68e03 --- /dev/null +++ b/lib/infinityPV_INA233_Arduino-library @@ -0,0 +1 @@ +Subproject commit bc68e03c01b931a65f384ad57c014ef1c6872d56 diff --git a/platformio.ini b/platformio.ini index 29ba966..1b085ee 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,4 +20,4 @@ lib_deps = ArduinoLog upload_protocol = espota -upload_port = 10.0.3.50 \ No newline at end of file +upload_port = 192.168.5.181 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 78e555b..4438dd3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include "INA226.h" #include "Wire.h" +#include "INA233.h" #include @@ -83,13 +84,14 @@ WaterData water_data; int64_t mac_address = ESP.getEfuseMac(); -//#define INA226 -// #ifdef INA226 +//#define USE_INA226 + +#ifdef USE_INA226 INA226 ina_sensor(0x40); -// #else -// INA233 ina_sensor(); -// #endif +#else +INA233 ina_sensor(0x40); +#endif Preferences prefs; @@ -173,6 +175,7 @@ void display_task(void* parameter) if (!is_error()) { // We have no error, refresh status display and wait half a second display_percentage(water_data.percentage); + delay(1000); } else { Log.verbose("Error detected"); // We have an error, display error code for 3 seconds and then water level for 3 seconds @@ -210,7 +213,7 @@ void wifi_task(void* parameter) String old_ssid = prefs.getString(ssid_key, "xxx"); while (prefs.getString(ssid_key, "") == "" || prefs.getString(ssid_key, "") == old_ssid) { - delay(1000); + delay(5000); } failed_connection_attempts = 0; @@ -264,8 +267,10 @@ void read_sensor_task(void* parameter) { while (true) { // Get Values from sensor + float bus_voltage = ina_sensor.getBusVoltage(); float shunt_voltage = ina_sensor.getShuntVoltage_mV() - zero_value; + float shunt_current = shunt_voltage / RESISTOR_VALUE; // Get values from storage @@ -348,10 +353,19 @@ void setup() Log.verbose("Begin INA"); ina_sensor.begin(33, 32); display_error_code(20); - ina_sensor.setMaxCurrentShunt(0.02, 4, false); - ina_sensor.setBusVoltageConversionTime(7); - ina_sensor.setShuntVoltageConversionTime(7); - ina_sensor.setAverage(4); + #ifdef USE_INA226 + ina_sensor.setMaxCurrentShunt(0.02, 4, false); + ina_sensor.setBusVoltageConversionTime(7); + ina_sensor.setShuntVoltageConversionTime(7); + ina_sensor.setAverage(4); + #else + //ina_sensor.setMaxCurrentShunt(0.02, 4, false); + //ina_sensor.setBusVoltageConversionTime(7); + //ina_sensor.setShuntVoltageConversionTime(7); + //ina_sensor.setAverage(4); + #endif + + display_error_code(21); display_error_code(22);