mirror of https://github.com/arendst/Tasmota.git
Added support to multiple SR04 (#20975)
* added support to multiple SR04 * Reduced MAX # of SR04 to 3 as ESP32 has only 3 serial uarts
This commit is contained in:
parent
b15a0e2f1f
commit
60666fa1c5
|
@ -92,6 +92,7 @@ const uint8_t MAX_IRSEND = 16; // Max number of IRSEND GPIOs
|
||||||
const uint8_t MAX_RULE_SETS = 3; // Max number of rule sets of size 512 characters
|
const uint8_t MAX_RULE_SETS = 3; // Max number of rule sets of size 512 characters
|
||||||
const uint16_t MAX_RULE_SIZE = 512; // Max number of characters in rules
|
const uint16_t MAX_RULE_SIZE = 512; // Max number of characters in rules
|
||||||
const uint16_t VL53LXX_MAX_SENSORS = 8; // Max number of VL53L0X sensors
|
const uint16_t VL53LXX_MAX_SENSORS = 8; // Max number of VL53L0X sensors
|
||||||
|
const uint8_t MAX_SR04 = 3; // Max number of SR04 ultrasonic sensors
|
||||||
|
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
const uint8_t MAX_I2C = 2; // Max number of I2C controllers (ESP32 = 2)
|
const uint8_t MAX_I2C = 2; // Max number of I2C controllers (ESP32 = 2)
|
||||||
|
|
|
@ -835,8 +835,8 @@ const uint16_t kGpioNiceList[] PROGMEM = {
|
||||||
AGPIO(GPIO_RF_SENSOR), // Rf receiver with sensor decoding
|
AGPIO(GPIO_RF_SENSOR), // Rf receiver with sensor decoding
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SR04
|
#ifdef USE_SR04
|
||||||
AGPIO(GPIO_SR04_TRIG), // SR04 Tri/TXgger pin
|
AGPIO(GPIO_SR04_TRIG) + MAX_SR04, // SR04 Tri/TXgger pin
|
||||||
AGPIO(GPIO_SR04_ECHO), // SR04 Ech/RXo pin
|
AGPIO(GPIO_SR04_ECHO) + MAX_SR04, // SR04 Ech/RXo pin
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_ME007
|
#ifdef USE_ME007
|
||||||
AGPIO(GPIO_ME007_TRIG), // ME007 Trigger pin (xsns_23_me007.ino)
|
AGPIO(GPIO_ME007_TRIG), // ME007 Trigger pin (xsns_23_me007.ino)
|
||||||
|
|
|
@ -45,10 +45,10 @@ struct {
|
||||||
float distance;
|
float distance;
|
||||||
uint8_t valid;
|
uint8_t valid;
|
||||||
uint8_t type = SR04_NOT_DETECTED;
|
uint8_t type = SR04_NOT_DETECTED;
|
||||||
} SR04;
|
NewPing* sonar = nullptr;
|
||||||
|
TasmotaSerial* sonar_serial = nullptr;
|
||||||
|
} SR04[MAX_SR04];
|
||||||
|
|
||||||
NewPing* sonar = nullptr;
|
|
||||||
TasmotaSerial* sonar_serial = nullptr;
|
|
||||||
|
|
||||||
uint16_t Sr04TMiddleValue(uint16_t first, uint16_t second, uint16_t third) {
|
uint16_t Sr04TMiddleValue(uint16_t first, uint16_t second, uint16_t third) {
|
||||||
uint16_t ret = first;
|
uint16_t ret = first;
|
||||||
|
@ -66,19 +66,19 @@ uint16_t Sr04TMiddleValue(uint16_t first, uint16_t second, uint16_t third) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Sr04TMode2Distance(void) {
|
uint16_t Sr04TMode2Distance(uint32_t i) {
|
||||||
uint8_t buffer[4]; // Accommodate either 2 or 4 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) {
|
||||||
if (sonar_serial->available() && (buffer_idx < sizeof(buffer))) {
|
if (SR04[i].sonar_serial->available() && (buffer_idx < sizeof(buffer))) {
|
||||||
buffer[buffer_idx++] = sonar_serial->read();
|
buffer[buffer_idx++] = SR04[i].sonar_serial->read();
|
||||||
end = millis() + 10;
|
end = millis() + 10;
|
||||||
}
|
}
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
||||||
if (SR04_MODE_NONE == SR04.type) { // Only log during detection
|
if (SR04_MODE_NONE == SR04[i].type) { // Only log during detection
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SR4: Received '%*_H'"), buffer_idx, buffer);
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SR4-%d: Received '%*_H'"), i+1, buffer_idx, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t distance = 0;
|
uint32_t distance = 0;
|
||||||
|
@ -90,7 +90,7 @@ uint16_t Sr04TMode2Distance(void) {
|
||||||
if (crc == buffer[3]) { // Check crc sum
|
if (crc == buffer[3]) { // Check crc sum
|
||||||
distance = (buffer[1] << 8) + buffer[2];
|
distance = (buffer[1] << 8) + buffer[2];
|
||||||
} else {
|
} else {
|
||||||
AddLog(LOG_LEVEL_ERROR, PSTR("SR4: CRC error"));
|
AddLog(LOG_LEVEL_ERROR, PSTR("SR4-%d: CRC error"), i+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (buffer_idx > 1) { // US-100 serial has no CRC
|
else if (buffer_idx > 1) { // US-100 serial has no CRC
|
||||||
|
@ -101,108 +101,111 @@ uint16_t Sr04TMode2Distance(void) {
|
||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Sr04TMode3Distance() {
|
uint16_t Sr04TMode3Distance(uint32_t i) {
|
||||||
sonar_serial->write(0x55);
|
SR04[i].sonar_serial->write(0x55);
|
||||||
sonar_serial->flush();
|
SR04[i].sonar_serial->flush();
|
||||||
|
|
||||||
return Sr04TMode2Distance();
|
return Sr04TMode2Distance(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************************************************************************************/
|
/*********************************************************************************************/
|
||||||
|
|
||||||
void Sr04TModeDetect(void) {
|
void Sr04TModeDetect(void) {
|
||||||
SR04.type = SR04_MODE_NONE;
|
for (uint32_t i = 0; i < MAX_SR04; i++) {
|
||||||
if (!PinUsed(GPIO_SR04_ECHO)) { return; }
|
SR04[i].type = SR04_MODE_NONE;
|
||||||
|
if (!PinUsed(GPIO_SR04_ECHO, i)) { continue; }
|
||||||
|
|
||||||
int sr04_echo_pin = Pin(GPIO_SR04_ECHO);
|
int sr04_echo_pin = Pin(GPIO_SR04_ECHO, i);
|
||||||
int sr04_trig_pin = Pin(GPIO_SR04_TRIG); // if GPIO_SR04_TRIG is not configured use single PIN mode with GPIO_SR04_TRIG as -1
|
int sr04_trig_pin = Pin(GPIO_SR04_TRIG, i); // if GPIO_SR04_TRIG is not configured use single PIN mode with GPIO_SR04_TRIG as -1
|
||||||
sonar_serial = new TasmotaSerial(sr04_echo_pin, sr04_trig_pin, 1);
|
SR04[i].sonar_serial = new TasmotaSerial(sr04_echo_pin, sr04_trig_pin, 1);
|
||||||
|
|
||||||
if (sonar_serial && sonar_serial->begin(9600)) {
|
if (SR04[i].sonar_serial && SR04[i].sonar_serial->begin(9600)) {
|
||||||
DEBUG_SENSOR_LOG(PSTR("SR4: Detect mode"));
|
DEBUG_SENSOR_LOG(PSTR("SR4: Detect mode"));
|
||||||
|
|
||||||
if (PinUsed(GPIO_SR04_TRIG)) {
|
if (PinUsed(GPIO_SR04_TRIG, i)) {
|
||||||
SR04.type = (Sr04TMiddleValue(Sr04TMode3Distance(), Sr04TMode3Distance(), Sr04TMode3Distance()) != 0) ? SR04_MODE_SER_TRANSCEIVER : SR04_MODE_TRIGGER_ECHO;
|
SR04[i].type = (Sr04TMiddleValue(Sr04TMode3Distance(i), Sr04TMode3Distance(i), Sr04TMode3Distance(i)) != 0) ? SR04_MODE_SER_TRANSCEIVER : SR04_MODE_TRIGGER_ECHO;
|
||||||
} else {
|
} else {
|
||||||
SR04.type = (Sr04TMiddleValue(Sr04TMode2Distance(), Sr04TMode2Distance(), Sr04TMode2Distance()) != 0) ? SR04_MODE_SER_RECEIVER : SR04_MODE_TRIGGER_ECHO;
|
SR04[i].type = (Sr04TMiddleValue(Sr04TMode2Distance(i), Sr04TMode2Distance(i), Sr04TMode2Distance(i)) != 0) ? SR04_MODE_SER_RECEIVER : SR04_MODE_TRIGGER_ECHO;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
SR04.type = SR04_MODE_TRIGGER_ECHO;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SR04.type < SR04_MODE_SER_RECEIVER) {
|
|
||||||
if (sonar_serial) {
|
|
||||||
delete sonar_serial;
|
|
||||||
sonar_serial = nullptr;
|
|
||||||
}
|
|
||||||
sr04_trig_pin = (PinUsed(GPIO_SR04_TRIG)) ? Pin(GPIO_SR04_TRIG) : Pin(GPIO_SR04_ECHO); // if GPIO_SR04_TRIG is not configured use single PIN mode with GPIO_SR04_ECHO only
|
|
||||||
sonar = new NewPing(sr04_trig_pin, sr04_echo_pin, SR04_MAX_SENSOR_DISTANCE);
|
|
||||||
delay(100); // give time to inizialise, preventing ping_median fails
|
|
||||||
if (!sonar || !sonar->ping_median(5)) {
|
|
||||||
SR04.type = SR04_MODE_NONE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (sonar_serial) {
|
|
||||||
if (sonar_serial->hardwareSerial()) {
|
|
||||||
ClaimSerial();
|
|
||||||
}
|
}
|
||||||
#ifdef ESP32
|
} else {
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("SR4: Serial UART%d"), sonar_serial->getUart());
|
SR04[i].type = SR04_MODE_TRIGGER_ECHO;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_INFO,PSTR("SR4: Mode %d"), SR04.type);
|
if (SR04[i].type < SR04_MODE_SER_RECEIVER) {
|
||||||
|
if (SR04[i].sonar_serial) {
|
||||||
|
delete SR04[i].sonar_serial;
|
||||||
|
SR04[i].sonar_serial = nullptr;
|
||||||
|
}
|
||||||
|
sr04_trig_pin = (PinUsed(GPIO_SR04_TRIG, i)) ? Pin(GPIO_SR04_TRIG,i ) : Pin(GPIO_SR04_ECHO, i); // if GPIO_SR04_TRIG is not configured use single PIN mode with GPIO_SR04_ECHO only
|
||||||
|
SR04[i].sonar = new NewPing(sr04_trig_pin, sr04_echo_pin, SR04_MAX_SENSOR_DISTANCE);
|
||||||
|
delay(100); // give time to inizialise, preventing ping_median fails
|
||||||
|
if (!SR04[i].sonar || !SR04[i].sonar->ping_median(5)) {
|
||||||
|
SR04[i].type = SR04_MODE_NONE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (SR04[i].sonar_serial) {
|
||||||
|
if (SR04[i].sonar_serial->hardwareSerial()) {
|
||||||
|
ClaimSerial();
|
||||||
|
}
|
||||||
|
#ifdef ESP32
|
||||||
|
AddLog(LOG_LEVEL_DEBUG, PSTR("SR4-%d: Serial UART%d"), i+1, SR04[i].sonar_serial->getUart());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AddLog(LOG_LEVEL_INFO,PSTR("SR4-%d: Mode %d"), i+1, SR04[i].type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sr04TReading(void) {
|
void Sr04TReading(uint32_t i) {
|
||||||
if (TasmotaGlobal.uptime < 3) { return; }
|
if (TasmotaGlobal.uptime < 3) { return; }
|
||||||
|
if (SR04[i].valid) {
|
||||||
if (SR04.valid) {
|
SR04[i].valid--;
|
||||||
SR04.valid--;
|
|
||||||
} else {
|
} else {
|
||||||
SR04.distance = 0;
|
SR04[i].distance = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float distance;
|
float distance;
|
||||||
switch (SR04.type) {
|
switch (SR04[i].type) {
|
||||||
case SR04_NOT_DETECTED:
|
case SR04_NOT_DETECTED:
|
||||||
Sr04TModeDetect();
|
Sr04TModeDetect();
|
||||||
SR04.valid = (SR04.type) ? SENSOR_MAX_MISS : 0;
|
SR04[i].valid = (SR04[i].type) ? SENSOR_MAX_MISS : 0;
|
||||||
break;
|
break;
|
||||||
case SR04_MODE_SER_TRANSCEIVER:
|
case SR04_MODE_SER_TRANSCEIVER:
|
||||||
distance = (float)(Sr04TMiddleValue(Sr04TMode3Distance(), Sr04TMode3Distance(), Sr04TMode3Distance())) / 10; // Convert to cm
|
distance = (float)(Sr04TMiddleValue(Sr04TMode3Distance(i), Sr04TMode3Distance(i), Sr04TMode3Distance(i))) / 10; // Convert to cm
|
||||||
break;
|
break;
|
||||||
case SR04_MODE_SER_RECEIVER:
|
case SR04_MODE_SER_RECEIVER:
|
||||||
//empty input buffer first
|
//empty input buffer first
|
||||||
while(sonar_serial->available()) { sonar_serial->read(); }
|
while(SR04[i].sonar_serial->available()) { SR04[i].sonar_serial->read(); }
|
||||||
distance = (float)(Sr04TMiddleValue(Sr04TMode2Distance(), Sr04TMode2Distance(), Sr04TMode2Distance())) / 10; // Convert to cm
|
distance = (float)(Sr04TMiddleValue(Sr04TMode2Distance(i), Sr04TMode2Distance(i), Sr04TMode2Distance(i))) / 10; // Convert to cm
|
||||||
break;
|
break;
|
||||||
case SR04_MODE_TRIGGER_ECHO:
|
case SR04_MODE_TRIGGER_ECHO:
|
||||||
distance = (float)(sonar->ping_median(5)) / US_ROUNDTRIP_CM;
|
distance = (float)(SR04[i].sonar->ping_median(5)) / US_ROUNDTRIP_CM;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
distance = 0;
|
distance = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (distance) {
|
if (distance) {
|
||||||
SR04.distance = distance;
|
SR04[i].distance = distance;
|
||||||
SR04.valid = SENSOR_MAX_MISS;
|
SR04[i].valid = SENSOR_MAX_MISS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sr04Show(bool json) {
|
void Sr04Show(uint32_t i, bool json) {
|
||||||
if (SR04.valid) { // Check if read failed
|
if (SR04[i].valid) {
|
||||||
|
char types[12];
|
||||||
|
snprintf_P(types, sizeof(types), PSTR("SR04%c%d"), IndexSeparator(), i+1);
|
||||||
if(json) {
|
if(json) {
|
||||||
ResponseAppend_P(PSTR(",\"SR04\":{\"" D_JSON_DISTANCE "\":%1_f}"), &SR04.distance);
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%1_f}"), types, &SR04[i].distance);
|
||||||
#ifdef USE_DOMOTICZ
|
#ifdef USE_DOMOTICZ
|
||||||
if (0 == TasmotaGlobal.tele_period) {
|
if (0 == TasmotaGlobal.tele_period) {
|
||||||
DomoticzFloatSensor(DZ_COUNT, SR04.distance); // Send distance as Domoticz Counter value
|
DomoticzFloatSensor(DZ_COUNT, SR04[i].distance); // Send distance as Domoticz Counter value
|
||||||
}
|
}
|
||||||
#endif // USE_DOMOTICZ
|
#endif // USE_DOMOTICZ
|
||||||
#ifdef USE_WEBSERVER
|
#ifdef USE_WEBSERVER
|
||||||
} else {
|
} else {
|
||||||
WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, "SR04", &SR04.distance);
|
WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, types, &SR04[i].distance);
|
||||||
#endif // USE_WEBSERVER
|
#endif // USE_WEBSERVER
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -215,20 +218,22 @@ void Sr04Show(bool json) {
|
||||||
bool Xsns22(uint32_t function) {
|
bool Xsns22(uint32_t function) {
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
if (SR04.type) {
|
for (uint32_t i = 0; i < MAX_SR04; i++) {
|
||||||
switch (function) {
|
if (SR04[i].type) {
|
||||||
case FUNC_EVERY_SECOND:
|
switch (function) {
|
||||||
Sr04TReading();
|
case FUNC_EVERY_SECOND:
|
||||||
result = true;
|
Sr04TReading(i);
|
||||||
break;
|
result = true;
|
||||||
case FUNC_JSON_APPEND:
|
break;
|
||||||
Sr04Show(1);
|
case FUNC_JSON_APPEND:
|
||||||
break;
|
Sr04Show(i, 1);
|
||||||
#ifdef USE_WEBSERVER
|
break;
|
||||||
case FUNC_WEB_SENSOR:
|
#ifdef USE_WEBSERVER
|
||||||
Sr04Show(0);
|
case FUNC_WEB_SENSOR:
|
||||||
break;
|
Sr04Show(i, 0);
|
||||||
#endif // USE_WEBSERVER
|
break;
|
||||||
|
#endif // USE_WEBSERVER
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in New Issue