|
10 | 10 | #include "esp_event.h" |
11 | 11 | #include "esp_log.h" |
12 | 12 | #include "nvs_flash.h" |
| 13 | +#include "driver/gpio.h" |
13 | 14 |
|
14 | 15 | #include "twifi.h" |
15 | 16 | #include "tbt.h" |
16 | 17 | #include "tmqtt.h" |
| 18 | +#include "tb2mconfig.h" |
17 | 19 | #include "ble2mqtt/ble2mqtt.h" |
18 | 20 | #ifndef DISABLETDEBUG |
19 | 21 | #include "tdebug.h" |
@@ -45,33 +47,56 @@ void app_main(void) |
45 | 47 | ESP_ERROR_CHECK(nvs_flash_erase()); |
46 | 48 | ret = nvs_flash_init(); |
47 | 49 | } |
| 50 | + |
48 | 51 | ESP_ERROR_CHECK(ret); |
49 | 52 | ESP_ERROR_CHECK(esp_event_loop_create_default()); |
50 | 53 |
|
51 | | - ble2mqtt_t *ble2mqtt = ble2mqtt_create(); |
52 | | - if (!ble2mqtt) |
| 54 | + ESP_ERROR_CHECK(gpio_set_direction(GPIO_NUM_4, GPIO_MODE_INPUT)); |
| 55 | + int reset_config = gpio_get_level(GPIO_NUM_4); |
| 56 | + |
| 57 | + b2mconfig_t *b2mconfig = b2mconfig_create(); |
| 58 | + if (b2mconfig == NULL) |
53 | 59 | { |
54 | 60 | ESP_LOGE(TAG, "malloc error!"); |
55 | 61 | while (1) |
56 | 62 | ; |
57 | 63 | } |
58 | | - if (ble2mqtt_init(ble2mqtt) == false) |
| 64 | + else |
59 | 65 | { |
60 | | - ESP_LOGE(TAG, "ble2mqtt_init error!"); |
61 | | - while (1) |
62 | | - ; |
| 66 | + b2mconfig_load(b2mconfig); |
63 | 67 | } |
64 | 68 |
|
| 69 | + if (reset_config == 1 || b2mconfig->not_initialized) |
| 70 | + { |
| 71 | + xTaskCreate(vTaskB2MConfig, "task_b2mconfig", (2048 * 6), (void *)b2mconfig, 8, NULL); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + ble2mqtt_t *ble2mqtt = ble2mqtt_create(); |
| 76 | + if (!ble2mqtt) |
| 77 | + { |
| 78 | + ESP_LOGE(TAG, "malloc error!"); |
| 79 | + while (1) |
| 80 | + ; |
| 81 | + } |
| 82 | + if (ble2mqtt_init(ble2mqtt, b2mconfig) == false) |
| 83 | + { |
| 84 | + ESP_LOGE(TAG, "ble2mqtt_init error!"); |
| 85 | + while (1) |
| 86 | + ; |
| 87 | + } |
| 88 | + |
65 | 89 | #ifndef DISABLETWIFI |
66 | | - xTaskCreate(vTaskWifi, "task_wifi", (2048 * 6), (void *)ble2mqtt, 8, NULL); |
| 90 | + xTaskCreate(vTaskWifi, "task_wifi", (2048 * 6), (void *)ble2mqtt, 8, NULL); |
67 | 91 | #endif |
68 | 92 | #ifndef DISABLETMQTT |
69 | | - xTaskCreate(vTaskMqtt, "task_mqtt", (2048 * 6), (void *)ble2mqtt, 10, NULL); |
| 93 | + xTaskCreate(vTaskMqtt, "task_mqtt", (2048 * 6), (void *)ble2mqtt, 10, NULL); |
70 | 94 | #endif |
71 | 95 | #ifndef DISABLETBT |
72 | | - xTaskCreate(vTaskBt, "task_bt", (2048 * 6), (void *)ble2mqtt, 12, NULL); |
| 96 | + xTaskCreate(vTaskBt, "task_bt", (2048 * 6), (void *)ble2mqtt, 12, NULL); |
73 | 97 | #endif |
74 | 98 | #ifndef DISABLETDEBUG |
75 | | - xTaskCreate(vTaskDebug, "task_debug", 2048, (void *)ble2mqtt, 20, NULL); |
| 99 | + xTaskCreate(vTaskDebug, "task_debug", 2048, (void *)ble2mqtt, 20, NULL); |
76 | 100 | #endif |
| 101 | + } |
77 | 102 | } |
0 commit comments