main.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include "sdkconfig.h"
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "esp_system.h"
  6. #include "esp_spi_flash.h"
  7. #include "freertos/queue.h"
  8. #include "driver/ledc.h"
  9. #include "driver/pcnt.h"
  10. #include "esp_attr.h"
  11. #include "esp_log.h"
  12. #include "config.h"
  13. // Externs
  14. extern void ledc_init(void);
  15. extern void init_pcnt_unit();
  16. extern float getkWh();
  17. // Chip info:
  18. // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
  19. /** Electric meter:
  20. * 10000 impulses per kWh
  21. * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
  22. *
  23. * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
  24. * So we need some other way of counting higher numbers that will last for one h
  25. * Probably we need to use interrupts !
  26. */
  27. void app_main(void)
  28. {
  29. printf("HomeEnergyMeter ESP32\n");
  30. init_pcnt_unit();
  31. ledc_init();
  32. float kWh;
  33. while( true ) {
  34. kWh = getkWh();
  35. printf("%f\n",kWh);
  36. vTaskDelay(1000 / portTICK_PERIOD_MS);
  37. }
  38. esp_restart();
  39. }