1
0

main.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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(int unit);
  16. const int pcnt_unit = PCNT_UNIT_0;
  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. void app_main(void)
  27. {
  28. printf("HomeEnergyMeter ESP32\n");
  29. init_pcnt_unit(pcnt_unit);
  30. ledc_init();
  31. for (int i = 600; i >= 0; i--) {
  32. int16_t count = 0;
  33. pcnt_get_counter_value(pcnt_unit, &count);
  34. printf("%d\n",count);
  35. vTaskDelay(1000 / portTICK_PERIOD_MS);
  36. }
  37. printf("Restarting now.\n");
  38. fflush(stdout);
  39. esp_restart();
  40. }