1
0

main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "uart.h"
  15. #include "serial.h"
  16. #include "ssd1306.h"
  17. #include "ssd1306_driver.h"
  18. // Externs
  19. extern void ledc_init(void);
  20. extern void init_pcnt_unit();
  21. extern double getkWh(uint32_t *bigCnt, int32_t *cnt);
  22. extern void mqtt_init();
  23. extern void sendMQTTMessage(const char * topic, const char * data);
  24. extern void sendHTTPMessage(const double inData, const uint32_t bigCnt, const int32_t cnt);
  25. void displayTask(void *pvParameters);
  26. // Chip info:
  27. // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
  28. /** Electric meter:
  29. * 10000 impulses per kWh
  30. * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
  31. *
  32. * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
  33. * So we need some other way of counting higher numbers that will last for one h
  34. * Probably we need to use interrupts !
  35. */
  36. void app_main(void)
  37. {
  38. ESP_LOGI("MAIN", "HomeEnergyMeter ESP32");
  39. init_pcnt_unit();
  40. ledc_init();
  41. #ifdef WIFI_ENABLED
  42. initWifi(); // Init WIFI
  43. #endif
  44. #ifdef MQTT_ENABLED
  45. mqtt_init();
  46. #endif
  47. #ifdef SERIAL_ENABLED
  48. initUart();
  49. xTaskCreate(serialRxTask, "Serial_RX_Task", 10000, NULL, 10, NULL);
  50. #endif
  51. char dataStr[100];
  52. double kWh = 0.0;
  53. uint32_t bigCnt = 0;
  54. int32_t cnt = 0;
  55. xTaskCreate(displayTask, "OLED-Task", 1024*10, NULL, 2, NULL);
  56. while( true ) {
  57. kWh = getkWh(&bigCnt, &cnt);
  58. sprintf(dataStr,"%.5f",kWh);
  59. // @TODO This is changed for testing
  60. #ifdef MQTT_ENABLED
  61. sendMQTTMessage("/sensors/TEST/energy/electricalTotal", dataStr);
  62. #endif
  63. #ifdef WIFI_ENABLED
  64. sendHTTPMessage(kWh, bigCnt, cnt);
  65. #endif
  66. ESP_LOGI("MAIN", "%.4f %u %d",kWh,bigCnt,cnt);
  67. vTaskDelay(60000 / portTICK_PERIOD_MS);
  68. }
  69. esp_restart();
  70. }
  71. void displayTask(void *pvParameters) {
  72. #ifdef ENABLE_SSD1306
  73. i2c_master_init();
  74. oled_ssd1306_init();
  75. vTaskDelay(100 / portTICK_PERIOD_MS);
  76. SSD1306_SetPosition(4,18);
  77. SSD1306_DrawVertLine(29,0,128);
  78. SSD1306_Puts("Home Energy Meter", &Font_7x10,SSD1306_COLOR_WHITE);
  79. SSD1306_SetPosition(4,31);
  80. SSD1306_Puts("v1.0", &Font_7x10,SSD1306_COLOR_WHITE);
  81. SSD1306_UpdateScreen();
  82. vTaskDelay(3000 / portTICK_PERIOD_MS);
  83. SSD1306_Clear();
  84. SSD1306_SetPosition(0,0);
  85. SSD1306_Puts("Temp A:", &Font_7x10,SSD1306_COLOR_WHITE);
  86. SSD1306_SetPosition(0,11);
  87. SSD1306_Puts("Temp B:", &Font_7x10,SSD1306_COLOR_WHITE);
  88. SSD1306_SetPosition(0,22);
  89. SSD1306_Puts("ADC Vpp:", &Font_7x10,SSD1306_COLOR_WHITE);
  90. SSD1306_SetPosition(0,33);
  91. SSD1306_Puts("Pulses:", &Font_7x10,SSD1306_COLOR_WHITE);
  92. SSD1306_UpdateScreen();
  93. #endif
  94. vTaskDelete(NULL);
  95. }