Skip to content

Commit

Permalink
Merge pull request #258 from stuartpittaway/wifi_mqtt_fixes
Browse files Browse the repository at this point in the history
Wifi mqtt fixes/home assistant
  • Loading branch information
stuartpittaway authored Nov 25, 2023
2 parents 0f7f03c + 5e00989 commit 0ee4ed0
Show file tree
Hide file tree
Showing 29 changed files with 1,698 additions and 529 deletions.
117 changes: 117 additions & 0 deletions ESPController/Home_Assistant_RESTful_API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# diyBMS v4
## Home Assistant RESTful API integration



### Secrets Configuration YAML file for Home Assistant
```
# Use this file to store secrets like usernames and passwords.
# Learn more at https://www.home-assistant.io/docs/configuration/secrets/
diybms_api_token: XXXXXXXXXXXXXXXXXXXXXXXX
```

### Example Configuration YAML file for Home Assistant
```
# Example configuration.yaml entry for Home Assistant integration with DIYBMS
rest:
- resource: http://192.168.0.70/ha
scan_interval: 10
timeout: 5
method: "GET"
headers:
Content-Type: application/json
ApiKey: !secret diybms_api_token
sensor:
- unique_id: "diybms.activerules"
value_template: "{{ value_json.activerules }}"
name: "Active rules"
state_class: "measurement"
- unique_id: "diybms.chargemode"
value_template: "{{ value_json.chgmode }}"
name: "Charge mode"
state_class: "measurement"
- unique_id: "diybms.lowest_bank_voltage"
value_template: "{{ value_json.lowbankv }}"
name: "Lowest bank voltage"
unit_of_measurement: "mV"
device_class: "voltage"
- unique_id: "diybms.highest_bank_voltage"
value_template: "{{ value_json.highbankv }}"
name: "Highest bank voltage"
unit_of_measurement: "mV"
device_class: "voltage"
- unique_id: "diybms.lowest_cell_voltage"
value_template: "{{ value_json.lowcellv }}"
name: "Lowest cell voltage"
unit_of_measurement: "mV"
device_class: "voltage"
- unique_id: "diybms.highest_cell_voltage"
value_template: "{{ value_json.highcellv }}"
name: "Highest cell voltage"
unit_of_measurement: "mV"
device_class: "voltage"
- unique_id: "diybms.highest_external_temp"
value_template: "{{ value_json.highextt }}"
name: "Highest cell temperature"
unit_of_measurement: "°C"
device_class: "temperature"
- unique_id: "diybms.highest_internal_temp"
value_template: "{{ value_json.highintt }}"
name: "Highest passive balance temperature"
unit_of_measurement: "°C"
device_class: "temperature"
- unique_id: "diybms.current"
value_template: "{% if 'c' in value_json %}{{ value_json.c }}{% else %}0{% endif %}"
name: "DC Current"
unit_of_measurement: "A"
device_class: "current"
icon: "mdi:current-dc"
- unique_id: "diybms.voltage"
value_template: "{% if 'v' in value_json %}{{ value_json.v }}{% else %}0{% endif %}"
name: "DC voltage"
unit_of_measurement: "V"
device_class: "voltage"
- unique_id: "diybms.power"
value_template: "{% if 'pwr' in value_json %}{{ value_json.pwr }}{% else %}0{% endif %}"
name: "Battery power"
unit_of_measurement: "W"
device_class: "power"
- unique_id: "diybms.stateofcharge"
value_template: "{% if 'soc' in value_json %}{{ value_json.soc }}{% else %}0{% endif %}"
name: "State of charge"
unit_of_measurement: "%"
device_class: "battery"
- unique_id: "diybms.dynamic_charge_voltage"
value_template: "{% if 'dyncv' in value_json %}{{ value_json.dyncv }}{% else %}0{% endif %}"
name: "Dynamic charge voltage"
unit_of_measurement: "V"
device_class: "voltage"
- unique_id: "diybms.dynamic_charge_current"
value_template: "{% if 'dyncc' in value_json %}{{ value_json.dyncc }}{% else %}0{% endif %}"
name: "Dynamic charge current"
unit_of_measurement: "A"
device_class: "current"
binary_sensor:
- unique_id: "diybms.charge_allowed"
value_template: "{{ value_json.chgallow }}"
name: "Battery charging allowed"
- unique_id: "diybms.discharge_allowed"
value_template: "{{ value_json.dischgallow }}"
name: "Battery discharging allowed"
```
110 changes: 0 additions & 110 deletions ESPController/TODOLIST.md

This file was deleted.

2 changes: 1 addition & 1 deletion ESPController/include/HAL_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class HAL_ESP32
SPIClass *VSPI_Ptr();

void Led(uint8_t bits);
void ConfigureCAN(uint16_t canbusbaudrate) const;
void ConfigurePins();
void TFTScreenBacklight(bool Status);

void CANBUSEnable(bool value);
void ConfigureCAN();

bool IsVSPIMutexAvailable()
{
Expand Down
7 changes: 5 additions & 2 deletions ESPController/include/Rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,13 @@ class Rules
// Number of modules who have reported zero volts (bad!)
uint8_t zeroVoltageModuleCount;

// Highest pack voltage (millivolts)
// Highest pack voltage (millivolts) & address
uint32_t highestBankVoltage;
// Lowest pack voltage (millivolts)
uint8_t address_highestBankVoltage;

// Lowest pack voltage (millivolts) & address
uint32_t lowestBankVoltage;
uint8_t address_lowestBankVoltage;

// Highest cell voltage in the whole system (millivolts)
uint16_t highestCellVoltage;
Expand Down
11 changes: 10 additions & 1 deletion ESPController/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ enum CanBusProtocolEmulation : uint8_t
{
CANBUS_DISABLED = 0x00,
CANBUS_VICTRON = 0x01,
CANBUS_PYLONTECH = 0x02
CANBUS_PYLONTECH = 0x02,
CANBUS_PYLONFORCEH2 = 0x03
};

enum CurrentMonitorDevice : uint8_t
Expand Down Expand Up @@ -183,6 +184,9 @@ struct diybms_eeprom_settings

CanBusProtocolEmulation canbusprotocol;
CanBusInverter canbusinverter;
//CANBUS baud rate, 250=250k, 500=500k
uint16_t canbusbaud;
//Nominal battery capacity (amp hours)
uint16_t nominalbatcap;
// Maximum charge voltage - scale 0.1
uint16_t chargevolt;
Expand Down Expand Up @@ -230,6 +234,8 @@ struct diybms_eeprom_settings

// NOTE this array is subject to buffer overflow vulnerabilities!
bool mqtt_enabled;
// Only report basic cell data (voltage and temperture) over MQTT
bool mqtt_basic_cell_reporting;
char mqtt_uri[128 + 1];
char mqtt_topic[32 + 1];
char mqtt_username[32 + 1];
Expand All @@ -245,6 +251,9 @@ struct diybms_eeprom_settings

// Holds a bit pattern indicating which "tiles" are visible on the web gui
uint16_t tileconfig[5];

uint8_t canbus_equipment_addr; // battery index on the same canbus for PYLONFORCE, 0 - 15, default 0
char homeassist_apikey[24+1];
};

typedef union
Expand Down
26 changes: 26 additions & 0 deletions ESPController/include/pylonforce_canbus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef DIYBMS_PYLONFORCE_CANBUS_H_
#define DIYBMS_PYLONFORCE_CANBUS_H_

#include "defines.h"
#include "Rules.h"
#include <driver/twai.h>


void pylonforce_handle_rx(twai_message_t *);
void pylonforce_handle_tx();


extern uint8_t TotalNumberOfCells();
extern Rules rules;
extern currentmonitoring_struct currentMonitor;
extern diybms_eeprom_settings mysettings;
extern std::string hostname;
extern ControllerState _controller_state;
extern uint32_t canbus_messages_failed_sent;
extern uint32_t canbus_messages_sent;
extern uint32_t canbus_messages_received;
extern bool wifi_isconnected;

extern void send_ext_canbus_message(const uint32_t identifier, const uint8_t *buffer, const uint8_t length);

#endif
2 changes: 2 additions & 0 deletions ESPController/include/webserver_json_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern void setCacheControl(httpd_req_t *req);
extern void configureSNTP(long gmtOffset_sec, int daylightOffset_sec, const char *server1);
extern void DefaultConfiguration(diybms_eeprom_settings *_myset);
extern bool SaveWIFIJson(const wifi_eeprom_settings* setting);
extern void randomCharacters(char *value, int length);

esp_err_t post_savebankconfig_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_saventp_json_handler(httpd_req_t *req, bool urlEncoded);
Expand Down Expand Up @@ -67,6 +68,7 @@ esp_err_t post_avrprog_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_savecurrentmon_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_saverules_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_restoreconfig_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_homeassistant_apikey_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t save_data_handler(httpd_req_t *req);

#endif
18 changes: 17 additions & 1 deletion ESPController/include/webserver_json_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

esp_err_t api_handler(httpd_req_t *req);
esp_err_t content_handler_downloadfile(httpd_req_t *req);
esp_err_t ha_handler(httpd_req_t *req);

esp_err_t SendFileInChunks(httpd_req_t *req, FS &filesystem, const char *filename);
int fileSystemListDirectory(char *buffer, size_t bufferLen, fs::FS &fs, const char *dirname, uint8_t levels);
Expand All @@ -42,7 +43,7 @@ extern uint32_t canbus_messages_received_error;

extern Rules rules;
extern ControllerState _controller_state;
extern void formatCurrentDateTime(char* buf, size_t buf_size);
extern void formatCurrentDateTime(char *buf, size_t buf_size);
extern void setNoStoreCacheControl(httpd_req_t *req);
extern char CookieValue[20 + 1];
extern std::string hostname;
Expand All @@ -56,4 +57,19 @@ extern CurrentMonitorINA229 currentmon_internal;
extern History history;
extern wifi_eeprom_settings _wificonfig;
extern esp_err_t diagnosticJSON(httpd_req_t *req, char buffer[], int bufferLenMax);

extern bool mqttClient_connected;
extern uint16_t mqtt_error_connection_count;
extern uint16_t mqtt_error_transport_count;
extern uint16_t mqtt_connection_count;
extern uint16_t mqtt_disconnection_count;

extern uint16_t wifi_count_rssi_low;
extern uint16_t wifi_count_sta_start;
extern uint16_t wifi_count_sta_connected;
extern uint16_t wifi_count_sta_disconnected;
extern uint16_t wifi_count_sta_lost_ip;
extern uint16_t wifi_count_sta_got_ip;

extern bool wifi_isconnected;
#endif
Loading

0 comments on commit 0ee4ed0

Please sign in to comment.