Fix SR04 sensor driver

Fix SR04 sensor driver regression from 2022.01.2
This commit is contained in:
Theo Arends 2022-02-04 14:53:42 +01:00
parent c47fef51bc
commit 101e787bb7
3 changed files with 19 additions and 16 deletions

View File

@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file.
- Version display from 2022.01.3 to 2022.1.4
### Fixed
- SR04 sensor driver regression from 2022.01.2
## [2022.01.3]
### Added

View File

@ -149,6 +149,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo
- ESP8266SAM library from v1.0 to v1.0.1
- BME68x-Sensor-API library from v3.5.9 to v4.4.7
- From Semantic Versioning (SemVer) to Calendar Versioning (CalVer)
- SR04 driver to support US-100
- Mitsubishi HVAC temperature resolution [#13936](https://github.com/arendst/Tasmota/issues/13936)
- Remove restriction of topic must differ from mqttclient [#14019](https://github.com/arendst/Tasmota/issues/14019)
- ESP32 Set stack size with ``#define SET_ESP32_STACK_SIZE``, added ``StackLowMark`` metrics

View File

@ -67,7 +67,7 @@ uint16_t Sr04TMiddleValue(uint16_t first, uint16_t second, uint16_t third) {
}
uint16_t Sr04TMode2Distance(void) {
uint8_t buffer[4]; // Accomodate either 2 or 3 bytes of data
uint8_t buffer[4]; // Accommodate either 2 or 4 bytes of data
uint32_t buffer_idx = 0;
uint32_t end = millis() + 100;
while (millis() < end) {
@ -77,25 +77,27 @@ uint16_t Sr04TMode2Distance(void) {
}
delay(1);
}
if (0 == SR04.type) {
if (SR04_MODE_NONE == SR04.type) { // Only log during detection
AddLog(LOG_LEVEL_DEBUG, PSTR("SR4: Received '%*_H'"), buffer_idx, buffer);
}
if (buffer_idx < 2) { return 0; }
uint8_t crc = buffer[0]; // Read high byte
uint16_t distance = ((uint16_t)crc) << 8;
distance += buffer[1]; // Read low byte
if (buffer_idx > 2) { // US-100 serial has no CRC
crc += distance & 0x00ff;
crc += 0x00FF;
if (crc != buffer[3]) { // Check crc sum
AddLog(LOG_LEVEL_ERROR, PSTR("SR4: Reading CRC error"));
return 0;
uint32_t distance = 0;
if (buffer_idx > 2) { // JSN-SR04T serial has four bytes
// FF00FAF9
uint8_t crc = buffer[0];
crc += buffer[1];
crc += buffer[2];
if (crc == buffer[3]) { // Check crc sum
distance = (buffer[1] << 8) + buffer[2];
} else {
AddLog(LOG_LEVEL_ERROR, PSTR("SR4: CRC error"));
}
}
//DEBUG_SENSOR_LOG(PSTR("SR4: Distance: %d"), distance);
else if (buffer_idx > 1) { // US-100 serial has no CRC
// 00FA = 250 millimeter
distance = (buffer[0] << 8) + buffer[1];
}
return distance;
}