xsns_02_analog.ino 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. xsns_02_analog.ino - ESP8266 ADC support for Sonoff-Tasmota
  3. Copyright (C) 2018 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. #ifndef USE_ADC_VCC
  16. /*********************************************************************************************\
  17. * ADC support
  18. \*********************************************************************************************/
  19. #define XSNS_02 2
  20. uint16_t adc_last_value = 0;
  21. uint16_t AdcRead(void)
  22. {
  23. uint16_t analog = 0;
  24. for (byte i = 0; i < 32; i++) {
  25. analog += analogRead(A0);
  26. delay(1);
  27. }
  28. analog >>= 5;
  29. return analog;
  30. }
  31. #ifdef USE_RULES
  32. void AdcEvery250ms(void)
  33. {
  34. uint16_t new_value = AdcRead();
  35. if ((new_value < adc_last_value -10) || (new_value > adc_last_value +10)) {
  36. adc_last_value = new_value;
  37. uint16_t value = adc_last_value / 10;
  38. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"ANALOG\":{\"A0div10\":%d}}"), (value > 99) ? 100 : value);
  39. XdrvRulesProcess();
  40. }
  41. }
  42. #endif // USE_RULES
  43. void AdcShow(boolean json)
  44. {
  45. uint16_t analog = AdcRead();
  46. if (json) {
  47. snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"ANALOG\":{\"A0\":%d}"), mqtt_data, analog);
  48. #ifdef USE_WEBSERVER
  49. } else {
  50. snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ANALOG, mqtt_data, "", 0, analog);
  51. #endif // USE_WEBSERVER
  52. }
  53. }
  54. /*********************************************************************************************\
  55. * Interface
  56. \*********************************************************************************************/
  57. boolean Xsns02(byte function)
  58. {
  59. boolean result = false;
  60. if (pin[GPIO_ADC0] < 99) {
  61. switch (function) {
  62. #ifdef USE_RULES
  63. case FUNC_EVERY_250_MSECOND:
  64. AdcEvery250ms();
  65. break;
  66. #endif // USE_RULES
  67. case FUNC_JSON_APPEND:
  68. AdcShow(1);
  69. break;
  70. #ifdef USE_WEBSERVER
  71. case FUNC_WEB_APPEND:
  72. AdcShow(0);
  73. break;
  74. #endif // USE_WEBSERVER
  75. }
  76. }
  77. return result;
  78. }
  79. #endif // USE_ADC_VCC