Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[P130] Add simple circuit to get a current sensor , result is Irms #4009

Open
wants to merge 23 commits into
base: mega
Choose a base branch
from

Conversation

handfreezer
Copy link

Note: to get real current value (not Irms), you just have to use the formulas " %value% * 1.414 ".
It is working at home from more than 4 months now, here is an example of result:
(As you can see, we can also observe my holidays...)

image

Ether and others added 17 commits February 6, 2022 12:11
…er, using ADS1015 to sample the wave and calculate current through Irms method (around 60 sample over 20ms, you can select how many wave you want to get accurate measure ;-) )
Centralised default reading conf of ADS1015
(sample count are similar between 2400 and 3300 => ESP32 too slow to benefit of 3300 BUT 2400 is more accurate!)
} else {
String log = F("Irms - ADS1015 : writeRegister NOK, errcode= ");
log += res ;
addLog(LOG_LEVEL_INFO, log);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an error, thus use LOG_LEVEL_ERROR.

res = Wire.endTransmission();
if ( 0 == res ) {
result = true;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this

} else { 
  if (logLevelActiveFor(LOG_LEVEL_ERROR)) {
    ...
  }
  result = false;
}

See also next comment about log level.

return result;
}

boolean P130_data_struct::readRegister(uint8_t registerAddr, uint16_t& registerValue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use I2C_read16_reg in ESPEasy/src/src/Helpers/I2C_access.h instead of re-implementing those I2C functions. (same for writeRegister above)

Thing is, there is lots and lots of I2C code doing it wrong (e.g. calling Wire.endTransmission() after Wire.requestForm()) and when you're using a single function like the one I linked, I know it is correct and if it isn't, I only need to fix it at one place.

currentValue = voltage * calCurrent;
}
if (false != this->debug) {
String log;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap logs like these in

#ifndef BUILD_NO_DEBUG

#endif

so the builds meant for as small builds as possible will not have debug code in them.

Also always wrap logs that need string manipulation in checks like if (logLevelActiveFor(LOG_LEVEL_XXXX)) {

@@ -1,3 +1,6 @@
# ⚠️ This is a fork of [letscontrolit/ESPEasy](https://github.com/letscontrolit/ESPEasy) and you should prefer to start from original project! This fork is for personnal maintenance and making PR to original project. ⚠️
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should go into the official ESPEasy repository... please remove this from this PR (you can of course keep it in your fork)

This is where you specify the calibration of the current sensor you connected, say you have connected a current sensor given for 50A/1V, you will indicate 50.

* **Canal 1 - Max current** : Current value for 1V
* **Canal 2 - Max current** : Current value for 1V
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Canal is most likely French, in English this should be Channel, ditto in _P130_IRMS_ADS1015.ino (and you probably need to update the screenshots too)

@@ -392,7 +392,9 @@ static const char DATA_ESPEASY_DEFAULT_MIN_CSS[] PROGMEM = {
// #define USES_P125 // ADXL345 SPI Acceleration / Gravity
// #define USES_P126 // 74HC595 Shift register
// #define USES_P127 // CDM7160

// #define USES_P128 //
// #define USES_P129 //
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P128 and P129 will define their own here, no need to fill the gap, and it will probably cause minor merge issues


#define PLUGIN_130
#define PLUGIN_ID_130 130
#define PLUGIN_NAME_130 "Current Sensor Irms - ADS1015"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To have plugin names with the same structure, this should probably be Current - ADS1015 IRMS [TESTING] (or use Irms), and include the status of the plugin (either Development or Testing).

addFormFloatNumberBox(F("Voltage Estimated"), F("p130_voltageEstimated"), PCONFIG_FLOAT(0), 0.0, 380.0, 2);
addFormSubHeader(F("Calibration"));
addFormNumericBox(F("Canal 1 - Max Current"), F("p130_calCurrent1"), PCONFIG(1), 1, 250);
addFormNumericBox(F("Canal 2 - Max Current"), F("p130_calCurrent2"), PCONFIG(2), 1, 250);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, Canal -> Channel

addFormNumericBox(F("Current Frequency"), F("p130_frequency"), PCONFIG_LONG(0), 50, 60);
addFormNumericBox(F("Nb Sinus to read"), F("p130_nb_sinus"), PCONFIG_LONG(1), 1, 25);
addFormNote("Take care! Reading sinus is a blocking process. With a SCT013 (60A/50A/30A - 1V), reading 2 sinus on 50Hz giving stable values.");
addFormCheckBox(F("ADC Conversion Mode Continous"), F("p130_adc_continous"), PCONFIG_LONG(2));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To more reliably convert that to a bool, better use PCONFIG_LONG(2) == 1 here.


PCONFIG_LONG(0) = getFormItemInt(F("p130_frequency"));
PCONFIG_LONG(1) = getFormItemInt(F("p130_nb_sinus"));
PCONFIG_LONG(2) = (true == isFormItemChecked(F("p130_adc_continous")))?1:0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C/C++ treats a non-zero value as true, so no need to compare to true

@@ -1192,6 +1191,7 @@ To create/register a plugin, you have to :
#define USES_P121 // HMC5883L
#define USES_P125 // ADXL345 SPI
#define USES_P126 // 74HC595 Shift register
#define USES_P130 // Current Sensor Irms - ADS1015
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This plugin should IMHO better fit in the PLUGIN_ENERGY_COLLECTION selection, not in the PLUGIN_SET_TESTING_E set. (And when moving there, should be updated in the documentation too).

@@ -1343,7 +1343,7 @@ To create/register a plugin, you have to :
//#define USES_P124 // Ventus_W266_RFM69
#define USES_P125 // ArduCAM
#define USES_P127 // Teleinfo
#define USES_P130 // VEML6075
#define USES_P130 // [Irms - ADS1015](https://github.com/letscontrolit/ESPEasy/issues/3839)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is defining the Experimental set of plugins, AKA ESPEasyPluginPlayGround. No need to change anything here, as that uses a separate plugin-ID range. This set can probably be removed or at least trimmed, but that should become be a separate PR, and handled by someone else 😉.


// write conf+ =>start reading
if ( false == this->writeRegister(P130_ADS1X15_REG_POINTER_CONFIG, configStartContinuous) ) {
addLog(LOG_LEVEL_INFO, F("readadccontinuous write register conf failed"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another LOG_LEVEL_ERROR to log here?

@handfreezer
Copy link
Author

Hello, I'm sorry for a so long silent from my side, I'm a quiet bit occupied, but I agree with almost.
I'll try to do changes on next weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants