2018-04-20 13:31:09 +01:00
|
|
|
/*
|
|
|
|
xsns_22_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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_SR04
|
2018-11-28 09:33:59 +00:00
|
|
|
|
|
|
|
#include <NewPing.h>
|
2018-04-20 13:31:09 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* HC-SR04, HC-SR04+, JSN-SR04T - Ultrasonic distance sensor
|
|
|
|
*
|
|
|
|
* Code for SR04 family of ultrasonic distance sensors
|
|
|
|
* References:
|
|
|
|
* - https://www.dfrobot.com/wiki/index.php/Weather-proof_Ultrasonic_Sensor_SKU_:_SEN0207
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
uint8_t sr04_echo_pin = 0;
|
|
|
|
uint8_t sr04_trig_pin = 0;
|
2018-11-28 09:33:59 +00:00
|
|
|
real64_t distance;
|
2018-04-20 13:31:09 +01:00
|
|
|
|
2018-11-28 09:33:59 +00:00
|
|
|
NewPing* sonar = NULL;
|
2018-04-20 13:31:09 +01:00
|
|
|
|
2018-11-28 09:33:59 +00:00
|
|
|
void Sr04Init()
|
2018-04-20 13:31:09 +01:00
|
|
|
{
|
|
|
|
sr04_echo_pin = pin[GPIO_SR04_ECHO];
|
|
|
|
sr04_trig_pin = pin[GPIO_SR04_TRIG];
|
2018-11-28 09:33:59 +00:00
|
|
|
sonar = new NewPing(sr04_trig_pin, sr04_echo_pin, 300);
|
2018-04-20 13:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_DISTANCE[] PROGMEM =
|
2018-11-28 09:33:59 +00:00
|
|
|
"%s{s}SR04 " D_DISTANCE "{m}%s" D_UNIT_CENTIMETER "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
2018-04-20 13:31:09 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
|
|
|
void Sr04Show(boolean json)
|
|
|
|
{
|
2018-11-28 09:33:59 +00:00
|
|
|
distance = (real64_t)(sonar->ping_median(5))/ US_ROUNDTRIP_CM;
|
|
|
|
|
|
|
|
if (distance != 0) { // Check if read failed
|
|
|
|
char distance_chr[10];
|
|
|
|
|
|
|
|
dtostrfd(distance, 3, distance_chr);
|
2018-04-20 13:31:09 +01:00
|
|
|
|
|
|
|
if(json) {
|
2018-11-28 09:33:59 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"SR04\":{\"" D_JSON_DISTANCE "\":%s}"), mqtt_data, distance_chr);
|
2018-11-11 12:21:46 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
if (0 == tele_period) {
|
2018-11-28 09:33:59 +00:00
|
|
|
DomoticzSensor(DZ_COUNT, distance_chr); // Send distance as Domoticz Counter value
|
2018-11-11 12:21:46 +00:00
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
2018-04-20 13:31:09 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2018-11-28 09:33:59 +00:00
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_DISTANCE, mqtt_data, distance_chr);
|
2018-04-20 13:31:09 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-28 09:33:59 +00:00
|
|
|
#define XSNS_22
|
|
|
|
|
2018-04-20 13:31:09 +01:00
|
|
|
boolean Xsns22(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
if ((pin[GPIO_SR04_ECHO] < 99) && (pin[GPIO_SR04_TRIG] < 99)) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
Sr04Init();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Sr04Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_APPEND:
|
|
|
|
Sr04Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_SR04
|