main/dcf77tx.c

changeset 1
86b275481021
parent 0
913eb9ca40b1
child 3
849ca14d4a2f
equal deleted inserted replaced
0:913eb9ca40b1 1:86b275481021
5 #include "dcf77tx.h" 5 #include "dcf77tx.h"
6 6
7 static const char *TAG = "dcf77tx"; 7 static const char *TAG = "dcf77tx";
8 8
9 static TaskHandle_t xTaskWifi = NULL; 9 static TaskHandle_t xTaskWifi = NULL;
10 static TaskHandle_t xTaskDCF = NULL;
10 char hostname[32]; 11 char hostname[32];
11 int Main_Loop = ML_INIT; 12 int Main_Loop = ML_INIT;
12 13
13 14
14 extern SemaphoreHandle_t xSemaphoreWiFi; 15 extern SemaphoreHandle_t xSemaphoreWiFi;
15 extern WIFI_State *wifi_state; ///< WiFi state 16 extern WIFI_State *wifi_state; ///< WiFi state
16 extern bool _wifi_ScanDone; 17 extern bool _wifi_ScanDone;
17 extern int8_t _wifi_RSSI; 18 extern int8_t _wifi_RSSI;
19 extern SemaphoreHandle_t xSemaphoreDCF;
20 extern DCF_State *dcf_state; ///< DCF77 state
21 extern bool System_TimeOk;
18 22
19 23
20 void app_main(void) 24 void app_main(void)
21 { 25 {
22 esp_err_t ret; 26 esp_err_t ret;
36 uint8_t mac_addr[8] = {0}; 40 uint8_t mac_addr[8] = {0};
37 // Set the configured hostname for the dhcp client. 41 // Set the configured hostname for the dhcp client.
38 esp_efuse_mac_get_default(mac_addr); 42 esp_efuse_mac_get_default(mac_addr);
39 sprintf(hostname, "dcf77tx-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]); 43 sprintf(hostname, "dcf77tx-%02x%02x%02x", mac_addr[3], mac_addr[4], mac_addr[5]);
40 44
45 setenv("TZ", "CET-01CEST-02,M3.4.0,M10.4.0", 1);
46 tzset();
47
41 esp_log_level_set("wifi", ESP_LOG_ERROR); 48 esp_log_level_set("wifi", ESP_LOG_ERROR);
42 xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi); 49 xTaskCreate(&task_wifi, "task_wifi", 4096, NULL, 3, &xTaskWifi);
50 xTaskCreate(&task_DCF, "task_DCF", 4096, NULL, 4, &xTaskDCF);
43 51
44 int wait = 150; 52 int wait = 150;
45 while (wait) { 53 while (wait) {
46 vTaskDelay(100 / portTICK_PERIOD_MS); 54 vTaskDelay(100 / portTICK_PERIOD_MS);
47 if (ready_WiFi()) { 55 if (ready_WiFi()) {
48 ESP_LOGI(TAG, "Online in %.1f seconds", (150 - wait) / 10.0); 56 ESP_LOGI(TAG, "Online in %.1f seconds", (150 - wait) / 10.0);
49 wait = 0; 57 wait = 0;
50 } else { 58 } else {
51 wait--; 59 wait--;
52 if (wait < 1) 60 if (wait < 1)
53 ESP_LOGE(TAG, "Timeout network connection"); 61 ESP_LOGE(TAG, "Timeout network connection");
54 } 62 }
55 } 63 }
56
57 64
58 time_t now; 65 time_t now;
59 struct tm timeinfo; 66 struct tm timeinfo;
60 char strftime_buf[64]; 67 char strftime_buf[64];
61 time(&now); 68 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); 69 localtime_r(&now, &timeinfo);
66 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); 70 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
67 ESP_LOGI(TAG, "The current date/time in Amsterdam is: %s", strftime_buf); 71 ESP_LOGI(TAG, "The current date/time in Amsterdam is: %s", strftime_buf);
68 72
69 /* 73 /*
72 while (1) { 76 while (1) {
73 switch (Main_Loop) { 77 switch (Main_Loop) {
74 case ML_INIT: if (ready_WiFi() ) { 78 case ML_INIT: if (ready_WiFi() ) {
75 Main_Loop = ML_SYNC; 79 Main_Loop = ML_SYNC;
76 ESP_LOGI(TAG, "ML_INIT -> ML_SYNC"); 80 ESP_LOGI(TAG, "ML_INIT -> ML_SYNC");
81 gpio_set_level(CONFIG_LED1_PIN, 1);
77 } else { 82 } else {
78 Main_Loop = ML_CONNECT; 83 Main_Loop = ML_CONNECT;
79 ESP_LOGI(TAG, "ML_INIT -> ML_CONNECT"); 84 ESP_LOGI(TAG, "ML_INIT -> ML_CONNECT");
80 } 85 }
81 break; 86 break;
82 case ML_CONNECT: break; 87 case ML_CONNECT: if (System_TimeOk) {
83 case ML_SYNC: break; 88 Main_Loop = ML_SYNC;
84 case ML_RUN: break; 89 ESP_LOGI(TAG, "ML_CONNECT -> ML_SYNC");
90 }
91 break;
92 case ML_SYNC: if (System_TimeOk == false) {
93 Main_Loop = ML_CONNECT;
94 ESP_LOGI(TAG, "ML_SYNC -> ML_CONNECT");
95 }
96 time(&now);
97 localtime_r(&now, &timeinfo);
98 if (timeinfo.tm_sec == 0) {
99 Main_Loop = ML_RUN;
100 ESP_LOGI(TAG, "ML_SYNC -> ML_RUN");
101 request_DCF(true);
102 gpio_set_level(CONFIG_LED2_PIN, 1);
103 gpio_set_level(CONFIG_LED1_PIN, 0);
104 }
105 break;
106 case ML_RUN: if (System_TimeOk == false) {
107 Main_Loop = ML_CONNECT;
108 ESP_LOGI(TAG, "ML_RUN -> ML_CONNECT");
109 request_DCF(false);
110 gpio_set_level(CONFIG_LED2_PIN, 0);
111 }
112 break;
85 } 113 }
86 114
87 vTaskDelay(10 / portTICK_PERIOD_MS); 115 vTaskDelay(10 / portTICK_PERIOD_MS);
88 } 116 }
89 } 117 }

mercurial