This commit is contained in:
82
lib/INA233/INA233.cpp
Normal file
82
lib/INA233/INA233.cpp
Normal file
@@ -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;
|
||||
}
|
||||
26
lib/INA233/INA233.h
Normal file
26
lib/INA233/INA233.h
Normal file
@@ -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;
|
||||
};
|
||||
1
lib/infinityPV_INA233_Arduino-library
Submodule
1
lib/infinityPV_INA233_Arduino-library
Submodule
Submodule lib/infinityPV_INA233_Arduino-library added at bc68e03c01
@@ -20,4 +20,4 @@ lib_deps =
|
||||
ArduinoLog
|
||||
|
||||
upload_protocol = espota
|
||||
upload_port = 10.0.3.50
|
||||
upload_port = 192.168.5.181
|
||||
34
src/main.cpp
34
src/main.cpp
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "INA226.h"
|
||||
#include "Wire.h"
|
||||
#include "INA233.h"
|
||||
|
||||
|
||||
#include <Preferences.h>
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user