Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Jan 15, 2021
1 parent bb5c031 commit c10085b
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 86 deletions.
Binary file added MHO_C401_v12.bin
Binary file not shown.
Binary file removed MHO_C401_v12_beta.bin
Binary file not shown.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ Initial forked from https://github.com/atc1441/ATC_MiThermometer

(Thanks @atc1441 for initial parsing and js code for OTA)

[Custom Firmware Version 1.1](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_Thermometer11.bin)
[LYWSD03MMC Custom Firmware Version 1.1](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_Thermometer11.bin)

[Custom Firmware Version 1.2](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_Thermometer12.bin) added "pin-code" of connection
[LYWSD03MMC Custom Firmware Version 1.2](https://github.com/pvvx/ATC_MiThermometer/raw/master/ATC_Thermometer12.bin) added "pin-code" of connection

[MHO-C401 Version 1.2-beta](https://github.com/pvvx/ATC_MiThermometer/raw/master/MHO_C401_v12_beta.bin)
[MHO-C401 Custom Firmware Version 1.2](https://github.com/pvvx/ATC_MiThermometer/raw/master/MHO_C401_v12.bin)

[Original Firmware v1.0.0_0106](https://github.com/pvvx/ATC_MiThermometer/raw/master/Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0106.bin)
[LYWSD03MMC Original Firmware v1.0.0_0106](https://github.com/pvvx/ATC_MiThermometer/raw/master/Original_OTA_Xiaomi_LYWSD03MMC_v1.0.0_0106.bin)

### Average consumption:
>* #### Default settings (Advertising interval of 2.5 seconds, the measurement interval 10 sec):
>* #### LYWSD03MMC - Default settings (Advertising interval of 2.5 seconds, the measurement interval 10 sec):
> * Advertisement: 15.5 uA
> * Connection: 15..17 uA
>* #### MHO-C401 - Default settings (Advertising interval of 2.5 seconds, the measurement interval 20 sec):
> * Advertisement: 12..30 uA (depends on the amount of temperature or humidity changes over time to display)
> * Connection: 15..30 uA (depends on the amount of temperature or humidity changes over time to display)
**Advertising in 4 formats:**

Expand Down Expand Up @@ -82,8 +85,6 @@ Setting the pin to "1" or "0" works if both hysteresis are set to zero (TRG off)

![SCH](https://github.com/pvvx/ATC_MiThermometer/blob/master/img/USBCOMFlashTxHtml.gif)

[MHO-C401](https://pvvx.github.io/MHO_C401/)

#### Building firmware:
1. Go to [wiki.telink-semi.cn](http://wiki.telink-semi.cn/wiki/IDE-and-Tools/IDE-for-TLSR8-Chips/) and getting IDE for TLSR8 Chips.
2. Clone https://github.com/Ai-Thinker-Open/Telink_825X_SDK
Expand All @@ -92,9 +93,11 @@ Setting the pin to "1" or "0" works if both hysteresis are set to zero (TRG off)

[Telink Linux Tool Chain 2020-06-26](https://yadi.sk/d/pt_qTBB-t24i9A)

[BoardPinout](https://github.com/pvvx/ATC_MiThermometer/blob/master/BoardPinout)
[LYWSD03MMC BoardPinout](https://github.com/pvvx/ATC_MiThermometer/blob/master/BoardPinout)

[LYWSD03MMC Original Full Flash v1.0.0_0106 (512 KB)](https://github.com/pvvx/ATC_MiThermometer/raw/master/Original_full_flash_Xiaomi_LYWSD03MMC.bin)

[Original Full Flash v1.0.0_0106 (512 KB)](https://github.com/pvvx/ATC_MiThermometer/raw/master/Original_full_flash_Xiaomi_LYWSD03MMC.bin)
[MHO-C401 Info](https://pvvx.github.io/MHO_C401/)

[Build Telink EVK on E104-BT10-G/N module (TLSR8269)](https://github.com/pvvx/TLSR8269-EVK)

Expand All @@ -106,4 +109,7 @@ Setting the pin to "1" or "0" works if both hysteresis are set to zero (TRG off)

[HTML files](https://github.com/pvvx/pvvx.github.io/tree/master/ATC_MiThermometer)

[Forum link (russian)](https://esp8266.ru/forum/threads/tlsr8251-lcd-termometr-lywsd03mmc-xiaomi-bluetooth-termometr.5263/)
[LYWSD03MMC Forum link (russian)](https://esp8266.ru/forum/threads/tlsr8251-lcd-termometr-lywsd03mmc-xiaomi-bluetooth-termometr.5263/)

[MHO-C401 Forum link (russian)](https://esp8266.ru/forum/threads/tlsr8251f512et24-e-inc-display-termometr-mho-c401-bluetooth-termometr.5446/)

19 changes: 10 additions & 9 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#if USE_TRIGGER_OUT
#include "trigger.h"
#endif

void app_enter_ota_mode(void);

RAM uint32_t vtime_count_us; // count validity time, in us
Expand All @@ -34,9 +35,9 @@ RAM volatile uint8_t end_measure;
RAM uint32_t tim_last_chow; // timer show lcd >= 1.5 sec
RAM uint32_t tim_measure; // timer measurements >= 10 sec

RAM uint32_t adv_interval; // adv interval in 0.625 ms // cfg.advertising_interval * 100
RAM uint32_t adv_interval; // adv interval in 0.625 ms // = cfg.advertising_interval * 100
RAM uint32_t connection_timeout; // connection timeout in 10 ms, Tdefault = connection_latency_ms * 4 = 2000 * 4 = 8000 ms
RAM uint32_t measurement_step_time; // adv_interval * measure_interval
RAM uint32_t measurement_step_time; // = adv_interval * measure_interval
RAM uint32_t min_step_time_update_lcd; // = cfg.min_step_time_update_lcd * 0.05 sec

void lcd(void);
Expand All @@ -56,7 +57,7 @@ const cfg_t def_cfg = {
#if DEVICE_TYPE == DEVICE_MHO_C401
.measure_interval = 8, // * advertising_interval = 20 sec
.min_step_time_update_lcd = 199, //x0.05 sec, 9.95 sec
#else
#else // DEVICE_LYWSD03MMC
.measure_interval = 4, // * advertising_interval = 10 sec
.min_step_time_update_lcd = 49, //x0.05 sec, 2.45 sec
#endif
Expand All @@ -71,12 +72,12 @@ static const external_data_t def_ext = {
#if DEVICE_TYPE == DEVICE_MHO_C401
.flg.smiley = 7, // 7 = "ooo"
.flg.percent_on = true,
#else
#else // DEVICE_LYWSD03MMC
.flg.smiley = 7, // 7 = "(ooo)"
.flg.percent_on = true,
#endif
.flg.battery = false,
.flg.temp_symbol = 5 // 5 = "°C", 3 = "°F", ... app.h
.flg.temp_symbol = 5 // 5 = "°C", ... app.h
};
RAM external_data_t ext;
RAM uint32_t pincode;
Expand Down Expand Up @@ -116,7 +117,7 @@ void test_config(void) {
else if(connection_timeout < 100)
connection_timeout = 100; //x10 ms, 1 sec
if(!cfg.connect_latency) {
my_periConnParameters.intervalMin = (cfg.advertising_interval * 625 / 30) - 1; // Tmin = 20*1.25 = 25 ms, Tмах = 3333*1.25 = 4166.25 ms
my_periConnParameters.intervalMin = (cfg.advertising_interval * 625 / 30) - 1; // Tmin = 20*1.25 = 25 ms, Tmax = 3333*1.25 = 4166.25 ms
my_periConnParameters.intervalMax = my_periConnParameters.intervalMin + 2;
my_periConnParameters.latency = 0;
} else {
Expand All @@ -129,8 +130,9 @@ void test_config(void) {
cfg.min_step_time_update_lcd = 10; // min 10*0.05 = 0.5 sec
min_step_time_update_lcd = cfg.min_step_time_update_lcd * (100 * CLOCK_16M_SYS_TIMER_CLK_1MS);

my_RxTx_Data[0] = 0x55;
my_RxTx_Data[0] = CMD_ID_CFG;
my_RxTx_Data[1] = VERSION;
my_RxTx_Data[sizeof(cfg)+2] = DEVICE_TYPE;
memcpy(&my_RxTx_Data[2], &cfg, sizeof(cfg));
}

Expand Down Expand Up @@ -211,7 +213,7 @@ void user_init_normal(void) {//this will get executed one time after power up
show_battery_symbol(1);
update_lcd();
#if DEVICE_TYPE == DEVICE_MHO_C401
while(!task_lcd()) pm_wait_ms(10);
while(task_lcd()) pm_wait_ms(10);
#endif
cpu_sleep_wakeup(DEEPSLEEP_MODE, PM_WAKEUP_TIMER,
clock_time() + 120 * CLOCK_16M_SYS_TIMER_CLK_1S); // go deep-sleep 2 minutes
Expand Down Expand Up @@ -415,7 +417,6 @@ _attribute_ram_code_ void main_loop(void) {
if(wrk_measure == 0 && stage_lcd) {
if(gpio_read(EPD_BUSY) && (!task_lcd())) {
cpu_set_gpio_wakeup(EPD_BUSY, Level_High, 0); // pad high wakeup deepsleep disable
//bls_pm_setWakeupSource(0); // gpio pad wakeup suspend/deepsleep
}
else if(stage_lcd && ((bls_pm_getSystemWakeupTick() - clock_time())) > 25 * CLOCK_16M_SYS_TIMER_CLK_1MS) {
cpu_set_gpio_wakeup(EPD_BUSY, Level_High, 1); // pad high wakeup deepsleep enable
Expand Down
18 changes: 15 additions & 3 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@ typedef struct __attribute__((packed)) _cfg_t {
uint8_t tx_measures : 1; // Send all measurements in connected mode
uint8_t lp_measures : 1; // Sensor measurements in "Low Power" mode
} flg;
/* 0 = " " off,
/* ==================
* LYWSD03MMC:
* 0 = " " off,
* 1 = " ^_^ "
* 2 = " -^- "
* 3 = " ooo "
* 4 = "( )"
* 5 = "(^_^)" happy
* 6 = "(-^-)" sad
* 7 = "(ooo)" */
* 7 = "(ooo)"
* -------------------
* MHO-C401:
* 0 = " " off,
* 1 = " o "
* 2 = "o^o"
* 3 = "o-o"
* 4 = "oVo"
* 5 = "vVv" happy
* 6 = "^-^" sad
* 7 = "oOo" */
uint8_t smiley; // 0..7
int8_t temp_offset; // Set temp offset, -12,5 - +12,5 °C (-125..125)
int8_t humi_offset; // Set humi offset, -12,5 - +12,5 % (-125..125)
Expand All @@ -41,7 +53,7 @@ typedef struct __attribute__((packed)) _cfg_t {
}cfg_t;
extern cfg_t cfg;
extern const cfg_t def_cfg;
//
/* Warning: MHO-C401 Symbols: "%", "°Г", "( )", "." have one control bit! */
typedef struct __attribute__((packed)) _external_data_t {
int16_t big_number; // -995..19995, x0.1
int16_t small_number; // -9..99, x1
Expand Down
6 changes: 3 additions & 3 deletions src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ extern "C" {
#define EEP_SUP_VER 0x09 // EEP data minimum supported version

#define DEVICE_LYWSD03MMC 0 // LCD display LYWSD03MMC
#define DEVICE_MHO_C401 1 // E-Ink display MHO-C401 (beta version)
#define DEVICE_MHO_C401 1 // E-Ink display MHO-C401

#define DEVICE_TYPE DEVICE_LYWSD03MMC
#define DEVICE_TYPE DEVICE_LYWSD03MMC // DEVICE_LYWSD03MMC or DEVICE_MHO_C401

#define BLE_SECURITY_ENABLE 1
#define BLE_HOST_SMP_ENABLE BLE_SECURITY_ENABLE
Expand Down Expand Up @@ -80,7 +80,7 @@ extern "C" {
#endif // USE_TRIGGER_OUT
#endif // DEVICE_TYPE == DEVICE_LYWSD03MMC

/* Average consumption (Show battery on, Comfort on, advertising 2 sec, measure 10 sec):
/* DEVICE_LYWSD03MMC Average consumption (Show battery on, Comfort on, advertising 2 sec, measure 10 sec):
* 16 MHz - 17.43 uA
* 24 MHz - 17.28 uA
* 32 MHz - 17.36 uA
Expand Down
2 changes: 1 addition & 1 deletion src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ _attribute_ram_code_ void adc_bat_init(void)
adc_power_on_sar_adc(1);
}

_attribute_ram_code_ uint16_t get_battery_mv()
_attribute_ram_code_ uint16_t get_battery_mv(void)
{
uint16_t temp;
int i,j;
Expand Down
10 changes: 4 additions & 6 deletions src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ uint8_t ota_is_working = 0;
_attribute_ram_code_ void app_enter_ota_mode(void) {
ota_is_working = 1;
bls_ota_setTimeout(45 * 1000000); // set OTA timeout 45 seconds
//show_smiley(1);
}

void ble_disconnect_callback(uint8_t e, uint8_t *p, int n) {
ble_connected = 0;
//lcd_flg.b.notify_on = 0;
lcd_flg.uc = 0;
//show_ble_symbol(0);
if(!cfg.flg.tx_measures)
tx_measures = 0;
}
Expand All @@ -53,7 +51,6 @@ void ble_connect_callback(uint8_t e, uint8_t *p, int n) {
bls_l2cap_requestConnParamUpdate(16, 16, cfg.connect_latency, connection_timeout); // (16*1.25 ms, 16*1.25 ms, (16*1.25)*100 ms, 800*10 ms)
else
bls_l2cap_requestConnParamUpdate(my_periConnParameters.intervalMin, my_periConnParameters.intervalMax, cfg.connect_latency, connection_timeout); // (16*1.25 ms, 16*1.25 ms, (16*1.25)*100 ms, 800*10 ms)
//show_ble_symbol(1);
// bls_l2cap_setMinimalUpdateReqSendingTime_after_connCreate(1000);
}

Expand All @@ -79,6 +76,7 @@ _attribute_ram_code_ int RxTxWrite(void * p) {
}

_attribute_ram_code_ void user_set_rf_power(u8 e, u8 *p, int n) {
(void) e; (void) p; (void) n;
rf_set_power_level_index(cfg.rf_tx_power);
}
/*
Expand All @@ -94,11 +92,12 @@ _attribute_ram_code_ void ev_adv_timeout(u8 e, u8 *p, int n) {

#if BLE_SECURITY_ENABLE
int app_host_event_callback(u32 h, u8 *para, int n) {
(void) para; (void) n;
uint8_t event = (uint8_t)h;
if (event == GAP_EVT_SMP_TK_DISPALY) {
//u32 pinCode = *(u32*) para;
uint32_t * p = (uint32_t *)&smp_param_own.paring_tk[0];
memset(p, 0, 16);
memset(p, 0, sizeof(smp_param_own.paring_tk));
p[0] = pincode;
}
// else if (event == GAP_EVT_SMP_TK_REQUEST_PASSKEY)
Expand Down Expand Up @@ -161,7 +160,6 @@ void init_ble(void) {
blc_smp_configSecurityRequestSending(SecReq_IMM_SEND, SecReq_PEND_SEND, 1000); //if not set, default is: send "security request" immediately after link layer connection established(regardless of new connection or reconnection )
blc_gap_registerHostEventHandler(app_host_event_callback);
blc_gap_setEventMask(GAP_EVT_MASK_SMP_TK_DISPALY); // | GAP_EVT_MASK_SMP_CONN_ENCRYPTION_DONE | GAP_EVT_MASK_SMP_TK_REQUEST_PASSKEY);

} else
#endif
blc_smp_setSecurityLevel(No_Security);
Expand Down Expand Up @@ -209,7 +207,7 @@ void init_ble(void) {
ev_adv_timeout(0,0,0);
}

// adv_type: 0 - Custom, 1 - Mi, 2 - atc1441
/* adv_type: 0 - atc1441, 1 - Custom, 2,3 - Mi */
_attribute_ram_code_ void set_adv_data(uint8_t adv_type) {
if(adv_type == 3)
adv_type = adv_mi_count & 3;
Expand Down
2 changes: 1 addition & 1 deletion src/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ inline void ble_send_battery(void) {
}

inline void ble_send_cfg(void) {
bls_att_pushNotifyData(RxTx_CMD_OUT_DP_H, my_RxTx_Data, sizeof(cfg) + 2);
bls_att_pushNotifyData(RxTx_CMD_OUT_DP_H, my_RxTx_Data, sizeof(cfg) + 3);
}
4 changes: 3 additions & 1 deletion src/cmd_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct __attribute__((packed)) _blk_mi_keys_t {
uint8_t data[MI_KEYTBIND_SIZE]; // token + bindkey
} blk_mi_keys_t, * pblk_mi_keys_t;
RAM blk_mi_keys_t keybuf;

#if DEVICE_TYPE == DEVICE_MHO_C401
uint8_t * find_mi_keys(uint16_t chk_id, uint8_t cnt) {
uint8_t * p = (uint8_t *)(FLASH_MIKEYS_ADDR);
Expand All @@ -59,7 +60,7 @@ uint8_t * find_mi_keys(uint16_t chk_id, uint8_t cnt) {
} while(id != 0xffff || len != 0xff || p < pend);
return NULL;
}
#else
#else // DEVICE_LYWSD03MMC
uint8_t * find_mi_keys(uint16_t chk_id, uint8_t cnt) {
uint8_t * p = (uint8_t *)(FLASH_MIKEYS_ADDR);
uint8_t * pend = p + FLASH_SECTOR_SIZE;
Expand All @@ -83,6 +84,7 @@ uint8_t * find_mi_keys(uint16_t chk_id, uint8_t cnt) {
return NULL;
}
#endif

uint8_t send_mi_key(void) {
if (blc_ll_getTxFifoNumber() < 9) {
while(keybuf.klen > TX_MAX_SIZE - 2) {
Expand Down
34 changes: 17 additions & 17 deletions src/cmd_parser.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#pragma once
enum {
CMD_MI_ID_MAC = 0x10, // MAC+RandMAC
CMD_MI_ID_MAC = 0x10, // Get MAC+RandMAC
CMD_MI_ID_DNAME = 0x11, // Mi key: DevNameId
CMD_MI_ID_TBIND = 0x12, // Mi keys: Token & Bind
CMD_MI_ID_CFG = 0x13, // Mi cfg data
CMD_MI_ID_KDEL = 0x14, // Mi marked as deleted keys
CMD_MI_ID_KALL = 0x15, // get all mi keys
CMD_MI_ID_REST = 0x16, // restore prev mi token & bindkeys
CMD_ID_EXTDATA = 0x22, // Get/set show ext. data
CMD_ID_MEASURE = 0x33, // Start/stop measures in connection mode
CMD_ID_TRG = 0x44, // Get/set trg data
CMD_ID_TRG_OUT = 0x45, // Set trg out
CMD_ID_TRG_NS = 0x4A, // Get/set trg data (not save to Flash)
CMD_ID_CFG = 0x55, // Get/set config
CMD_ID_CFG_DEF = 0x56, // Get default config
CMD_ID_CFG_NS = 0x5A, // Get/set config (not save to Flash)
CMD_ID_LCD_DUMP = 0x60, // Get/set lcd buf
CMD_ID_LCD_FLG = 0x61, // Start/stop notify lcd dump and ...
CMD_ID_PINCODE = 0x70, // Set new pinCode 0..999999
CMD_ID_DEBUG = 0xDE // test/debug
CMD_MI_ID_CFG = 0x13, // Mi cfg data
CMD_MI_ID_KDEL = 0x14, // Mi marked as deleted keys
CMD_MI_ID_KALL = 0x15, // Get all mi keys
CMD_MI_ID_REST = 0x16, // Restore prev mi token & bindkeys
CMD_ID_EXTDATA = 0x22, // Get/set show ext. data
CMD_ID_MEASURE = 0x33, // Start/stop measures in connection mode
CMD_ID_TRG = 0x44, // Get/set trg data
CMD_ID_TRG_OUT = 0x45, // Set trg out
CMD_ID_TRG_NS = 0x4A, // Get/set trg data (not save to Flash)
CMD_ID_CFG = 0x55, // Get/set config
CMD_ID_CFG_DEF = 0x56, // Get default config
CMD_ID_CFG_NS = 0x5A, // Get/set config (not save to Flash)
CMD_ID_LCD_DUMP = 0x60, // Get/set lcd buf
CMD_ID_LCD_FLG = 0x61, // Start/stop notify lcd dump and ...
CMD_ID_PINCODE = 0x70, // Set new pinCode 0..999999
CMD_ID_DEBUG = 0xDE // Test/Debug
} CMD_MI_ID_KEYS;

uint8_t mi_key_stage;
Expand Down
19 changes: 9 additions & 10 deletions src/epd.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "tl_common.h"
#include "app_config.h"
#if DEVICE_TYPE == DEVICE_MHO_C401
/* Based on source: https://github.com/znanev/ATC_MiThermometer */
#include "epd.h"
#include "drivers/8258/pm.h"
#include "drivers/8258/timer.h"
Expand Down Expand Up @@ -103,13 +104,13 @@ _attribute_ram_code_ void show_temp_symbol(uint8_t symbol) {
display_buff[14] &= ~BIT(2); // "_"
}
/* 0 = " " off,
* 1 = " @ "
* 2 = " O "
* 1 = " o "
* 2 = "o^o"
* 3 = "o-o"
* 4 = "ovo"
* 4 = "oVo"
* 5 = "vVv" happy
* 6 = "`^-^" sad
* 7 = "ooo" */
* 6 = "^-^" sad
* 7 = "oOo" */
_attribute_ram_code_ void show_smiley(uint8_t state){
// off
display_buff[3] = 0;
Expand All @@ -118,12 +119,11 @@ _attribute_ram_code_ void show_smiley(uint8_t state){
switch(state & 7) {
case 1:
display_buff[3] |= BIT(2);
display_buff[4] |= BIT(4) | BIT(7);
display_buff[5] |= BIT(6);
display_buff[4] |= BIT(4);
break;
case 2:
display_buff[3] |= BIT(2);
display_buff[4] |= BIT(4);
display_buff[4] |= BIT(1) | BIT(4);
display_buff[5] |= BIT(6);
break;
case 3:
display_buff[4] |= BIT(1) | BIT(7);
Expand All @@ -139,7 +139,6 @@ _attribute_ram_code_ void show_smiley(uint8_t state){
display_buff[4] |= BIT(1);
break;
case 6:
display_buff[3] |= BIT(5);
display_buff[4] |= BIT(7);
display_buff[5] |= BIT(6);
break;
Expand Down
Loading

0 comments on commit c10085b

Please sign in to comment.