ESP8266 battery monitor

For stand-alone microprocessors

ESP8266 battery monitor

Postby Doug Coulter » Wed Oct 30, 2019 5:33 pm

Since I'm doing some changes to the solar system, and have had minor issues with the existing monitor setup, I decided to make a new one based on an ESP8266, using a WeMos module.
This will use WiFi to let another host grab data from it, excluding the lightning damage issues, and has a better analog front end so should be more accurate and less susceptible to leakage currents (think spiders etc).
I used 2 ads1115 breakouts, so I cound run the a/d in continuous mode, instead of the blocking mode in the default library for these. You can't do channel switching meaningfully in that mode, but rather just grab the latest results now and then. This allowed me to also set the sample rate at its lowest limit (8 sps) so as to get the benefit of some on chip lowpassing as well as the stuff I added.
The result appears to be robust and as accurate as the meters I have to calibrate it with; kind of by definition, as I adjusted the scaling to read the same as my meters. This system has no offset.
That is, compared to my meters. If anything is drifting, it's a fluke.

I also added a sht31 temperature and humidity sensor, because why not.

Results are dispayed on an ssh 1106 128x64 OLED display so I can see if it's working locally and get a reading when I'm "right there", and because it's cool.
Here is is burning in
20191030-1743-batmon-1.jpg
Seems to work, the values read correctly with a test source. Virtually no noise pckup when open inputs are touched.

I did the software on a raspberry pi 4-4g shown here, using arduino 1.6.10, and a geany setup as an external editor to get the kind of code highlighting I like and other features the Arduino IDE lacks.
While that pi takes awhile to start the Arduino IDE, well, that IDE takes awhile (less) to start even on a big fast machine. It's not bad. I'm actually sitting elsewhere and talking to that via VNC, so it can be on the bench, while I'm on the couch with a big screen, in comfort.
20191030-1744-batmon-2.jpg
Here's the dev box, with a fan I added.


I set a static IP rather than use DHCP - I have a nice map of where things live on the LAN that I keep updated in a local version of PHPBB (along with a lot of other stuff - it's the ultimate note-to self app).

But no plan survives contact. So I have more than one way of finding things.
The sketch uses a thing that is standard here, which I call tellme - this broadcasts a hostname / IP pair to a known UDP port on the LAN, in this case port 53831. Other, more sophisticated machines (anything running linux) listen on this port and keep a version of /etc/hosts updated with what is online now, including noticing when a canary stops tweeing. So all my devices on the LAN are resolved internally.

I'm using my standard model Arduino scheduler and tiny opsys (it's nor that big a deal), and using my also standard (at least here) setup of having this be a slave that listens on a UDP port - in this case port 42042 - for commands, and simply speaks when spoken to. Whoever is interested simply asks for data, and it's sent back in human readable ASCII. Other commands are easy to add.

A benefit of this is that it's cheap...and it also hard-boots very quickly - so surprises like one might get once in awhile with something bigger (like a pi) - really, no way to get hung up - if it works at all, it works.
Further, an image is a heck of a lot easier to keep as backup than a big linux image.

So, here's the code, compressed. I had to modify the ADS1115 library, so that's in the package too. Basically, nothing special other than adding the ability to start continuous conversions.
ESP_batmon.tar.gz
sketch and new library
(9.55 KiB) Downloaded 231 times


Of perhaps peripheral interest, this is what I see when messing with the code on my nice comfy couch with a wireless keyboard and a big display (no glasses required).
devworld.png
about to open a beer after closing the file


The schematic is really simple. All the perhipherals are wired up to I2C and I used a couple of 2.7k pullups on the lines, maybe not needed (it worked without them).
The voltage a/d input is from a divider, with 3 10k in series, and a 22uf capacitor to inp_n after the first one, and a 1k to the input negative, with a 330uf capacitor across it and the input positive. Just a 31:1 divider with a couple of lowpass capacitors along the way.
Same idea for the one that measures amps, but inp_p and _n are switched, and the values are 4.7k (times two) and there is no capacitor across the inputs - just this lowpass filter.

Power ground and measurement ground aren't connected on this board. They are in the real world, but at the battery. This avoids noise from the switcher I'll be using to get 5v from the 24v nominal batteries. That ampere shunt is .001 ohm and in the negative battery lead.

All the modules are socketed and are unmodified pieces. Sometimes you need to replace one, why make it hard?
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: ESP8266 battery monitor

Postby Rex Allers » Wed Nov 06, 2019 10:50 pm

Thanks for sharing again Doug.

What is the WeMos module? Is that the processor plus WiFi board or something else? I did a bit of searching for WeMos but didn't find a match for what you are using.
Rex Allers
 
Posts: 28
Joined: Mon Sep 05, 2016 8:22 am
Location: San Jose CA

Re: ESP8266 battery monitor

Postby Doug Coulter » Thu Nov 07, 2019 6:09 pm

It's pretty much the standard thing, I got a bunch of them awhile back. There are now several flavors that should be around the same, just a matter of packaging.
WeMos is good, MakerFocus is good, HiLetGo is good..I actually haven't gotten a bad one yet. Some have nicer silkscreen but none are as nice as the picture here for knowing what pin is what.
There are some special IO pins you have to be careful about using, as they are involved in the uploading process or have to be pulled one way or the other at boot.
Note that it doesn't matter if they come with lua or micropython. The Arduino IDE will just overwrite that stuff.

This guy is a real go to resource on all things arduino and many other things. https://www.youtube.com/channel/UCu7_D0 ... pEohoP7YSQ

I used one like this (hairy amazon link hidden inside this)
Which is another (good) brand of the identical thing - I should have been more specific. If it looks like this...it's probably the same thing, they're all clones of one another.

Here's a later version that packs it tighter: another hairy amazon link

The main confusion you get is the pinouts...as of course, there are a couple of numbering schemes, one of which mirrors the UNO, and in that one, the I2C lines are 4 and 5. Since I may misremember, here's a picture I added the notation to.
8266_nodemcu_pins.png
Pinouts for the larger format dev module


Of course, if you're patient, Banggood is going to have it cheaper. Either of these above use the -12e esp8266 core, and most of it isn't really in use for this. Even though this tensilica cpu is far less code-space-efficient than an uno, there's so much more memory and speed...it's still better. here's a bunch of them on Banggood. The ones that look like the above picture are all pretty much identical. That search also shows some esp32 modules, which are another ball of wax (lots more powerful, not directly compatible, and not needed for this). https://www.banggood.com/search/esp8266.html?from=nav

If you're going to want to put one of these in a metal box, there's a version -07 module that has antenna connectors for the standard tiny plugs and most wifi antennas with the cable work with those.
Though honestly, unless you need a ton of range...it's going to be hard to get a box tight enough to stop it from working with the little PCB antenna on the above modules! A raspberry pi here, inside an aluminum chassis with a copper cover and screen on any small holes to let cables through - works out to 50 meters or so on it's wifi, and these are comparable if not better (the pi antenna isn't that great).
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: ESP8266 battery monitor

Postby Doug Coulter » Wed Jan 01, 2020 2:46 pm

I now have this burning in alongside the older unit that's seen better days. When I'm happy that it's fine (looking really good so far) - I'll hit it with conformal coating and really install it where it belongs, instead of hanging from a wire(!) in the battery compartment. One of the big failure modes of the older one was due to spiders and other bugs - the literal kind - depositing this and that slightly conductive junk on the board. When you're looking at milli and microvolts this is a problem, even with what one would normally think of as fairly low impedances. I was also having a problem when things got really cold with what looked like tons of noise overwhelming the original filtering - including the averaging of many samples per report. The "looked like" is a problem I'd rather have a real answer to before I really nail this down... After some pix...I'll include a sample perl program that isn't that useful for other than just looking at the data and putting it in a file too, to have something for this phase of things, before I integrate it into the house raspberry pi "Lan of things" master database and CGI plotting code.


The old unit was on its second pi and second uno. I was using the uno to also control the cooling system on the Lister diesel, which is no longer in service itself. Yes, I didn't do a super maintenance job, as...when this kind of thing breaks, it's always on a Friday or Saturday night in an ice storm - you just get it going again. And then once it works...it works.
shrunk-20191231-1409-ESP_batbox-3.jpg
Nasty but functioning - kind of. Amps are off enough that the integration of them to see battery amp hours is..pretty bad.


There are design changes in the new version to accommodate real life, as in - everything plugs in and is trivial to replace in-place...and generic.
shrunk-20200101-1536-ESP_batbox-2.jpg
burning it in. Only 3 wires total to battery pack. Well, 4, as I used two "ground" wires in the same lug so sensing doesn't see drop from board power.

That little 3 wire regulator on a heatsink is part of another function that runs a little vent fan, switched on a high battery volts (when they are gassing).

Here's some example code.
The code for both the ESP and this perl use hardcoded addresses and so forth - you'll probably have to change those for your setup.
ESPBat.7z
perl code to talk to the thing -
(1.13 KiB) Downloaded 213 times

ESP_bat_Schiz.gif
Schiz for the analog part
ESP_bat_Schiz.gif (3.65 KiB) Viewed 17344 times

shrunk-20191231-1507-ESP_batbox-1.jpg
The backside. This will get conformal coating.


Note that this doesn't actually need the little display - and it can be hotplugged as needed. For that matter, not having any of the peripheral modules simply means no data in that field...I learn, if slowly.
However, due to the screwiness of the I2C protocol and various implementations, and the questionable software (or even on board pushbutton!) reset ability of the ESP - it'd be a good move to put a switch in the power line.
I'll be doing that here.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA


Return to Embedded software

Who is online

Users browsing this forum: No registered users and 1 guest

cron