// ACS712 Arduino code //new version with filter in secrets.h: //#define useACS712 #define ACS712useFilter true #define ACS712diag true //false // every second print and now also send to mqtt /log in main.ino // declare #ifdef useACS712 float Coffset = 1.650; float Cfix = 8.190; // aka 5.0/3.3 / 0.185 Vdivider and hall factor float Ctune = 1.0; // float ACS712amp = 0.0; String ACS712s = ""; // add filter float Ma = 0.15; float Mb = 1.0 - Ma; float A0signal = 0.0; boolean Finit = true; #endif in main.ino // setup #ifdef useACS712 msg="ACS712 enabled"; Serial.println(msg); #endif in main.ino // 1 sec job // Ains() // after reading Ains, before printing values #ifdef useACS712 // here use the A0 sensorA0val (Volt) and calc about the ACS AMP ( for 5Amp version 0.185 V/A) // Coffset = 1.650; // Cfix = 8.190; Ctune = 1.0; ACS712amp = 0.0; ACS712s = ""; // add filter float Ma = 0.15; float Mb = 1.0 - Ma; float A0signal = 0.0; boolean useFilter = true; boolean Finit = true; //sensorA0val = 1.65; // _____________________________ simulate input test if no hardware available if ( Finit ) { Finit = false; A0signal = sensorA0val; // ________________________ init to avoid the first 15 sec SWING IN } if ( ACS712useFilter ) { A0signal = Ma * sensorA0val + Mb * A0signal; } else { A0signal = sensorA0val; } ACS712amp = ( A0signal - Coffset ) * Cfix * Ctune; if ( ACS712diag ) { // diagnostic print // now make diagnostic print a mqtt dataset ACS712s = "{ \"input\": "+String(sensorA0val,3)+", \"Ma\": "+String(Ma,3)+", \"Mb\": "+String(Mb,3)+", \"Filter\": "+String(ACS712useFilter)+", \"A0signal\": "+String(A0signal,3)+", \"Coffset\": "+String(Coffset,3)+", \"Cfix\": "+String(Cfix,3)+", \"Ctune\": "+String(Ctune,3)+", \"ACS712amp\": " + String(ACS712amp,3)+" }"; Serial.println(ACS712s); BootLog = ACS712s; send_MQTT_LOG(); } sensorA0val = ACS712amp; // ________________________ temporary overwrite the measured Volt with the ACS712 calculation and it goes all the way to broker and database without any code #endif