main/dcf77tx.c

Thu, 19 Oct 2023 17:32:16 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 19 Oct 2023 17:32:16 +0200
changeset 0
913eb9ca40b1
child 1
86b275481021
permissions
-rw-r--r--

Initial checkin, WiFi connects and SNTP timesync works.

/**
 * @file dcf77tx.c
 * @brief DCF77 emulator/transmitter
 */
#include "dcf77tx.h"

static const char *TAG = "dcf77tx";

static TaskHandle_t			xTaskWifi = NULL;
char					hostname[32];
int					Main_Loop = ML_INIT;


extern SemaphoreHandle_t		xSemaphoreWiFi;
extern WIFI_State			*wifi_state;			///< WiFi state
extern bool				_wifi_ScanDone;
extern int8_t				_wifi_RSSI;


void app_main(void)
{
    esp_err_t	ret;

    ESP_LOGI(TAG, "Starting DCF77tx");

    /*
     * Initialize NVS
     */
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    uint8_t mac_addr[8] = {0};
    // Set the configured hostname for the dhcp client.
    esp_efuse_mac_get_default(mac_addr);
    sprintf(hostname, "dcf77tx-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]);

    esp_log_level_set("wifi", ESP_LOG_ERROR);
    xTaskCreate(&task_wifi,    "task_wifi",     4096, NULL, 3, &xTaskWifi);

    int wait = 150;
    while (wait) {
        vTaskDelay(100 / portTICK_PERIOD_MS);
        if (ready_WiFi()) {
                ESP_LOGI(TAG, "Online in %.1f seconds", (150 - wait) / 10.0);
                wait = 0;
        } else {
                wait--;
                if (wait < 1)
                        ESP_LOGE(TAG, "Timeout network connection");
        }
    }


    time_t now;
    struct tm timeinfo;
    char strftime_buf[64];
    time(&now);
    // Set timezone to Central Europe Time and print local time
    setenv("TZ", "CET-01CEST-02,M3.4.0,M10.4.0", 1);
    tzset();
    localtime_r(&now, &timeinfo);
    strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
    ESP_LOGI(TAG, "The current date/time in Amsterdam is: %s", strftime_buf);

    /*
     * Main application loop
     */
    while (1) {
	switch (Main_Loop) {
	    case ML_INIT:	if (ready_WiFi() ) {
				    Main_Loop = ML_SYNC;
				    ESP_LOGI(TAG, "ML_INIT -> ML_SYNC");
				} else {
				    Main_Loop = ML_CONNECT;
				    ESP_LOGI(TAG, "ML_INIT -> ML_CONNECT");
				}
		    		break;
	    case ML_CONNECT:	break;
	    case ML_SYNC:	break;
	    case ML_RUN:	break;
	}

	vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

mercurial