Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PVVX broadcaster #200

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions main/broadcasters.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,90 @@ static broadcaster_ops_t atc1441_temp_hum_ops = {
.metadata_get = atc1441_temp_hum_metadata_get,
};

/* PVVX Firmware for the Xiaomi Thermometer LYWSD03MMC Temperature and
* Humidity Sensor, see: https://github.com/pvvx/ATC_MiThermometer */

#define PVVX_TEMP_HUM_SERVICE_UUID 0x181A

typedef struct {
// uint16_t not_used;
nerdb0y marked this conversation as resolved.
Show resolved Hide resolved
uint16_t service_uuid;
mac_addr_t mac;
int16_t temp;
uint16_t humid;
uint16_t battery_mv;
uint8_t battery_percent;
uint8_t message_counter;
uint8_t flags;
} __attribute__((packed)) pvvx_temp_hum_t;

static pvvx_temp_hum_t *pvvx_temp_hum_data_get(uint8_t *adv_data,
uint8_t adv_data_len, uint8_t *pvvx_temp_hum_len)
{
uint8_t len;
uint8_t *data = esp_ble_resolve_adv_data(adv_data,
ESP_BLE_AD_TYPE_SERVICE_DATA, &len);

if (pvvx_temp_hum_len)
*pvvx_temp_hum_len = len;

return (pvvx_temp_hum_t *)data;
}
static int pvvx_temp_hum_is_broadcaster(uint8_t *adv_data,
size_t adv_data_len)
{
uint8_t len;
pvvx_temp_hum_t *pvvx_data = pvvx_temp_hum_data_get(adv_data,
adv_data_len, &len);

if (len < sizeof(pvvx_temp_hum_t) ||
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< or !=?
Are you expecting variable length messages?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not expecting variable length, but was following convention of ATC and other broadcasters. Happy to change if you'd prefer.

le16toh(pvvx_data->service_uuid) != PVVX_TEMP_HUM_SERVICE_UUID)
{
return 0;
}

return 1;
}

static void pvvx_temp_hum_metadata_get(uint8_t *adv_data,
size_t adv_data_len, int rssi, broadcaster_meta_data_cb_t cb, void *ctx)
{
char s[32];
uint8_t len;
pvvx_temp_hum_t *pvvx_data = pvvx_temp_hum_data_get(adv_data,
adv_data_len, &len);

cb("MACAddress", _mactoa(pvvx_data->mac), ctx);

sprintf(s, "%hhu", pvvx_data->message_counter);
cb("MessageCounter", s, ctx);

sprintf(s, "%.2f", pvvx_data->temp / 100.0);
cb("Temperature", s, ctx);

sprintf(s, "%.2f", pvvx_data->humid / 100.0);
cb("Humidity", s, ctx);

sprintf(s, "%u", pvvx_data->battery_percent);
cb("BatteryLevel", s, ctx);

sprintf(s, "%.3f", pvvx_data->battery_mv / 1000.0 );
cb("BatteryVolts", s, ctx);
}

static broadcaster_ops_t pvvx_temp_hum_ops = {
.name = "PVVX",
.is_broadcaster = pvvx_temp_hum_is_broadcaster,
.metadata_get = pvvx_temp_hum_metadata_get,
};

/* Common */
static broadcaster_ops_t *broadcaster_ops[] = {
&ibeacon_ops,
&eddystone_ops,
&mijia_sensor_ops,
&beewi_smart_door_ops,
&pvvx_temp_hum_ops,
&atc1441_temp_hum_ops,
NULL
};
Expand Down