# JK BMS → Cloud setup (bms.hedrasolutions.lk)

## 1. Server (cPanel)
1. Create subdomain `bms.hedrasolutions.lk` and turn on SSL (AutoSSL / Let's Encrypt).
2. Upload everything in the `bms/` folder to the subdomain's document root.
3. MySQL Databases: create a database + user, give the user ALL privileges.
4. phpMyAdmin → select the database → Import → `schema.sql`.
5. Edit `config.php`:
   - DB_NAME / DB_USER / DB_PASS
   - BMS_API_KEY (long random string, same as in ESP32 secrets.yaml)
   - DASH_PASSWORD (dashboard login)
   - SMS_TO (your number, 94XXXXXXXXX)
   - `send_sms()` – paste the same SMS gateway call your garage app uses
6. Open https://bms.hedrasolutions.lk and log in. It shows "No data yet" until the ESP32 sends.

## 2. ESP32
1. In `secrets.yaml` set WiFi, `bms_cloud_url` and `bms_api_key`.
2. `esphome run jk-bms-wifi.yaml` (USB the first time, over WiFi afterwards).
3. Within ~30 s the dashboard should show live data.

## How records work
- Current > +0.5 A = charging, < −0.5 A = discharging (change in config.php).
- A record ends when the direction changes, or after 5 min idle.
- Each record stores start/end time, SOC, voltage, Ah, kWh and peak current.
- "Export CSV" downloads all records.
- If charging shows as discharging, set `CURRENT_SIGN = -1` in config.php.

## SMS
- Sent only when the BMS reports an error (e.g. cell over/undervoltage, over-temperature).
- Same error still active → reminder every 6 h (SMS_REPEAT_HOURS).
- One "Battery OK again" SMS when it clears (SMS_ON_CLEAR = false to turn off).
- Every alert is also listed on the dashboard.

Raw readings are kept 90 days (KEEP_READINGS_DAYS); charge/discharge records and alerts are kept forever.
