2018-04-20 13:31:09 +01:00
|
|
|
/*
|
|
|
|
xsns_22_sr04.ino - SR04 ultrasonic sensor support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Nuno Ferreira and Theo Arends
|
2018-04-20 13:31:09 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-03-11 15:49:59 +00:00
|
|
|
#define XSNS_22 22
|
|
|
|
|
2018-04-20 13:31:09 +01:00
|
|
|
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-12-01 21:15:19 +00:00
|
|
|
void Sr04Init(void)
|
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
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Sr04Show(bool json)
|
2018-04-20 13:31:09 +01:00
|
|
|
{
|
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
|
2018-12-21 15:17:06 +00:00
|
|
|
char distance_chr[33];
|
2018-11-28 09:33:59 +00:00
|
|
|
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
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns22(uint8_t function)
|
2018-04-20 13:31:09 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-04-20 13:31:09 +01:00
|
|
|
|
|
|
|
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
|