Skip to content

Commit

Permalink
Merge pull request fsaris#32 from fsaris/dev
Browse files Browse the repository at this point in the history
Switch to esp-idf
  • Loading branch information
fsaris authored Sep 28, 2023
2 parents c3b023d + ab6d97e commit 9d3cedc
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ In case of using a WLAN with hidden SSID, mind to use the multiple netowrk optio

### Requirements
- ESP32 module
- ESPHome 2023.2.0 or newer
- ESPHome 2023.8.1 or newer
- MQTT broker

### Recomendations
Expand Down
14 changes: 9 additions & 5 deletions awox-ble-mesh-hub.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ esphome:
friendly_name: "AwoX BLE mesh hub"

esp32:
board: esp32dev
# Match this with the board your using
board: nodemcu-32s
framework:
type: arduino
# Set to fixed version due to build issues https://community.home-assistant.io/t/esphome-could-not-find-package-json/499026/8
version: 2.0.6 #https://github.com/espressif/arduino-esp32/releases
# Be sure to "Clean Build Files" when switching framework
type: esp-idf

status_led:
# Match this with the board your using
pin: GPIO2

# Enable logging
Expand Down Expand Up @@ -47,7 +48,10 @@ wifi:
password: !secret wifi_password

external_components:
source: github://fsaris/EspHome-AwoX-BLE-mesh-hub
- source:
type: git
url: https://github.com/fsaris/EspHome-AwoX-BLE-mesh-hub
# ref: dev
# - source:
# type: local
# path: components
Expand Down
3 changes: 0 additions & 3 deletions components/awox_mesh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,3 @@ async def to_code(config):
cg.add(var.register_connection(connection_var))
cg.add(var.set_address_prefix(config["address_prefix"]))
await esp32_ble_tracker.register_client(connection_var, config["connection"])

# Crypto
cg.add_library("rweather/Crypto", "0.4.0")
5 changes: 2 additions & 3 deletions components/awox_mesh/awox_mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma once

#ifdef USE_ESP32
#include <cstring>
#include <cstdio>
Expand All @@ -24,6 +22,7 @@ FoundDevice AwoxMesh::add_to_devices(const esp32_ble_tracker::ESPBTDevice &devic
found.address = device.address_uint64();
found.rssi = device.get_rssi();
found.last_detected = esphome::millis();
found.device = device;
this->devices_.push_back(found);

this->remove_devices_that_are_not_available();
Expand Down Expand Up @@ -62,7 +61,7 @@ void AwoxMesh::loop() {

ESP_LOGI(TAG, "Try to connect %s => rssi: %d", device.address_str.c_str(), device.rssi);
this->connection->set_address(device.address);
this->connection->connect();
this->connection->parse_device(device.device);

this->set_timeout("connecting", 20000, [this, device]() {
if (this->connection->connected()) {
Expand Down
5 changes: 1 addition & 4 deletions components/awox_mesh/awox_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
#include <map>
#include <vector>

#include <AES.h>
#include <Crypto.h>
#include <GCM.h>

#include "esphome/core/hal.h"
#include "esphome/components/esp32_ble_client/ble_client_base.h"
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
Expand All @@ -28,6 +24,7 @@ struct FoundDevice {
uint64_t address{0};
int rssi{0};
uint32_t last_detected;
esp32_ble_tracker::ESPBTDevice device;
};

class AwoxMesh : public esp32_ble_tracker::ESPBTDeviceListener, public Component {
Expand Down
85 changes: 42 additions & 43 deletions components/awox_mesh/mesh_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include <math.h>
#include "mesh_device.h"
#include "device_info.h"
#include <AES.h>
#include <Crypto.h>
#include <GCM.h>
#include "aes/esp_aes.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
Expand All @@ -33,12 +31,12 @@ static std::string encrypt(std::string key, std::string data) {
std::reverse(data.begin(), data.end());

unsigned char buffer[16];
auto aes128 = AES128();

if (!aes128.setKey((uint8_t *) key.c_str(), key.size())) {
ESP_LOGE(TAG, "Failed to set key");
};
aes128.encryptBlock(buffer, (uint8_t *) data.c_str());
esp_aes_context aes;
esp_aes_init(&aes);
esp_aes_setkey(&aes, (const unsigned char *) key.c_str(), key.size() * 8);
esp_aes_crypt_ecb(&aes, 1, (const unsigned char *) data.c_str(), buffer);
esp_aes_free(&aes);

std::string result = std::string((char *) buffer, 16);

Expand All @@ -49,7 +47,7 @@ static std::string encrypt(std::string key, std::string data) {
}

static std::string int_as_hex_string(unsigned char hex1, unsigned char hex2, unsigned char hex3) {
char value[6];
char value[7];
sprintf(value, "%02X%02X%02X", hex1, hex2, hex3);
return std::string((char *) value, 6);
}
Expand All @@ -64,13 +62,13 @@ static unsigned char turn_off_bit(unsigned char value, int bit) {
static int get_product_code(unsigned char part1, unsigned char part2) { return int(part2); }

static std::string get_product_code_as_hex_string(int product_id) {
char value[15];
sprintf(value, "Product: 0x%02X", product_id);
return std::string((char *) value, 15);
}
char value[15];
sprintf(value, "Product: 0x%02X", product_id);
return std::string((char *) value, 15);
}

static std::string get_device_mac(unsigned char part3, unsigned char part4, unsigned char part5, unsigned char part6) {
char value[17];
char value[18];
sprintf(value, "A4:C1:%02X:%02X:%02X:%02X", part3, part4, part5, part6);
return std::string((char *) value, 17);
}
Expand Down Expand Up @@ -206,6 +204,9 @@ bool MeshDevice::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t g
}
break;
}

default:
break;
}

return true;
Expand Down Expand Up @@ -328,6 +329,8 @@ void MeshDevice::handle_packet(std::string &packet) {
bool online, state, color_mode, transition_mode;
unsigned char white_brightness, temperature, color_brightness, R, G, B;

mesh_id = 0;

if (static_cast<unsigned char>(packet[7]) == COMMAND_ONLINE_STATUS_REPORT) { // DC
mesh_id = (static_cast<unsigned char>(packet[19]) * 256) + static_cast<unsigned char>(packet[10]);
mode = static_cast<unsigned char>(packet[12]);
Expand Down Expand Up @@ -505,11 +508,8 @@ void MeshDevice::publish_state(Device *device) {
},
0, true);
} else {
global_mqtt_client->publish(
this->get_mqtt_topic_for_(device, "state"),
device->state ? "ON" : "OFF",
device->state ? 2 : 3
);
global_mqtt_client->publish(this->get_mqtt_topic_for_(device, "state"), device->state ? "ON" : "OFF",
device->state ? 2 : 3);
}
}

Expand All @@ -532,7 +532,7 @@ void MeshDevice::send_discovery(Device *device) {
root[MQTT_NAME] = nullptr;
root[MQTT_UNIQUE_ID] = "awox-" + device->mac + "-" + device->device_info->get_component_type();

if (device->device_info->get_icon() != "") {
if (strlen(device->device_info->get_icon()) > 0) {
root[MQTT_ICON] = device->device_info->get_icon();
}

Expand Down Expand Up @@ -591,7 +591,7 @@ void MeshDevice::send_discovery(Device *device) {

device_info[MQTT_DEVICE_NAME] = device->device_info->get_name();

if (device->device_info->get_model() == "") {
if (strlen(device->device_info->get_model()) == 0) {
device_info[MQTT_DEVICE_MODEL] = get_product_code_as_hex_string(device->device_info->get_product_id());
} else {
device_info[MQTT_DEVICE_MODEL] = device->device_info->get_model();
Expand All @@ -606,28 +606,27 @@ void MeshDevice::send_discovery(Device *device) {
this->get_mqtt_topic_for_(device, "command"),
[this, device](const std::string &topic, JsonObject root) { this->process_incomming_command(device, root); });
} else {
global_mqtt_client->subscribe(
this->get_mqtt_topic_for_(device, "command"),
[this, device](const std::string &topic, const std::string &payload) {
ESP_LOGI(TAG, "command %s - %s", topic.c_str(), payload.c_str());
auto val = parse_on_off(payload.c_str());
switch (val) {
case PARSE_ON:
device->state = true;
this->set_state(device->mesh_id, true);
break;
case PARSE_OFF:
device->state = false;
this->set_state(device->mesh_id, false);
break;
case PARSE_TOGGLE:
device->state = !device->state;
this->set_state(device->mesh_id, device->state);
break;
case PARSE_NONE:
break;
}
});
global_mqtt_client->subscribe(this->get_mqtt_topic_for_(device, "command"),
[this, device](const std::string &topic, const std::string &payload) {
ESP_LOGI(TAG, "command %s - %s", topic.c_str(), payload.c_str());
auto val = parse_on_off(payload.c_str());
switch (val) {
case PARSE_ON:
device->state = true;
this->set_state(device->mesh_id, true);
break;
case PARSE_OFF:
device->state = false;
this->set_state(device->mesh_id, false);
break;
case PARSE_TOGGLE:
device->state = !device->state;
this->set_state(device->mesh_id, device->state);
break;
case PARSE_NONE:
break;
}
});
}
this->publish_availability(device, true);
}
Expand Down
6 changes: 4 additions & 2 deletions components/awox_mesh/mesh_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ class MeshDevice : public esp32_ble_client::BLEClientBase {
this->mesh_password = mesh_password;
}

void register_device(int device_type, int product_id, const char *name, const char *model, const char *manufacturer, const char *icon) {
void register_device(int device_type, int product_id, const char *name, const char *model, const char *manufacturer,
const char *icon) {
this->device_info_resolver->register_device(device_type, product_id, name, model, manufacturer, icon);
}

Expand All @@ -211,8 +212,9 @@ class MeshDevice : public esp32_ble_client::BLEClientBase {
esp_ble_gattc_cb_param_t *param) override;

void set_address(uint64_t address) {

this->publish_connected(false);
this->set_state(esp32_ble_tracker::ClientState::IDLE);

BLEClientBase::set_address(address);

if (address == 0) {
Expand Down

0 comments on commit 9d3cedc

Please sign in to comment.