main.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "freertos/FreeRTOS.h"
  2. #include "freertos/task.h"
  3. #include "esp_log.h"
  4. #include "config.h"
  5. #include "wifi.h"
  6. #include "mqtt.h"
  7. #include "kWhCounter.h"
  8. #include "http_client.h"
  9. #include "readTemps.h"
  10. // Chip info:
  11. // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
  12. void app_main(void)
  13. {
  14. char txt[50];
  15. ESP_LOGI("MAIN", "HomeEnergyMeter ESP32. Core:%d",xPortGetCoreID());
  16. gpio_reset_pin(VVB_RELAY_OUTPUT_IO);
  17. gpio_set_direction(VVB_RELAY_OUTPUT_IO, GPIO_MODE_OUTPUT);
  18. // Wait for stable environment
  19. vTaskDelay(2000.0 / portTICK_PERIOD_MS);
  20. #ifdef ENABLE_DS18B20
  21. initTempReadings();
  22. #endif
  23. #ifdef ENABLE_KWH_COUNTER
  24. kWhCounter_init();
  25. #endif
  26. #ifdef WIFI_ENABLED
  27. initWifi(); // Init WIFI
  28. #endif
  29. #ifdef MQTT_ENABLED
  30. mqtt_init();
  31. #endif
  32. const TickType_t xFreq = 5000 / portTICK_PERIOD_MS;
  33. TickType_t vLastWakeTime = xTaskGetTickCount();
  34. // ---------------- MAIN WHILE -------------------
  35. while(1) {
  36. vTaskDelayUntil( &vLastWakeTime, 2000/portTICK_PERIOD_MS );
  37. readAndSendTemps();
  38. vTaskDelayUntil( &vLastWakeTime, xFreq );
  39. #ifdef HTTP_ENABLED
  40. http_rest_with_url();
  41. #endif
  42. vTaskDelayUntil( &vLastWakeTime, xFreq );
  43. sprintf(txt,"%u",kWh_cnt);
  44. #ifdef MQTT_ENABLED
  45. sendMQTTMessage("basement/boiler/onTime", txt);
  46. #endif
  47. #ifdef ENABLE_DS18B20
  48. readAndSendTemps();
  49. #endif
  50. }
  51. vTaskDelete(NULL);
  52. }