Skip to content

Commit

Permalink
Added rssi to scan results. Changed poll command to settime. Fixed js…
Browse files Browse the repository at this point in the history
…on format issue when trv not available. Fixed memory leak
  • Loading branch information
softypit committed Jan 12, 2019
1 parent fd37571 commit f30e012
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 77 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,34 @@ On first use the ESP32 will start in access-point mode appearing as 'HeatingCont
- gw - gateway IP for WiFi network (leave blank for DHCP)
- netmask - netmask for WiFi network (leave blank for DHCP)

Once connected in WiFi STA mode this application first scans for EQ-3 valves and publishes their addresses to the MQTT broker.
Once connected in WiFi STA mode this application first scans for EQ-3 valves and publishes their addresses and rssi to the MQTT broker.
A scan can be initiated at any time by publishing to the `/<mqttid>radin/scan` topic.
Scan results are published to `/<mqttid>radout/devlist` in json format.

Control of valves is carried out by publishing to the <mqttid>radin/trv topic with a payload consisting of:
`ab:cd:ef:gh:ij:kl <command> [parm]`
where the device is indicated by its bluetooth address (MAC)

commands are: poll, boost, unboost, manual, auto, lock, unlock, offset, settemp.
commands are: settime, boost, unboost, manual, auto, lock, unlock, offset, settemp.

- poll makes no changes but generates a status response in order to check the valve is available and its settings.
Attention: See Issue #6, as this is currently not working correctly
- settime sets the current time on the valve.
- boost and unboost set and clear boost mode.
- lock and unlock set and clear the front-panel lock.
- manual mode prevents the valve from using its internal temperature/time program.
- offset sets the room-temperature offset (see EQ-3 instructions for what this means).
- settemp sets the required temperature for the valve to open/close at.

settime has an additional parameter of the hexadecimal encoded durrent time. parm is 12 characters hexadecimal yymmddhhMMss
e.g. 13010c0c0a00 is 2019/Jan/12 12:00.00

offset and settemp have an additional parameter of the temperature to set this can be -3.5 - +3.5 for offset and
5.0 to 29.5 in 0.5 degree increments.

In response to every successful command a status message is published to /<mqttid>radout/status containing json-encoded details of address, temperature set point, valve open percentage, mode, boost state, lock state and battery state. This can be used as an acknowledgement of a successful command to remote mqtt clients.
In response to every successful command a status message is published to /<mqttid>radout/status containing json-encoded details of address, temperature set point, valve open percentage, mode, boost state, lock state and battery state.
This can be used as an acknowledgement of a successful command to remote mqtt clients.
There is no specific command to poll the status of the valve but using any of the commands to re-set the current value will achieve the required result.
Note: It has been observed that using unboost to poll a valve can result in the valve opening as if in boost mode but without reporting boost mode active on the display or status.
It is probably not advisable to poll the valve with the unboost command.

On first boot this application uses Kolbans bootwifi code to create the wifi AP.
Once configuration is complete and on subsequent boots the configured details are used for connection. If connection fails the application reverts to AP mode where the web interface is used to reconfigure. The application can be forced into config mode by pressing and holding the BOOT key AFTER the EN key has been released.
Expand Down
41 changes: 27 additions & 14 deletions main/eq3_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static bool gap_scanning = false;
/* Device list handling */
struct found_device {
esp_bd_addr_t bda;
int rssi;
struct found_device *next;
};
struct found_device *found_devices = NULL;
Expand All @@ -77,18 +78,21 @@ static void free_found_devices(){
found_devices = NULL;
num_devices = 0;
}
int add_found_device(esp_bd_addr_t *bda){

int add_found_device(esp_bd_addr_t *bda, int rssi){
int rc = 0;
struct found_device *lastdev, *walkdevs = found_devices;
if(found_devices == NULL){
walkdevs = malloc(sizeof(struct found_device));
if(walkdevs != NULL){
walkdevs->next = NULL;
memcpy(&walkdevs->bda, bda, sizeof(esp_bd_addr_t));
walkdevs->rssi = rssi;
found_devices = walkdevs;
ESP_LOGI(EQ3_DBG_TAG, "Started list:");
//ESP_LOGI(EQ3_DBG_TAG, "Started list:");
num_devices++;
esp_log_buffer_hex(EQ3_DBG_TAG, walkdevs->bda, sizeof(esp_bd_addr_t));
//esp_log_buffer_hex(EQ3_DBG_TAG, walkdevs->bda, sizeof(esp_bd_addr_t));
//ESP_LOGI(EQ3_DBG_TAG, "RSSI %d", rssi);
}
}else{
lastdev = NULL;
Expand All @@ -103,10 +107,12 @@ int add_found_device(esp_bd_addr_t *bda){
if(walkdevs != NULL){
walkdevs->next = NULL;
memcpy(&walkdevs->bda, bda, sizeof(esp_bd_addr_t));
walkdevs->rssi = rssi;
lastdev->next = walkdevs;
ESP_LOGI(EQ3_DBG_TAG, "Added to list:");
//ESP_LOGI(EQ3_DBG_TAG, "Added to list:");
num_devices++;
esp_log_buffer_hex(EQ3_DBG_TAG, walkdevs->bda, sizeof(esp_bd_addr_t));
//esp_log_buffer_hex(EQ3_DBG_TAG, walkdevs->bda, sizeof(esp_bd_addr_t));
//ESP_LOGI(EQ3_DBG_TAG, "RSSI %d", rssi);
}
}else{
rc = 1;
Expand Down Expand Up @@ -168,8 +174,8 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *par
if ( strlen(remote_device_names[i]) == adv_name_len
&& strncmp( (char *)adv_name, remote_device_names[i], adv_name_len)
== 0){
if(add_found_device(&scan_result->scan_rst.bda) == 0){
ESP_LOGI(EQ3_DBG_TAG, "Found device %s", remote_device_names[i]);
if(add_found_device(&scan_result->scan_rst.bda, scan_result->scan_rst.rssi) == 0){
ESP_LOGI(EQ3_DBG_TAG, "Found device %s - rssi %d", remote_device_names[i], scan_result->scan_rst.rssi);
esp_log_buffer_hex(EQ3_DBG_TAG, scan_result->scan_rst.bda, 6);
}
}
Expand Down Expand Up @@ -243,24 +249,31 @@ void start_scan(){
static void scan_done(){
ESP_LOGI(EQ3_DBG_TAG, "Scan complete\nDevices found:\n");

char *report = malloc((20 * num_devices) + 2);
/* Yuck - magic numbers */
char *report = malloc((44 * num_devices) + 15);
int wridx = 12;
struct found_device *devwalk = found_devices;
sprintf(report, "{\"devices\":[");
if(devwalk != NULL){
int devnum = 0;
while(devwalk != NULL){
ESP_LOGI(EQ3_DBG_TAG, "Device:");
esp_log_buffer_hex(EQ3_DBG_TAG, devwalk->bda, 6);
ESP_LOGI(EQ3_DBG_TAG, "\n");
ESP_LOGI(EQ3_DBG_TAG, "rssi %d", devwalk->rssi);

/* {"devices":[{"rssi":-123,"bleaddr":"00:00:00:00:00:00"},....]} */

wridx += sprintf(&report[wridx], "{\"rssi\":%d,\"bleaddr\":\"%02X:%02X:%02X:%02X:%02X:%02X\"},",
devwalk->rssi, devwalk->bda[0], devwalk->bda[1], devwalk->bda[2],
devwalk->bda[3], devwalk->bda[4], devwalk->bda[5]);

sprintf(&report[devnum * 20], "{%02X:%02X:%02X:%02X:%02X:%02X},", devwalk->bda[0],
devwalk->bda[1], devwalk->bda[2], devwalk->bda[3], devwalk->bda[4],
devwalk->bda[5]);
devnum++;
devwalk = devwalk->next;
}
if(devnum > 0)
report[(devnum * 20) - 1] = 0;
send_device_list(report);
wridx--;
sprintf(&report[wridx], "]}");
send_device_list(report);
//free(report);
}else{
ESP_LOGI(EQ3_DBG_TAG, "None");
Expand Down
Loading

0 comments on commit f30e012

Please sign in to comment.