mirror of https://github.com/arendst/Tasmota.git
Fix SR04 sensor driver
Fix SR04 sensor driver regression from 2022.01.2
This commit is contained in:
parent
c47fef51bc
commit
101e787bb7
|
@ -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
|
- Version display from 2022.01.3 to 2022.1.4
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
- SR04 sensor driver regression from 2022.01.2
|
||||||
|
|
||||||
## [2022.01.3]
|
## [2022.01.3]
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -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
|
- ESP8266SAM library from v1.0 to v1.0.1
|
||||||
- BME68x-Sensor-API library from v3.5.9 to v4.4.7
|
- BME68x-Sensor-API library from v3.5.9 to v4.4.7
|
||||||
- From Semantic Versioning (SemVer) to Calendar Versioning (CalVer)
|
- 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)
|
- 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)
|
- 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
|
- ESP32 Set stack size with ``#define SET_ESP32_STACK_SIZE``, added ``StackLowMark`` metrics
|
||||||
|
|
|
@ -67,7 +67,7 @@ uint16_t Sr04TMiddleValue(uint16_t first, uint16_t second, uint16_t third) {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Sr04TMode2Distance(void) {
|
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 buffer_idx = 0;
|
||||||
uint32_t end = millis() + 100;
|
uint32_t end = millis() + 100;
|
||||||
while (millis() < end) {
|
while (millis() < end) {
|
||||||
|
@ -77,25 +77,27 @@ uint16_t Sr04TMode2Distance(void) {
|
||||||
}
|
}
|
||||||
delay(1);
|
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);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SR4: Received '%*_H'"), buffer_idx, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buffer_idx < 2) { return 0; }
|
uint32_t distance = 0;
|
||||||
|
if (buffer_idx > 2) { // JSN-SR04T serial has four bytes
|
||||||
uint8_t crc = buffer[0]; // Read high byte
|
// FF00FAF9
|
||||||
uint16_t distance = ((uint16_t)crc) << 8;
|
uint8_t crc = buffer[0];
|
||||||
distance += buffer[1]; // Read low byte
|
crc += buffer[1];
|
||||||
|
crc += buffer[2];
|
||||||
if (buffer_idx > 2) { // US-100 serial has no CRC
|
if (crc == buffer[3]) { // Check crc sum
|
||||||
crc += distance & 0x00ff;
|
distance = (buffer[1] << 8) + buffer[2];
|
||||||
crc += 0x00FF;
|
} else {
|
||||||
if (crc != buffer[3]) { // Check crc sum
|
AddLog(LOG_LEVEL_ERROR, PSTR("SR4: CRC error"));
|
||||||
AddLog(LOG_LEVEL_ERROR, PSTR("SR4: Reading CRC error"));
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//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;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue