xsns_22_sr04.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. xsns_22_sr04.ino - SR04 ultrasonic sensor support for Sonoff-Tasmota
  3. Copyright (C) 2018 Nuno Ferreira and Theo Arends
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_SR04
  16. #include <NewPing.h>
  17. /*********************************************************************************************\
  18. * HC-SR04, HC-SR04+, JSN-SR04T - Ultrasonic distance sensor
  19. *
  20. * Code for SR04 family of ultrasonic distance sensors
  21. * References:
  22. * - https://www.dfrobot.com/wiki/index.php/Weather-proof_Ultrasonic_Sensor_SKU_:_SEN0207
  23. \*********************************************************************************************/
  24. uint8_t sr04_echo_pin = 0;
  25. uint8_t sr04_trig_pin = 0;
  26. real64_t distance;
  27. NewPing* sonar = NULL;
  28. void Sr04Init(void)
  29. {
  30. sr04_echo_pin = pin[GPIO_SR04_ECHO];
  31. sr04_trig_pin = pin[GPIO_SR04_TRIG];
  32. sonar = new NewPing(sr04_trig_pin, sr04_echo_pin, 300);
  33. }
  34. #ifdef USE_WEBSERVER
  35. const char HTTP_SNS_DISTANCE[] PROGMEM =
  36. "%s{s}SR04 " D_DISTANCE "{m}%s" D_UNIT_CENTIMETER "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
  37. #endif // USE_WEBSERVER
  38. void Sr04Show(boolean json)
  39. {
  40. distance = (real64_t)(sonar->ping_median(5))/ US_ROUNDTRIP_CM;
  41. if (distance != 0) { // Check if read failed
  42. char distance_chr[33];
  43. dtostrfd(distance, 3, distance_chr);
  44. if(json) {
  45. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"SR04\":{\"" D_JSON_DISTANCE "\":%s}"), mqtt_data, distance_chr);
  46. #ifdef USE_DOMOTICZ
  47. if (0 == tele_period) {
  48. DomoticzSensor(DZ_COUNT, distance_chr); // Send distance as Domoticz Counter value
  49. }
  50. #endif // USE_DOMOTICZ
  51. #ifdef USE_WEBSERVER
  52. } else {
  53. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_DISTANCE, mqtt_data, distance_chr);
  54. #endif // USE_WEBSERVER
  55. }
  56. }
  57. }
  58. /*********************************************************************************************\
  59. * Interface
  60. \*********************************************************************************************/
  61. #define XSNS_22
  62. boolean Xsns22(byte function)
  63. {
  64. boolean result = false;
  65. if ((pin[GPIO_SR04_ECHO] < 99) && (pin[GPIO_SR04_TRIG] < 99)) {
  66. switch (function) {
  67. case FUNC_INIT:
  68. Sr04Init();
  69. break;
  70. case FUNC_JSON_APPEND:
  71. Sr04Show(1);
  72. break;
  73. #ifdef USE_WEBSERVER
  74. case FUNC_WEB_APPEND:
  75. Sr04Show(0);
  76. break;
  77. #endif // USE_WEBSERVER
  78. }
  79. }
  80. return result;
  81. }
  82. #endif // USE_SR04