I'm using two SCT013 current transformers to measure power consumption in the office, not for costing purposes, but just to get an idea of the power consumption. They are connected to the analog inputs of an Esp32. However, the measurements have a lot of noise and are very inaccurate at lower values.
In a new attempt, I'm using the same SCT-013's but now connected to an ADS1115 analog to digital converter. I want to use the ADS1115 in differential mode, uncoupling the SCT013's completely from any noise caused by the Esp32.
The circuit is simple, like this but with the second SCT013 connected to analog inputs A2 and A3.
Tasmota does not support differential mode of the ADS1115 so I had the option of either adapting the xsns_12_ads1115_ada.ino module or to write my own driver. Standard scripting did not give enough control of I2C devices, but Berry scripting does, so I decided to try Berry Scripting.
The long and short of it is: It works, but here are a few caveats. I'm not using the latest version of Tasmota but are on 9.4.0 so some problems I encountered may already be solved.
First of all, there are some errors:
- I experienced restarts every 45 minutes or so. This may have to do with garbage collection. I added a call to tasmota.gc in the json_append() override so this call is issued every teleperiod. This looks promising, no restart yet after 2.5 hours and counting.
- Editing a saved program in the file system console is possible, but the program will not work correctly thereafter, even when the compiled Berry program is removed and the Esp32 restarted.
- Uploading a new program file after first deleting the old sometimes causes a restart, after which the program can be uploaded correctly.
Then there are some things you just have to experience:
- In Berry scripting all integers are 32 bit integers. This caused some problems in issuing commands to the ADS1115. For write commands the only way to send a 16 bit value to the configuration register is to use the write_bytes function and a byte array. When reading the 16 bit conversion result with the read function, the result is placed in the lower 16 bits of a 32 bits integer and you have to explicitly propagate the sign bit to the upper 16 bits.
- The Math module does not seem to be implemented, so you have to code functions like abs() yourself.
- I could not find any documentation of the Berry string module, but I probably did not search long enough.
- There does not seem to be a way to remove a driver once you have added it, so you need to restart often.
- The contents of the file system are not saved when you backup the configuration of the Esp32, so you need to remember to save your scripts yourself.
- I am still looking for a way to keep some variables over a restart. There is a save_before_restart() module override in the driver class, but I cannot find any documentation on how to implement one.
As an illustration I include a copy of the script as it is now. It is by no means complete yet and still contains some things that are only needed for debugging.