esp32 firmware for a toaster reflow oven WIP!!!!!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
reflower/main/app_main.c

149 lines
3.9 KiB

/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <web/websrv.h>
#include <esp_wifi.h>
#include <esp_log.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "graphics/nokia.h"
#include "knob.h"
#include "gui.h"
#include "analog.h"
#include "utils.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "firehazard.h"
/**
* Application event handler
*
* @param ctx - ignored, context passed to esp_event_loop_init()
* @param event
* @return success
*/
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id) {
case SYSTEM_EVENT_STA_START:
try_reconn_if_have_wifi_creds();
break;
case SYSTEM_EVENT_STA_CONNECTED:
// we should get an IP address soon
break;
case SYSTEM_EVENT_STA_GOT_IP:
// xEventGroupSetBits(g_wifi_event_group, EG_WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
// xEventGroupClearBits(g_wifi_event_group, EG_WIFI_CONNECTED_BIT);
try_reconn_if_have_wifi_creds();
break;
default:
break;
}
// dhcp_watchdog_notify(dhcp_wd, event->event_id);
return ESP_OK;
}
/**
* Initialize WiFi in station mode, try to connect if settings are stored.
* Set up WiFi event group & start related tasks
*/
static void initialise_wifi(void)
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}
void __attribute__((noreturn)) app_main()
{
// Initialize NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
//ESP_ERROR_CHECK(esp_register_shutdown_handler(cspemu_run_shutdown_handlers));
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
gpio_config_t output = {
.pin_bit_mask = (1<<22),
.mode = GPIO_MODE_OUTPUT
};
gpio_config(&output);
tcpip_adapter_init();
initialise_wifi();
// TODO add proper join procedure
const char *ssid = "Chlivek_2.4G";
const char *pass = "slepice123";
wifi_config_t wifi_config = {};
strncpy((char*) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
if (pass) {
strncpy((char*) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
}
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_connect() );
gui_init();
knob_init();
analog_init();
try_reconn_if_have_wifi_creds();
websrv_init();
fire_init();
nvs_handle nvs;
ESP_ERROR_CHECK(nvs_open("config", NVS_READWRITE, &nvs)); // must use RW to allow opening nonexistent
union uf32 kp, ki, kd;
// TODO set good defaults
kp.f = 0.3f;
ki.f = 0.5f;
kd.f = 0.0f;
nvs_get_u32(nvs, "kp", &kp.u);
nvs_get_u32(nvs, "ki", &ki.u);
nvs_get_u32(nvs, "kd", &kd.u);
fire_set_tuning(kp.f, ki.f, kd.f);
nvs_close(nvs);
bool level = 0;
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
gpio_set_level(22, level);
level ^= 1;
}
}