Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // Energy Monitor Sensor using builtin ADC
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include "Arduino.h"
  7. #include "BaseSensor.h"
  8. #include "EmonSensor.h"
  9. #define INTERNAL_ADC_RESOLUTION 10
  10. class EmonAnalogSensor : public EmonSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. // ---------------------------------------------------------------------
  16. // Sensor API
  17. // ---------------------------------------------------------------------
  18. // Initialization method, must be idempotent
  19. void begin() {
  20. if (!_dirty) return;
  21. _dirty = false;
  22. // Just one channel
  23. _count = _magnitudes;
  24. // Bit depth
  25. _resolution = INTERNAL_ADC_RESOLUTION;
  26. // Init analog PIN)
  27. pinMode(_gpio, INPUT);
  28. // Call the parent class method
  29. EmonSensor::begin();
  30. // warmup channel 0 (the only one)
  31. _pivot = _adc_counts >> 1;
  32. read(_gpio, _pivot);
  33. }
  34. // Descriptive name of the sensor
  35. String name() {
  36. char buffer[20];
  37. snprintf(buffer, sizeof(buffer), "EMON @ GPIO%d", _gpio);
  38. return String(buffer);
  39. }
  40. // Descriptive name of the slot # index
  41. String slot(unsigned char index) {
  42. return name();
  43. }
  44. // Type for slot # index
  45. magnitude_t type(unsigned char index) {
  46. _error = SENSOR_ERROR_OK;
  47. unsigned char i=0;
  48. #if EMON_REPORT_CURRENT
  49. if (index == i++) return MAGNITUDE_CURRENT;
  50. #endif
  51. #if EMON_REPORT_POWER
  52. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  53. #endif
  54. #if EMON_REPORT_ENERGY
  55. if (index == i) return MAGNITUDE_ENERGY;
  56. #endif
  57. _error = SENSOR_ERROR_OUT_OF_RANGE;
  58. return MAGNITUDE_NONE;
  59. }
  60. // Pre-read hook (usually to populate registers with up-to-date data)
  61. void pre() {
  62. _current = read(0, _pivot);
  63. #if EMON_REPORT_ENERGY
  64. static unsigned long last = 0;
  65. if (last > 0) {
  66. _energy += (_current * _voltage * (millis() - last) / 1000);
  67. }
  68. last = millis();
  69. #endif
  70. }
  71. // Current value for slot # index
  72. double value(unsigned char index) {
  73. _error = SENSOR_ERROR_OK;
  74. unsigned char i=0;
  75. #if EMON_REPORT_CURRENT
  76. if (index == i++) return _current;
  77. #endif
  78. #if EMON_REPORT_POWER
  79. if (index == i++) return _current * _voltage;
  80. #endif
  81. #if EMON_REPORT_ENERGY
  82. if (index == i) return _energy;
  83. #endif
  84. _error = SENSOR_ERROR_OUT_OF_RANGE;
  85. return 0;
  86. }
  87. protected:
  88. unsigned int readADC(unsigned char channel) {
  89. return analogRead(_gpio);
  90. }
  91. unsigned char _gpio = 0;
  92. double _pivot = 0;
  93. double _current;
  94. #if EMON_REPORT_ENERGY
  95. unsigned long _energy = 0;
  96. #endif
  97. };