1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include <stdio.h>
- #include "sdkconfig.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_system.h"
- #include "esp_spi_flash.h"
- #include "freertos/queue.h"
- #include "driver/ledc.h"
- #include "driver/pcnt.h"
- #include "esp_attr.h"
- #include "esp_log.h"
- #include "config.h"
- // Externs
- extern void ledc_init(void);
- extern void init_pcnt_unit(int unit);
- const int pcnt_unit = PCNT_UNIT_0;
- // Chip info:
- // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
- /** Electric meter:
- * 10000 impulses per kWh
- * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
- *
- * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
- * So we need some other way of counting higher numbers that will last for one h
- * Probably we need to use interrupts !
- void app_main(void)
- {
- printf("HomeEnergyMeter ESP32\n");
- init_pcnt_unit(pcnt_unit);
- ledc_init();
- for (int i = 600; i >= 0; i--) {
- int16_t count = 0;
- pcnt_get_counter_value(pcnt_unit, &count);
- printf("%d\n",count);
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- }
- printf("Restarting now.\n");
- fflush(stdout);
- esp_restart();
- }
|