First impressions using Berry Scripting Language

1,358 views
Skip to first unread message

FransOv

unread,
Sep 8, 2021, 6:58:13 AM9/8/21
to TasmotaUsers
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.
ADS1115-SCT013.jpg
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.

ads1115sct013.be

Stephan Hadinger

unread,
Sep 8, 2021, 2:04:09 PM9/8/21
to TasmotaUsers
Thanks for your feedback. Actually it would be easier to discuss in a GitHub issue.

Berry is still under high evolution, and many of your issues are solved or about to be. I would advise to upgrade to the latest version 9.5.0.8 since there were many improvements lately.

I converted the documentation from LATEX to Markdown, it's usable but still not up to date with the latest improvements.

Also, Berry now compiles in `strict` mode which helps resolving bugs, but can refuse to compile previous code. See https://github.com/berry-lang/berry/pull/121



Let me try to answer point by point, although some items will need a dive deep:
  • 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.
    • The Math module is supported but still lacks abs, min, max
  • 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.
    • Correct, I just added `tasmota.remove_driver()`
  • 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.
    • Correct, this is a more general issue on Tasmota
  • 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.
    • I'm writing simple code to load/store objects as JSON with automatic introspection. That may help you.
Finally, I introduced static members for classes, which should simplify your code whenever you need constants https://github.com/berry-lang/berry/pull/113

Can we continue the discussion on GitHub?

Thanks
Stephan

FransOv

unread,
Sep 9, 2021, 7:23:09 AM9/9/21
to TasmotaUsers
Thanks for the update.
I'm on  9.5.0.8 now and the problem with editing the Berry script in the file management console seems to be solved. The problem with the frequent restart also looks to be gone.
I'll use GitHub to discuss further issues.

One issue that is strictly Tasmota is that I cannot seem to prevent the standard ads1115 module from loading and executing. I not only do not have a USE_ADS1115 definition in my user_config_override.h but explicitly undefine  it by #undef USE_ADS1115. At startup the ADS1115 is recognized and the callback functions in xsns_12_ads1115.ino are executed even though all the code in that module is bracketed by #ifdef USE_ADS1115 ... #endif. I had to comment out the code to prevent it from accessing the ADS1115.

Op woensdag 8 september 2021 om 20:04:09 UTC+2 schreef stephan....@gmail.com:

sfromis

unread,
Sep 11, 2021, 9:29:54 AM9/11/21
to TasmotaUsers
To disable the standard ADS1115 driver from interfering, just use the command "I2CDriver13 0". Your lack of success removing it build time may be up to not getting the build process working all the way. There are several details needing to be done just right.

FransOv

unread,
Sep 22, 2021, 4:57:54 AM9/22/21
to TasmotaUsers
If I disable i2cdriver13, I also cannot access the ads1115 in the Berry scripting.

Op zaterdag 11 september 2021 om 15:29:54 UTC+2 schreef sfromis:
Reply all
Reply to author
Forward
0 new messages