|
1 /** |
|
2 * @file dcf77tx.c |
|
3 * @brief DCF77 emulator/transmitter |
|
4 */ |
|
5 #include "dcf77tx.h" |
|
6 |
|
7 static const char *TAG = "dcf77tx"; |
|
8 |
|
9 static TaskHandle_t xTaskWifi = NULL; |
|
10 char hostname[32]; |
|
11 int Main_Loop = ML_INIT; |
|
12 |
|
13 |
|
14 extern SemaphoreHandle_t xSemaphoreWiFi; |
|
15 extern WIFI_State *wifi_state; ///< WiFi state |
|
16 extern bool _wifi_ScanDone; |
|
17 extern int8_t _wifi_RSSI; |
|
18 |
|
19 |
|
20 void app_main(void) |
|
21 { |
|
22 esp_err_t ret; |
|
23 |
|
24 ESP_LOGI(TAG, "Starting DCF77tx"); |
|
25 |
|
26 /* |
|
27 * Initialize NVS |
|
28 */ |
|
29 ret = nvs_flash_init(); |
|
30 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
|
31 ESP_ERROR_CHECK(nvs_flash_erase()); |
|
32 ret = nvs_flash_init(); |
|
33 } |
|
34 ESP_ERROR_CHECK(ret); |
|
35 |
|
36 uint8_t mac_addr[8] = {0}; |
|
37 // Set the configured hostname for the dhcp client. |
|
38 esp_efuse_mac_get_default(mac_addr); |
|
39 sprintf(hostname, "dcf77tx-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); |
|
40 |
|
41 esp_log_level_set("wifi", ESP_LOG_ERROR); |
|
42 xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi); |
|
43 |
|
44 int wait = 150; |
|
45 while (wait) { |
|
46 vTaskDelay(100 / portTICK_PERIOD_MS); |
|
47 if (ready_WiFi()) { |
|
48 ESP_LOGI(TAG, "Online in %.1f seconds", (150 - wait) / 10.0); |
|
49 wait = 0; |
|
50 } else { |
|
51 wait--; |
|
52 if (wait < 1) |
|
53 ESP_LOGE(TAG, "Timeout network connection"); |
|
54 } |
|
55 } |
|
56 |
|
57 |
|
58 time_t now; |
|
59 struct tm timeinfo; |
|
60 char strftime_buf[64]; |
|
61 time(&now); |
|
62 // Set timezone to Central Europe Time and print local time |
|
63 setenv("TZ", "CET-01CEST-02,M3.4.0,M10.4.0", 1); |
|
64 tzset(); |
|
65 localtime_r(&now, &timeinfo); |
|
66 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); |
|
67 ESP_LOGI(TAG, "The current date/time in Amsterdam is: %s", strftime_buf); |
|
68 |
|
69 /* |
|
70 * Main application loop |
|
71 */ |
|
72 while (1) { |
|
73 switch (Main_Loop) { |
|
74 case ML_INIT: if (ready_WiFi() ) { |
|
75 Main_Loop = ML_SYNC; |
|
76 ESP_LOGI(TAG, "ML_INIT -> ML_SYNC"); |
|
77 } else { |
|
78 Main_Loop = ML_CONNECT; |
|
79 ESP_LOGI(TAG, "ML_INIT -> ML_CONNECT"); |
|
80 } |
|
81 break; |
|
82 case ML_CONNECT: break; |
|
83 case ML_SYNC: break; |
|
84 case ML_RUN: break; |
|
85 } |
|
86 |
|
87 vTaskDelay(10 / portTICK_PERIOD_MS); |
|
88 } |
|
89 } |
|
90 |