[code] [blog] [Repos] [GPS Tracker] [Music] [VNC] [Restart VNC] [E-Mail me]
git clone https://coredump.ws/repos/solar-relay.git
# push (client certificate):
git remote set-url --push origin https://coredump.ws:8443/repos/solar-relay.git

Branches and tags

mainbranch25 days agozip

Files at HEAD (3ee6f8e7cd) · download zip

.gitignore184 B
config.example.h1.1 KB
LICENSE34.3 KB
README.md17.0 KB
relay.ino74.3 KB

Recent commits · all

3ee6f8e7Add HAS_SOLAR for sites without solar panelsno-body-in-particular25 days ago
c4564302Point the forecast at the shed roof: 39 degrees tilt, 16 degrees east of southno-body-in-particular25 days ago
a50b7ba1Stop the charger cycling: detect full at the end voltage, steady the solar ruleno-body-in-particular25 days ago
7f64002bPrefer the home network over phone hotspots; keep the radio awakeno-body-in-particular25 days ago
dc5145f5Hear the Shine stick over ESP-NOW as well as UDPno-body-in-particular25 days ago
c1bce3a6Charge on low battery only after a sustained drop, and for 30 minutesno-body-in-particular25 days ago
8d9e17e4License under GPL-3.0-or-laterno-body-in-particular27 days ago
d9765e45Plan charging so the battery stays above a floor until the cheap slotsno-body-in-particular2026-10-09
2dcf6a10Use the solar forecast until the inverter is heardno-body-in-particular2026-10-08
5dac7dd9Drop the DinMeter's own clamp; solar comes from the inverterno-body-in-particular2026-10-08

README.md

solar-relay

Charges a 28 V battery bank when it is cheapest, on an M5Stack DinMeter.

The DinMeter switches the battery charger with a relay. It decides when from three things:

The two solar inputs are optional: with HAS_SOLAR 0 in config.h, for a site without panels, it plans on prices alone (see Without solar panels).

It learns how much charging a day needs, and how much power your panels give per unit of forecast sunlight. It shows its state on the DinMeter's screen. Readings and a log go to a CTracker server, so you can follow it from anywhere.

Hardware

PartNotes
M5Stack DinMeter (ESP32-S3)2.4 GHz WiFi only.
Relay on G1Switches the battery charger's mains supply. High = charger on.
Battery voltage divider on G227 kΩ from battery + to G2, 1.5 kΩ from G2 to ground (ratio 1:19). Optionally 100 nF across the 1.5 kΩ. The DinMeter's ground must be the battery's minus.
BatteryWritten for a 28 V LTO bank (10 cells, 2.8 V each when full) with a 28 V / 45 A charger.

The battery voltage is read with the ESP32's calibrated analogReadMilliVolts. The divider puts 28 V at 1.47 V on the pin, well inside the ADC's linear range.

How the relay decides

Checked in this order, every pass of the loop:

  1. USB override. relay on, relay off or relay auto over the serial port; see Serial commands.
  2. Forced on. Turn the encoder to toggle. The screen confirms with "Force on : On/Off".
  3. Low battery. Only a sustained drop counts: below MIN_BATT_VOLTAGE (20 V) for 15 minutes without a break (LOW_BATT_HOLD_MS). Boiling water can pull the voltage under for 10–15 minutes and then recover. Then the charger runs for 30 minutes (LOW_BATT_CHARGE_MS), not to full, and the plan takes over again. It keeps running those 30 minutes even though charging lifts the voltage over the limit straight away.
  4. Full. The battery is full when the charger has brought it to its end voltage and it has stopped rising there: at least BATT_FULL_MIN_V (27.5 V) under charge, and less than 0.05 V higher than 5 minutes earlier. Below that, only a voltage that has been flat for a whole hour counts (a hot battery or a heavy load can keep it lower). Voltages are compared as 1-minute means, and the first 2 minutes after switch-on are ignored, because the voltage jumps.

With the charger on, the voltage reads about 2 V above rest. Mid-charge it creeps up only 0.01–0.02 V a minute with ±0.1 V of jitter, so the old test (no 0.01 V rise in 5 minutes) called the battery full at around 23.4 V and stopped almost every charge early. Real fulls level off at 27.9–28.0 V.

Charging is allowed again once the voltage has stayed 0.8 V below where it settled 10 minutes after the full (charger off) for 5 minutes, and at least 30 minutes after it got full. Measured from the under-charge voltage instead, the 2 V the charger adds counted as a drop the moment it stopped.

  1. Planned. The current quarter is one the plan picked; see Planning.
  2. Solar. The solar power (measured by the inverter, or else expected by the forecast; see Where solar power comes from) is SOLAR_ON_W (960 W) or more. The rule looks at a smoothed figure (3-minute time constant), switches off again only below SOLAR_OFF_W (800 W), and once switched stays on for at least 10 minutes or off for at least 5, so passing clouds don't make it chatter. With neither a measurement nor a forecast, or with HAS_SOLAR 0, this rule does nothing.

The screen shows which rule decided, for example Relay: On plan.

Planning

Every quarter is priced as price per kWh × the share of the charger's power that must come from the grid. The grid share is the charger's power from the wall (CHARGE_POWER_W, 1100 W here) minus the solar expected in that quarter, never below zero. Expected solar is the forecast irradiance × the learned power factor. Negative-price quarters keep their negative cost and are always picked.

Until the first full charge has been learned from, and for 15 minutes after a restart (the resting voltage needs the charger off that long), it uses the learned daily need instead, plus 25% and two quarters, scaled to the window's length.

Without solar panels

Set HAS_SOLAR 0 in config.h. The planner then counts every quarter at its full price per kWh, so it picks the cheapest quarters, with the same floor, block cost and learned charge need as above. Nothing about the sun is fetched or used:

A config.h without HAS_SOLAR counts as having panels, so existing setups keep working unchanged.

Prices come from https://www.stroomperuur.nl/ajax/tarieven.php?leverancier=17&kwartier=1&datum=YYYY-MM-DD. The total per quarter is the market price, plus energy tax and VAT, plus the supplier's purchase fee. leverancier picks the supplier whose fee is added. On the two daylight-saving days the site returns 93 or 95 values; each is placed by its real time.

The forecast is Open-Meteo's global_tilted_irradiance per 15 minutes, for the panel tilt and azimuth set in relay.ino. Each value is the mean of the 15 minutes before its timestamp.

What it learns

Both values are kept in flash (NVS namespace relay) and survive restarts and uploads.

The screen

WiFi:Y Price:Y Sun:Y      connected / today's prices / forecast in
Err: None                 last fetch error, if any
Time: 14:03:27
Next: 13:15-15:30         next planned block ("tm" = tomorrow)
Relay: On plan            relay state and the rule that decided
Bat 26.41V full           battery voltage, "full" once detected
Solar: 1240W (inv)        solar power, and where it came from: "inv" or "fcst"
PV 2150W need 4.5h        power factor and learned daily need

Controls

ActionEffect
Turn the encoderToggle forced on
Hold the button 5 sSave the power factor and power off

Remote logging and chart

The device reports to a CTracker server (LOG_HOST, port 9000) using its plain-text "basic" phone protocol, under the made-up IMEI in config.h. Add that IMEI to your account on the tracker's website.

Every minute it sends:

It also sends EVENT;<imei>;<text>! for the tracker's Events list, for things worth seeing at a glance:

This needs a CTracker server with EVENT support in its basic protocol. An older server just puts them in its log as unknown messages.

The server takes one message per pass of its loop (about 100 ms) and discards whatever is unprocessed once the connection closes. So messages are sent 150 ms apart, and each connection is closed half a second after its last message. At most 20 log lines go per connection; the rest wait for the next minute.

The tracker protocol has no authentication: anyone who knows the IMEI can send data under it.

WiFi and lost internet

At startup, and whenever it's offline, the device scans and joins the strongest network from WIFI_NETWORKS that's in range. It joins that exact access point, so a router and its extender with the same name are told apart.

Being connected isn't the same as being online, so every request that gets through (tracker, prices, forecast) counts as proof that the network works:

Serial commands

Over USB at 115200 baud, one command per line:

CommandEffect
relay on / relay offOverride the relay
relay autoBack to normal operation
adcBattery voltage over one second, and the current solar power and its source
netTest the connection now: network, IP, DNS lookup of the tracker, TCP to its port
wifi <n> / wifi autoUse only network n from config.h (0 = first), or any of them again

Configuration

  1. Copy config.example.h to config.h and fill in:
  2. WIFI_NETWORKS (2.4 GHz only; names must match exactly, including trailing spaces);
  3. HAS_SOLAR: 1 with solar panels, 0 without (prices only);
  4. SOLAR_LAT and SOLAR_LON (only with panels);
  5. LOG_IMEI.

config.h is in .gitignore.

  1. Check the settings at the top of relay.ino:
  2. pins;
  3. SOLAR_ON_W and SOLAR_OFF_W;
  4. CHARGE_POWER_W;
  5. the divider resistors;
  6. PANEL_TILT and PANEL_AZIMUTH (0 = south, −90 = east, 90 = west).
  7. Calibrate the battery voltage: measure the battery with a multimeter while reading Bat on the screen. Set BATT_CALIBRATION to multimeter ÷ screen, times its current value.

Building

Arduino core esp32 3.3.x, with the libraries M5DinMeter, M5Unified, M5GFX and ArduinoJson 7.

arduino-cli compile --upload -p COM8 --fqbn esp32:esp32:m5stack_dinmeter:PartitionScheme=huge_app relay

Replace COM8 with your port. The "Huge APP" layout is required: with Bluetooth the build is about 1.5 MB, too big for the default layout. The settings area stays where it was, so learned values survive switching.

Where solar power comes from

The DinMeter has no clamp of its own. The first of these that's available is used:

  1. The inverter itself (shown as (inv) on the screen). A Growatt ShineWiFi-X stick running the shine-stick firmware broadcasts GW … pac=<W> … src=shine over UDP port 47800 every 5 seconds. A reading counts as fresh for 30 seconds. It's only heard while the DinMeter is on the same network as the stick.
  2. The forecast ((fcst)): this quarter's forecast irradiance × the power factor.
  3. A Bluetooth "SolarClamp" clamp meter ((BT), manufacturer data FF FF 'S' 'C' <W lo> <W hi> <seq>) can go before the forecast again by setting USE_BT_CLAMP 1. It's off by default: clamps proved unreliable, and without it the DinMeter doesn't scan Bluetooth at all.

Only measured watts (the inverter, or the clamp meter if enabled) teach the power factor and are charted as solar_w. Learning from the forecast would only compare it with itself. The forecast's expectation is charted separately as solar_fcst_w. With neither a measurement nor a forecast, solar is unknown, and the charger isn't run on solar.

The 5-minute status line in the log says which source is in use.

License

GPL-3.0-or-later, see LICENSE.