diff --git a/sonoff/i18n.h b/sonoff/i18n.h
index ce984f1e4227..2114b4dc1ba4 100644
--- a/sonoff/i18n.h
+++ b/sonoff/i18n.h
@@ -126,6 +126,7 @@
#define D_JSON_WRONG "Wrong"
#define D_JSON_YESTERDAY "Yesterday"
#define D_JSON_ZERO_POINT_CALIBRATION "Zero Point Calibration"
+#define D_JSON_DISTANCE "Distance"
#define D_RSLT_ENERGY "ENERGY"
#define D_RSLT_INFO "INFO"
diff --git a/sonoff/language/en-GB.h b/sonoff/language/en-GB.h
index d5555b094470..0f5515ab16a2 100644
--- a/sonoff/language/en-GB.h
+++ b/sonoff/language/en-GB.h
@@ -157,6 +157,7 @@
#define D_VOLTAGE "Voltage"
#define D_WARMLIGHT "Warm"
#define D_WEB_SERVER "Web Server"
+#define D_DISTANCE "Distance"
// sonoff.ino
#define D_WARNING_MINIMAL_VERSION "WARNING This version does not support persistent settings"
@@ -429,6 +430,8 @@
#define D_SENSOR_SDS0X1 "SDS0X1"
#define D_SENSOR_SBR_RX "SerBr Rx"
#define D_SENSOR_SBR_TX "SerBr Tx"
+#define D_SENSOR_SR04_TRIG "SR04 Trig"
+#define D_SENSOR_SR04_ECHO "SR04 Echo"
// Units
#define D_UNIT_AMPERE "A"
@@ -438,6 +441,7 @@
#define D_UNIT_LUX "lx"
#define D_UNIT_MICROGRAM_PER_CUBIC_METER "ug/m3"
#define D_UNIT_MICROMETER "um"
+#define D_UNIT_CENTIMETER "cm"
#define D_UNIT_MICROSECOND "us"
#define D_UNIT_MILLIAMPERE "mA"
#define D_UNIT_MILLISECOND "ms"
diff --git a/sonoff/sonoff_post.h b/sonoff/sonoff_post.h
index 4455519d063d..e097a05bfe1a 100644
--- a/sonoff/sonoff_post.h
+++ b/sonoff/sonoff_post.h
@@ -74,6 +74,7 @@ void WifiWpsStatusCallback(wps_cb_status status);
#endif
// #define USE_WS2812_DMA // DMA supports only GPIO03 (= Serial RXD) (+1k mem). When USE_WS2812_DMA is enabled expect Exceptions on Pow
#define USE_ARILUX_RF // Add support for Arilux RF remote controller (+0k8 code, 252 iram (non 2.3.0))
+#define USE_SR04 // Add support for ultrasonic SR04x sensors (+xxk code)
#endif // USE_ALL_SENSORS =====================
#if defined(USE_DS18x20) || defined(USE_DS18x20_LEGACY)
@@ -147,6 +148,9 @@ void WifiWpsStatusCallback(wps_cb_status status);
#ifdef DEBUG_THEO
#undef DEBUG_THEO // Disable debug code
#endif
+#ifdef USE_SR04
+#undef USE_SR04 // Disable SR04 support
+#endif
#endif // BE_MINIMAL ==========================
#ifndef SWITCH_MODE
diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h
index cbb197abb23e..a2cbf133943d 100644
--- a/sonoff/sonoff_template.h
+++ b/sonoff/sonoff_template.h
@@ -91,6 +91,8 @@ enum UserSelectablePins {
GPIO_SDS0X1, // Nova Fitness SDS011 Serial interface
GPIO_SBR_TX, // Serial Bridge Serial interface
GPIO_SBR_RX, // Serial Bridge Serial interface
+ GPIO_SR04_TRIG, // SR04 Trigger pin
+ GPIO_SR04_ECHO, // SR04 Echo pin
GPIO_SENSOR_END };
// Programmer selectable GPIO functionality offset by user selectable GPIOs
@@ -133,7 +135,8 @@ const char kSensorNames[] PROGMEM =
D_SENSOR_SAIR_TX "|" D_SENSOR_SAIR_RX "|"
D_SENSOR_SPI_CS "|" D_SENSOR_SPI_DC "|" D_SENSOR_BACKLIGHT "|"
D_SENSOR_PMS5003 "|" D_SENSOR_SDS0X1 "|"
- D_SENSOR_SBR_TX "|" D_SENSOR_SBR_RX;
+ D_SENSOR_SBR_TX "|" D_SENSOR_SBR_RX "|"
+ D_SENSOR_SR04_TRIG "|" D_SENSOR_SR04_ECHO;
/********************************************************************************************/
diff --git a/sonoff/user_config.h b/sonoff/user_config.h
index cf1d29985eb5..5663e5f80665 100644
--- a/sonoff/user_config.h
+++ b/sonoff/user_config.h
@@ -281,6 +281,8 @@
#define USE_ARILUX_RF // Add support for Arilux RF remote controller (+0k8 code, 252 iram (non 2.3.0))
+#define USE_SR04 // Add support for SR04 ultrasonic devices (+xk code)
+
/*********************************************************************************************\
* Select all sensors - overrides above undefines!!
\*********************************************************************************************/
diff --git a/sonoff/webserver.ino b/sonoff/webserver.ino
index 20ddb0b0ced2..849f2bdf8de4 100644
--- a/sonoff/webserver.ino
+++ b/sonoff/webserver.ino
@@ -653,6 +653,14 @@ boolean GetUsedInModule(byte val, uint8_t *arr)
return true;
}
#endif
+#ifndef USE_SR04
+ if (GPIO_SR04_TRIG == val) {
+ return true;
+ }
+ if (GPIO_SR04_ECHO == val) {
+ return true;
+ }
+#endif
#ifndef USE_WS2812
if (GPIO_WS2812 == val) {
return true;
diff --git a/sonoff/xsns_91_sr04.ino b/sonoff/xsns_91_sr04.ino
new file mode 100644
index 000000000000..8d6c3fa25270
--- /dev/null
+++ b/sonoff/xsns_91_sr04.ino
@@ -0,0 +1,167 @@
+/*
+ xsns_20_sr04.ino - SR04 ultrasonic sensor support for Sonoff-Tasmota
+
+ Copyright (C) 2018 Nuno Ferreira and Theo Arends
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see