atc1441 / ATC_MiThermometer

Custom firmware for the Xiaomi Thermometer LYWSD03MMC and Telink Flasher via USB to Serial converter
2.86k stars 480 forks source link

Minimum and maximum storing #105

Open danielkucera opened 4 years ago

danielkucera commented 4 years ago

I'd like to have min/max stored. When the device starts, it will save the current value of temp and humidity as both minimum and maximum. Over the time, it will compare each value measured and adjust min/max values. For my application, it would be enough to be able to read and reset min/max using the web application. For some advanced usage, it could store hourly min/max values and display 24hour min/max values.

Or...

#define LOG_LEN = 32

int timestamp = 0;
int p = 0;
int minT[LOG_LEN];
int maxT[LOG_LEN];
int minH[LOG_LEN];
int maxH[LOG_LEN];

if (now() > timestamp + 1 hour){
  timestamp = now();
  p++;
  if (p>LOG_LEN-1){
    p = 0;
  }
  minT[p] = temp;
  maxT[p] = temp;
  minH[p] = humid;
  maxH[p] = humid;
}

if (temp > maxT[p]) {
  maxT[p] = temp;
}
if (temp < minT[p]) {
  minT[p];
}
if (humid > maxH[p]) {
  maxH[p] = humd;
}
if (humid < minH[p]) {
  minH[p]  = humid;
}

then it is easily possible to return min/max for each hour or for last N hours (where N<LOG_LEN)

What do you think?

el-gringo commented 4 years ago

On the original firmware you can. I don't know if it is possible in the custom firmware. Write 0x0010 to the handle characteristic 0x0031 You will receive notifications on handle 0x00f2 Which looks like:

00 00 00 00 10 0e 00 00 06 01 57 d8 00 35
01 00 00 00 20 1c 00 00 d8 00 3b d4 00 3a
02 00 00 00 30 2a 00 00 ed 00 3c d3 00 39
....

Each notification can be decoded this way

struct.unpack('<IIHBHB', bytes.fromhex(line))

This gives something like

[(0, 3600, 262, 87, 216, 53),
 (1, 7200, 216, 59, 212, 58),
 (2, 10800, 237, 60, 211, 57),
...
]

This is how the Xiaomi Home is getting the history.

I have the device since only 16 days and have over 380 notifications ! I don't know if there i a way to limit the amount of data but you have to wait for the last notification to have values for the last hours :(