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.

126 lines
3.4 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. // Just one channel
  21. _count = _magnitudes;
  22. // Bit depth
  23. _resolution = INTERNAL_ADC_RESOLUTION;
  24. // Init analog PIN
  25. pinMode(_gpio, INPUT);
  26. // Call the parent class method
  27. EmonSensor::begin();
  28. // warmup channel 0 (the only one)
  29. _pivot = _adc_counts >> 1;
  30. read(_gpio, _pivot);
  31. }
  32. // Descriptive name of the sensor
  33. String name() {
  34. char buffer[20];
  35. snprintf(buffer, sizeof(buffer), "EMON @ GPIO%d", _gpio);
  36. return String(buffer);
  37. }
  38. // Descriptive name of the slot # index
  39. String slot(unsigned char index) {
  40. return name();
  41. }
  42. // Type for slot # index
  43. magnitude_t type(unsigned char index) {
  44. _error = SENSOR_ERROR_OK;
  45. unsigned char i=0;
  46. #if EMON_REPORT_CURRENT
  47. if (index == i++) return MAGNITUDE_CURRENT;
  48. #endif
  49. #if EMON_REPORT_POWER
  50. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  51. #endif
  52. #if EMON_REPORT_ENERGY
  53. if (index == i) return MAGNITUDE_ENERGY;
  54. #endif
  55. _error = SENSOR_ERROR_OUT_OF_RANGE;
  56. return MAGNITUDE_NONE;
  57. }
  58. // Pre-read hook (usually to populate registers with up-to-date data)
  59. void pre() {
  60. _current = read(0, _pivot);
  61. #if EMON_REPORT_ENERGY
  62. static unsigned long last = 0;
  63. if (last > 0) {
  64. _energy += (_current * _voltage * (millis() - last) / 1000);
  65. }
  66. last = millis();
  67. #endif
  68. }
  69. // Current value for slot # index
  70. double value(unsigned char index) {
  71. _error = SENSOR_ERROR_OK;
  72. unsigned char i=0;
  73. #if EMON_REPORT_CURRENT
  74. if (index == i++) return _current;
  75. #endif
  76. #if EMON_REPORT_POWER
  77. if (index == i++) return _current * _voltage;
  78. #endif
  79. #if EMON_REPORT_ENERGY
  80. if (index == i) return _energy;
  81. #endif
  82. _error = SENSOR_ERROR_OUT_OF_RANGE;
  83. return 0;
  84. }
  85. protected:
  86. unsigned int readADC(unsigned char channel) {
  87. return analogRead(_gpio);
  88. }
  89. unsigned char _gpio = 0;
  90. double _pivot = 0;
  91. double _current;
  92. #if EMON_REPORT_ENERGY
  93. unsigned long _energy = 0;
  94. #endif
  95. };