1
0

main.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "wifi.h"
  14. // Externs
  15. extern void ledc_init(void);
  16. extern void init_pcnt_unit();
  17. extern float getkWh();
  18. // Chip info:
  19. // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
  20. /** Electric meter:
  21. * 10000 impulses per kWh
  22. * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
  23. *
  24. * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
  25. * So we need some other way of counting higher numbers that will last for one h
  26. * Probably we need to use interrupts !
  27. */
  28. void app_main(void)
  29. {
  30. printf("HomeEnergyMeter ESP32\n");
  31. init_pcnt_unit();
  32. ledc_init();
  33. #ifdef WIFI_ENABLED
  34. initWifi(); // Init WIFI
  35. #endif
  36. float kWh;
  37. while( true ) {
  38. kWh = getkWh();
  39. printf("%f\n",kWh);
  40. vTaskDelay(1000 / portTICK_PERIOD_MS);
  41. }
  42. esp_restart();
  43. }