esp32 firmware for a toaster reflow oven WIP!!!!!

app_main.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Hello World Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <web/websrv.h>
  9. #include <esp_wifi.h>
  10. #include <esp_log.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "esp_system.h"
  14. #include "esp_spi_flash.h"
  15. #include "graphics/nokia.h"
  16. #include "knob.h"
  17. #include "gui.h"
  18. #include "analog.h"
  19. #include "utils.h"
  20. #include "esp_event_loop.h"
  21. #include "nvs_flash.h"
  22. #include "firehazard.h"
  23. /**
  24. * Application event handler
  25. *
  26. * @param ctx - ignored, context passed to esp_event_loop_init()
  27. * @param event
  28. * @return success
  29. */
  30. static esp_err_t event_handler(void *ctx, system_event_t *event)
  31. {
  32. switch (event->event_id) {
  33. case SYSTEM_EVENT_STA_START:
  34. try_reconn_if_have_wifi_creds();
  35. break;
  36. case SYSTEM_EVENT_STA_CONNECTED:
  37. // we should get an IP address soon
  38. break;
  39. case SYSTEM_EVENT_STA_GOT_IP:
  40. // xEventGroupSetBits(g_wifi_event_group, EG_WIFI_CONNECTED_BIT);
  41. break;
  42. case SYSTEM_EVENT_STA_DISCONNECTED:
  43. // xEventGroupClearBits(g_wifi_event_group, EG_WIFI_CONNECTED_BIT);
  44. try_reconn_if_have_wifi_creds();
  45. break;
  46. default:
  47. break;
  48. }
  49. // dhcp_watchdog_notify(dhcp_wd, event->event_id);
  50. return ESP_OK;
  51. }
  52. /**
  53. * Initialize WiFi in station mode, try to connect if settings are stored.
  54. * Set up WiFi event group & start related tasks
  55. */
  56. static void initialise_wifi(void)
  57. {
  58. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  59. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  60. ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
  61. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  62. ESP_ERROR_CHECK(esp_wifi_start());
  63. }
  64. void __attribute__((noreturn)) app_main()
  65. {
  66. // Initialize NVS
  67. esp_err_t err = nvs_flash_init();
  68. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  69. // NVS partition was truncated and needs to be erased
  70. // Retry nvs_flash_init
  71. ESP_ERROR_CHECK(nvs_flash_erase());
  72. err = nvs_flash_init();
  73. }
  74. ESP_ERROR_CHECK( err );
  75. //ESP_ERROR_CHECK(esp_register_shutdown_handler(cspemu_run_shutdown_handlers));
  76. ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
  77. gpio_config_t output = {
  78. .pin_bit_mask = (1<<22),
  79. .mode = GPIO_MODE_OUTPUT
  80. };
  81. gpio_config(&output);
  82. tcpip_adapter_init();
  83. initialise_wifi();
  84. // TODO add proper join procedure
  85. const char *ssid = "Chlivek_2.4G";
  86. const char *pass = "slepice123";
  87. wifi_config_t wifi_config = {};
  88. strncpy((char*) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
  89. if (pass) {
  90. strncpy((char*) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
  91. }
  92. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  93. ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  94. ESP_ERROR_CHECK( esp_wifi_disconnect() );
  95. ESP_ERROR_CHECK( esp_wifi_connect() );
  96. gui_init();
  97. knob_init();
  98. analog_init();
  99. try_reconn_if_have_wifi_creds();
  100. websrv_init();
  101. fire_init();
  102. nvs_handle nvs;
  103. ESP_ERROR_CHECK(nvs_open("config", NVS_READWRITE, &nvs)); // must use RW to allow opening nonexistent
  104. union uf32 kp, ki, kd;
  105. // TODO set good defaults
  106. kp.f = 0.3f;
  107. ki.f = 0.5f;
  108. kd.f = 0.0f;
  109. nvs_get_u32(nvs, "kp", &kp.u);
  110. nvs_get_u32(nvs, "ki", &ki.u);
  111. nvs_get_u32(nvs, "kd", &kd.u);
  112. fire_set_tuning(kp.f, ki.f, kd.f);
  113. nvs_close(nvs);
  114. bool level = 0;
  115. while (1) {
  116. vTaskDelay(pdMS_TO_TICKS(1000));
  117. gpio_set_level(22, level);
  118. level ^= 1;
  119. }
  120. }