wifi component enabling for aqi
authorjweigele <jweigele@local>
Sat, 15 Jul 2023 05:47:27 +0000 (22:47 -0700)
committerjweigele <jweigele@local>
Sat, 15 Jul 2023 05:47:27 +0000 (22:47 -0700)
 * uses esp32-wifi-manager as a component
 * tries to connect through mqtt and publish to a topic
 * few more Kconfig items to match various options
 * probably not very robust yet, very little error handling

.gitmodules
aqi/CMakeLists.txt
aqi/components/esp32-wifi-manager [new submodule]
aqi/dependencies.lock
aqi/main/Kconfig
aqi/main/aqi.c
aqi/main/aqi.h
aqi/main/idf_component.yml
aqi/partitions.csv
aqi/sdkconfig
aqi/sdkconfig.old

index 1b903210a3fa6de59b430621131682e9fbddaffb..0f9546466376d312bddd62ac5797a4520ae01856 100644 (file)
@@ -11,3 +11,6 @@
 [submodule "aqi/components/esp32-aqi-calc"]
        path = aqi/components/esp32-aqi-calc
        url = ssh://git@git.hexthepla.net:2222/esp32-aqi-calc
+[submodule "aqi/components/esp32-wifi-manager"]
+       path = aqi/components/esp32-wifi-manager
+       url = https://github.com/huardti/esp32-wifi-manager.git
index 0a4455b3d5a81585ca95670f25094f934fdbc9e2..7a069db4670525ac1d3dc4bad8973483a86ede1b 100644 (file)
@@ -1,7 +1,11 @@
 # The following lines of boilerplate have to be in your project's CMakeLists
 # in this exact order for cmake to work correctly
 cmake_minimum_required(VERSION 3.16)
-set(EXTRA_COMPONENT_DIRS
+if (CONFIG_WIFI_ENABLED)
+set(EXTRA_COMPONENT_DIRS components/esp32-wifi-manager
     )
+else()
+set(EXTRA_COMPONENT_DIRS )
+endif()
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
-project(color_light_bulb)
+project(aqi)
diff --git a/aqi/components/esp32-wifi-manager b/aqi/components/esp32-wifi-manager
new file mode 160000 (submodule)
index 0000000..2783195
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 278319504537e8d0b6485284eeb35c4df2b9ad68
index d83555384e957d0e486c6367c14f15ba236a486f..a2d7b3ec4cd1de9759b7704da2cac1a355c6bd8b 100644 (file)
@@ -1,21 +1,27 @@
 dependencies:
   espressif/esp-zboss-lib:
-    component_hash: 550d9833182407e2904b347a8d21202a1ff0209817793738095fa9b99d209738
+    component_hash: ba11f1139481e9d0aaa08e9d621eb04113052fad28c8316744a5cb503d79fe6a
     source:
       service_url: https://api.components.espressif.com/
       type: service
-    version: 0.5.0
+    version: 0.5.1
   espressif/esp-zigbee-lib:
-    component_hash: 59855c0c3e5bbbc39a1d87571cbf11b2249d55a9c3e7dc84ec8c8f4ed2049849
+    component_hash: 5031e107e2da6a65d30770a0b1a130860fe353021549e65e3a788ea8a53933ff
     source:
       service_url: https://api.components.espressif.com/
       type: service
-    version: 0.7.0
+    version: 0.7.1
+  espressif/mdns:
+    component_hash: 53b22a3b01d0b61180369a5dab00e271f1e725164b6affbc73af85e80b043658
+    source:
+      service_url: https://api.components.espressif.com/
+      type: service
+    version: 1.2.0
   idf:
     component_hash: null
     source:
       type: idf
     version: 5.1.0
-manifest_hash: 45912e4d9cd43099df4d25e2583dff3e2d28d61278283bac4618062c48e66228
+manifest_hash: ff6bc35ecb146a6568c60d0082c3b75fc19df0e714da158f280741aead576f87
 target: esp32c6
 version: 1.0.0
index 937a28be50f5b368112fe468ac2236d5756634a6..d030f8c54f8ef79e5af718e1dc83ec436ae42781 100644 (file)
@@ -6,9 +6,29 @@ config TEMP_ENABLED
     help
       This gets passed and used for includes later
 
+config ZIG_ENABLED
+    bool "Are we using zigbee pairing and reporting"
+    default n
+    help
+      Gets passed and used later for includes
+config WIFI_ENABLED
+    bool "Are we using wifi association and MQTT"
+    default n
+    help
+      Gets passed and used later for includes
 config LED_ENABLED
     bool "Are we using LCD functions for display?"
     default n
     help
       Gets passed and used later for includes
+config BROKER_URL
+    string "Set this to connect mqtt and report back data"
+    default ""
+    help
+      Gets used downstream 
+config LOCATION
+    string "Set this to whatever you'd like reported as location"
+    default "ESP32 babyyy"
+    help
+      Gets used when sending off sensor data
 endmenu
index a6de8597b2055b609000f9841467903376c6aff8..8c9f9925ec3135aa86e4781ac481674317b2ef6a 100644 (file)
@@ -18,7 +18,6 @@
 #include "freertos/task.h"
 #include "ha/esp_zigbee_ha_standard.h"
 #include "aqi.h"
-//#include "esp_sleep.h"
 
 /**
  * @note Make sure set idf.py menuconfig in zigbee component as zigbee end device!
 #error Define ZB_ED_ROLE in idf.py menuconfig to compile light (End Device) source code.
 #endif
 
-
-typedef struct zdo_info_ctx_s {
-    uint8_t endpoint;
-    esp_zb_ieee_addr_t long_addr;
-} zdo_info_user_ctx_t;
-
-
 typedef struct aqi_data_s {
     float pm10;
     float pm25;
@@ -53,7 +45,13 @@ static OW ow;
 static tm1637_led_t* led;
 #endif
 
+#ifdef CONFIG_ZIG_ENABLED
 static bool zig_connected = false;
+#endif
+#ifdef CONFIG_WIFI_ENABLED
+static bool wifi_connected =  false;
+static bool mqtt_connected = false;
+#endif
 static aqi_data_t cur_pm = {
     .pm10 = 0,
     .pm25 = 0
@@ -62,17 +60,18 @@ static aqi_data_t cur_pm = {
 static aqi_data_t pmbuffer[PMBUFFERSIZE];
 static int pmbufferindex = 0;
 
-/********************* Define functions **************************/
+
+#ifdef CONFIG_ZIG_ENABLED
+typedef struct zdo_info_ctx_s {
+    uint8_t endpoint;
+    esp_zb_ieee_addr_t long_addr;
+} zdo_info_user_ctx_t;
+
 static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
 {
     ESP_ERROR_CHECK(esp_zb_bdb_start_top_level_commissioning(mode_mask));
 }
-
-void attr_cb(uint8_t status, uint8_t endpoint, uint16_t cluster_id, uint16_t attr_id, void *new_value)
-{
-    /* Implement some actions if needed when other cluster changed */
-    ESP_LOGI(TAG, "cluster:0x%x, attribute:0x%x changed ", cluster_id, attr_id);
-}
+#endif
 
 #ifdef CONFIG_LED_ENABLED
 void init_led(){
@@ -245,7 +244,37 @@ void init_uart(void) {
     uart_set_pin(UART_NUM_1, UART_TX_GPIO, UART_RX_GPIO, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
 }
 
-static void send_report(int report_cluster, int report_attribute){
+
+#ifdef CONFIG_WIFI_ENABLED
+static void send_report_wifi(uint16_t pm10, uint16_t pm25, float aqi, uint16_t temperature){
+    ESP_LOGI(TAG, "send_report_wifi was called!");
+    cJSON *root;
+    root = cJSON_CreateObject();
+    cJSON_AddStringToObject(root, "location", CONFIG_LOCATION);
+    cJSON_AddNumberToObject(root, "pm10", ((float)pm10/100));
+    cJSON_AddNumberToObject(root, "pm25", ((float)pm25/100));
+    cJSON_AddNumberToObject(root, "aqi", aqi);
+#ifdef CONFIG_TEMP_ENABLED
+    cJSON_AddNumberToObject(root, "temperature", ((float)temperature/100));
+#endif
+
+    //const char *my_json_string = cJSON_Print(root);
+    char *mqtt_string = cJSON_Print(root);
+    ESP_LOGI(TAG, "sending mqtt_string:\n%s",mqtt_string);
+    if (wifi_connected == true && mqtt_connected == true){
+        int msg_id = esp_mqtt_client_publish(mqtt_client, "esp32/sensor_info", mqtt_string, 0, 1, 0);
+        ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
+    } else {
+        ESP_LOGW(TAG, "well, not quite yet (not connected)");
+    }
+    
+    cJSON_Delete(root);    
+}
+
+#endif
+
+#ifdef CONFIG_ZIG_ENABLED
+static void send_report_zig(int report_cluster, int report_attribute){
     ESP_LOGI(TAG, "sending report here");
     esp_zb_zcl_report_attr_cmd_t des;
 
@@ -267,6 +296,7 @@ static void send_report(int report_cluster, int report_attribute){
     ESP_LOGI(TAG, "sent report");
 
 }
+#endif
 
 bool verify_checksum(uint8_t* data){
     int total = 0;
@@ -335,90 +365,90 @@ static void monitoring_task(void* discard)
         ESP_LOGI(TAG, "free heap: %"PRIu32, esp_get_free_heap_size());
         //temp_list[0] += 100;
 
-       if (zig_connected == true){
-
-            // aqi fetch/calculation/report
-            ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_1, (size_t*)&length));
-            const int rxBytes = uart_read_bytes(UART_NUM_1, data, length, 100);
-            if (rxBytes > 0) {
-                data[rxBytes] = 0;
-                //ESP_LOGI(TAG, "Read %d bytes: '%s'", rxBytes, data);
-                //ESP_LOG_BUFFER_HEXDUMP(TAG, data, rxBytes, ESP_LOG_INFO);
-                aqi_data_t cur_avg = get_average_pm25(data, rxBytes);
-                ESP_LOGI(TAG, "average from last 10 seconds pm10: %f", cur_avg.pm10);                
-                ESP_LOGI(TAG, "average from last 10 seconds pm25: %f", cur_avg.pm25);
-                
-                ESP_LOGI(TAG, "setting attrib value and sending, I guess");
-                cur_pm = new_pm_average(cur_avg);
+
+
+        // aqi fetch/calculation/report
+        ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_1, (size_t*)&length));
+        const int rxBytes = uart_read_bytes(UART_NUM_1, data, length, 100);
+        if (rxBytes > 0) {
+            data[rxBytes] = 0;
+            //ESP_LOGI(TAG, "Read %d bytes: '%s'", rxBytes, data);
+            //ESP_LOG_BUFFER_HEXDUMP(TAG, data, rxBytes, ESP_LOG_INFO);
+            aqi_data_t cur_avg = get_average_pm25(data, rxBytes);
+            ESP_LOGI(TAG, "average from last 10 seconds pm10: %f", cur_avg.pm10);                
+            ESP_LOGI(TAG, "average from last 10 seconds pm25: %f", cur_avg.pm25);
+            
+            ESP_LOGI(TAG, "setting attrib value and sending, I guess");
+            cur_pm = new_pm_average(cur_avg);
+#ifdef CONFIG_ZIG_ENABLED            
+
+            if (zig_connected){
                 uint16_t set_pm10 = (uint16_t)cur_pm.pm10;
-                uint16_t set_pm25 = (uint16_t)cur_pm.pm25;                
+                uint16_t set_pm25 = (uint16_t)cur_pm.pm25;
+
                 esp_zb_zcl_set_attribute_val(HA_ESP_ENDPOINT, PM25CLUSTER, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
                                 PM10MEASURED, &set_pm10, false);
                 esp_zb_zcl_set_attribute_val(HA_ESP_ENDPOINT, PM25CLUSTER, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
                                 PM25MEASURED, &set_pm25, false);
+                send_report_zig(PM10CLUSTER, PM10MEASURED);
+                send_report_zig(PM25CLUSTER, PM25MEASURED);
+            }
+#endif
+        } else {
+            ESP_LOGW(TAG, "Nothing seen from uart");
+        }
 
-                send_report(PM10CLUSTER, PM10MEASURED);
-                send_report(PM25CLUSTER, PM25MEASURED);
+        float cur_aqi_pm10 = aqi_calc_pm10(cur_pm.pm10);
+        float cur_aqi_pm25 = aqi_calc_pm25(cur_pm.pm25);
+        uint16_t set_aqi;
+        if (cur_aqi_pm25 > cur_aqi_pm10){
+            ESP_LOGI(TAG, "pm25 > pm10, using %f > %f", cur_aqi_pm25, cur_aqi_pm10);
+            set_aqi = round(cur_aqi_pm25);
+        } else {
+            ESP_LOGI(TAG, "pm10 > pm25, using %f > %f", cur_aqi_pm10, cur_aqi_pm25);
+            set_aqi = round(cur_aqi_pm10);
+        }
 #ifdef CONFIG_LED_ENABLED
-                //uint16_t led_value = (uint16_t)(cur_pm.pm25/100);
-                float cur_aqi_pm10 = aqi_calc_pm10(cur_pm.pm10);
-                float cur_aqi_pm25 = aqi_calc_pm25(cur_pm.pm25);
-                uint16_t set_aqi;
-                if (cur_aqi_pm25 > cur_aqi_pm10){
-                    ESP_LOGI(TAG, "pm25 > pm10, using %f > %f", cur_aqi_pm25, cur_aqi_pm10);
-                    set_aqi = round(cur_aqi_pm25);
-                } else {
-                    ESP_LOGI(TAG, "pm10 > pm25, using %f > %f", cur_aqi_pm10, cur_aqi_pm25);
-                    set_aqi = round(cur_aqi_pm10);
-                }
-                ESP_LOGI(TAG, "setting led value to %d", set_aqi);
-                tm1637_set_number(led, set_aqi);
-
-#endif                
-
-            } else {
-                ESP_LOGI(TAG, "Nothing seen from uart");
-            }
+        ESP_LOGI(TAG, "setting led value to %d", set_aqi);
+        tm1637_set_number(led, set_aqi);
 
+#endif
+    
+        short temp_avg = 0; 
 #ifdef CONFIG_TEMP_ENABLED
-            // Temperature fetch/calculation/report
-            if (tempsetup){
-                short temp_avg = new_temp_average(get_temp());
-                ESP_LOGI(TAG, "current: %d", temp_avg);
-
-                ESP_LOGI(TAG, "setting attrib value and sending, I guess");
-                esp_zb_zcl_set_attribute_val(HA_ESP_ENDPOINT, ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
-                                ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID, &temp_avg, false);
-                send_report(ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID);
-
-            } else {
-                    ESP_LOGI(TAG, "temp not setup, skipping update");
-            }
+        // Temperature fetch/calculation/report
+        temp_avg = new_temp_average(get_temp());
+        ESP_LOGI(TAG, "current: %d", temp_avg);
+
+#ifdef CONFIG_ZIG_ENABLED
+        if (zig_connected){
+            ESP_LOGI(TAG, "setting attrib value and sending, I guess");
+            esp_zb_zcl_set_attribute_val(HA_ESP_ENDPOINT, ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE,
+                            ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID, &temp_avg, false);
+            send_report_zig(ESP_ZB_ZCL_CLUSTER_ID_TEMP_MEASUREMENT, ESP_ZB_ZCL_ATTR_TEMP_MEASUREMENT_VALUE_ID);
+        }
+
+#endif // ZIG
+
+
+#endif // TEMP
+
 
+#ifdef CONFIG_WIFI_ENABLED
+        // one BIG report for wifi
+        send_report_wifi(cur_pm.pm10, cur_pm.pm25, cur_aqi_pm25, temp_avg);
 #endif
-            // loop around and sleep
-            //
-            // reg sleep for 1 second to clear out backlog, then light sleep
-            //ESP_LOGI(TAG, "light sleep starts in a second");
-            vTaskDelay( pdMS_TO_TICKS(10000) );
-            //ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(9000000));
-            //ESP_ERROR_CHECK(esp_light_sleep_start());
-
-            // now we've woken up I guess?
-            ESP_LOGI(TAG, "I have worken up after sleep, looping around");
-               
-       } else {
-               ESP_LOGI(TAG, "zig not connected, skipping temp report (and staying awake)");
-               vTaskDelay( pdMS_TO_TICKS(10000) );
-       }
+            
+        // uart buffer
+        vTaskDelay( pdMS_TO_TICKS(10000) );
 
     }
-    // uart buffer
     free(data);
-
     //esp_zb_scheduler_alarm((esp_zb_callback_t)monitoring_task, parm, 10000);
 }
 
+
+#ifdef CONFIG_ZIG_ENABLED
 void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
 {
     uint32_t *p_sg_p       = signal_struct->p_app_signal;
@@ -507,7 +537,6 @@ static void esp_zb_task(void *pvParameters)
     esp_zb_cluster_list_t *esp_zb_cluster_list = esp_zb_zcl_cluster_list_create();
     esp_zb_cluster_list_add_basic_cluster(esp_zb_cluster_list, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
     esp_zb_cluster_list_add_identify_cluster(esp_zb_cluster_list, esp_zb_identify_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
-
 #ifdef CONFIG_TEMP_ENABLED
     short min_temp = -5500;
     short max_temp = 12500;
@@ -536,13 +565,126 @@ static void esp_zb_task(void *pvParameters)
     esp_zb_main_loop_iteration();
 
 }
+#endif
 
-void app_main(void)
+#ifdef CONFIG_WIFI_ENABLED
+
+static void log_error_if_nonzero(const char *message, int error_code)
 {
-    esp_zb_platform_config_t config = {
-        .radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
-        .host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
+    if (error_code != 0) {
+        ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
+    }
+}
+
+/*
+ * @brief Event handler registered to receive MQTT events
+ *
+ *  This function is called by the MQTT client event loop.
+ *
+ * @param handler_args user data registered to the event.
+ * @param base Event base for the handler(always MQTT Base in this example).
+ * @param event_id The id for the received event.
+ * @param event_data The data for the event, esp_mqtt_event_handle_t.
+ */
+static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
+{
+    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id);
+    esp_mqtt_event_handle_t event = event_data;
+    switch ((esp_mqtt_event_id_t)event_id) {
+    case MQTT_EVENT_CONNECTED:
+        mqtt_connected = true;
+        mqtt_client = event->client;
+        ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
+        break;
+    case MQTT_EVENT_DISCONNECTED:
+        ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
+        mqtt_connected = false;
+        break;
+
+    case MQTT_EVENT_SUBSCRIBED:
+        ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
+        break;
+    case MQTT_EVENT_UNSUBSCRIBED:
+        ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
+        break;
+    case MQTT_EVENT_PUBLISHED:
+        ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
+        break;
+    case MQTT_EVENT_DATA:
+        ESP_LOGI(TAG, "MQTT_EVENT_DATA");
+        printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
+        printf("DATA=%.*s\r\n", event->data_len, event->data);
+        break;
+    case MQTT_EVENT_ERROR:
+        mqtt_connected = false;
+        ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
+        if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
+            log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err);
+            log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err);
+            log_error_if_nonzero("captured as transport's socket errno",  event->error_handle->esp_transport_sock_errno);
+            ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
+
+        }
+        break;
+    default:
+        ESP_LOGI(TAG, "Other event id:%d", event->event_id);
+        break;
+    }
+}
+
+void connect_mqtt(){
+    esp_mqtt_client_config_t mqtt_cfg = {
+        .broker = {
+            .address.uri  = CONFIG_BROKER_URL,
+            .verification.certificate = NULL,
+            //.verification.use_global_ca_store = true,
+            //.verification.crt_bundle_attach = esp_crt_bundle_attach,
+        },
+        //.cacert_buf = ca_cert,
+        //.cacert_bytes = strlen(ca_cert)+1,
     };
+    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
+    /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
+    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
+    esp_mqtt_client_start(client);
+}
+
+// wifi connection nuked callbak
+// wifi connection ok callback
+void wifi_connection_bad(void* pvParameter){
+    //ip_event_got_ip_t* param = (ip_event_got_ip_t*)pvParameter;
+    ESP_LOGW(TAG, "wifi connection got dropped! just gonna set stuff down and hope for the best");
+    wifi_connected = false;
+    mqtt_connected = false;
+}
+
+
+// wifi connection ok callback
+void wifi_connection_ok(void* pvParameter){
+    wifi_connected = true;
+    ip_event_got_ip_t* param = (ip_event_got_ip_t*)pvParameter;
+
+    /* transform IP to human readable string */
+    char str_ip[16];
+    esp_ip4addr_ntoa(&param->ip_info.ip, str_ip, IP4ADDR_STRLEN_MAX);
+
+    ESP_LOGI(TAG, "I have a connection and my IP is %s!", str_ip);
+    esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
+    esp_netif_sntp_init(&config);
+    if (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(10000)) != ESP_OK) {
+        printf("Failed to update system time within 10s timeout");
+    }    
+    ESP_LOGI(TAG, "NTP sync complete");
+
+    //xQueueSend(tx_task_queue, &tx_action, portMAX_DELAY);
+    connect_mqtt();
+}
+
+#endif
+
+
+void app_main(void)
+{
     ESP_ERROR_CHECK(nvs_flash_init());
     init_uart();
     init_pmbuffer();
@@ -550,16 +692,34 @@ void app_main(void)
 #ifdef CONFIG_TEMP_ENABLED
     init_tempbuffer();
 #endif
-    /* load Zigbee light_bulb platform config to initialization */
-    ESP_ERROR_CHECK(esp_zb_platform_config(&config));
+
 #ifdef CONFIG_LED_ENABLED
     init_led();
     tm1637_set_number(led, set_val);
     //i2c_init();
     //runit();
 #endif
+
+#ifdef CONFIG_ZIG_ENABLED
+    esp_zb_platform_config_t config = {
+        .radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
+        .host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
+    };
+
+    /* load Zigbee light_bulb platform config to initialization */
+    ESP_ERROR_CHECK(esp_zb_platform_config(&config));
     /* hardware related and device init */
     //light_driver_init(LIGHT_DEFAULT_OFF);
     xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
+#endif
+#ifdef CONFIG_WIFI_ENABLED
+//    ESP_ERROR_CHECK(esp_tls_set_global_ca_store(ca_cert, strlen((char*)ca_cert)));
+    /* start the wifi manager */
+    wifi_manager_start();
+
+    wifi_manager_set_callback(WM_EVENT_STA_GOT_IP, &wifi_connection_ok);    
+    wifi_manager_set_callback(WM_EVENT_STA_DISCONNECTED, &wifi_connection_bad);
+#endif
     xTaskCreate(monitoring_task, "monitoring_task", 4096, NULL, 1, NULL);    
 }
+
index de029ffa98646500426c0c5e67db0f2197eebe39..8f611257afff8680941b51b6bd6729a8a36cd929 100644 (file)
  * CONDITIONS OF ANY KIND, either express or implied.
  */
 
+#ifdef CONFIG_ZIG_ENABLED
 #include "esp_zigbee_core.h"
+#include "ha/esp_zigbee_ha_standard.h"
+#endif
+
+#ifdef CONFIG_WIFI_ENABLED
+#include <esp_wifi.h>
+#include "wifi_manager.h"
+#include <lwip/ip4_addr.h>
+#include <esp_netif.h>
+#include <esp_tls.h>
+#include "mqtt_client.h"
+#include "esp_crt_bundle.h"
+#include "esp_netif_sntp.h"
+#include "cJSON.h"
+static esp_mqtt_client_handle_t mqtt_client;
+#endif
+
 #include "esp_system.h"
 
 //#include "light_driver.h"
@@ -80,6 +97,7 @@ static const int RX_BUF_SIZE = 1024;
 
 
 
+#ifdef CONFIG_ZIG_ENABLED
 /* Zigbee configuration */
 #define INSTALLCODE_POLICY_ENABLE       false    /* enable the install code policy for security */
 #define ED_AGING_TIMEOUT                ESP_ZB_ED_AGING_TIMEOUT_64MIN
@@ -108,3 +126,4 @@ static const int RX_BUF_SIZE = 1024;
     {                                                           \
         .host_connection_mode = HOST_CONNECTION_MODE_NONE,      \
     }
+#endif
index 1d60ab3792ffd4e1d2dc5bc977f2a77837867574..4aab1203802a12725a40d6c1c690a32fe8848548 100644 (file)
@@ -2,6 +2,7 @@
 dependencies:
   espressif/esp-zboss-lib: "~0.5.0"
   espressif/esp-zigbee-lib: "~0.7.0"
+  espressif/mdns: "^1.1.0"
   ## Required IDF version
   idf:
     version: ">=5.0.0"
index d8dcfcdf6b50b3e6b62e908c7d7e813e58acd3e1..2df59e370a7dce5c422c45d85c49ffdfae2d58d8 100644 (file)
@@ -2,6 +2,6 @@
 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
 nvs,        data, nvs,      0x9000,  0x6000,
 phy_init,   data, phy,      0xf000,  0x1000,
-factory,    app,  factory,  0x10000, 550K,
-zb_storage, data, fat,      0x9a000, 16K,
-zb_fct,     data, fat,      0x9e000, 1K,
+factory,    app,  factory,  0x10000, 2048K,
+zb_storage, data, fat,      0x210000, 16K,
+zb_fct,     data, fat,      0x214000, 1K,
index c9bd6713a3788a9737ab3c9ed6ab0dc970dc0299..ca28e5b98f7d9b2c5d9ced3d7fb2a87985ab95ce 100644 (file)
@@ -542,13 +542,13 @@ CONFIG_SPI_SLAVE_ISR_IN_IRAM=y
 #
 # UART Configuration
 #
-CONFIG_UART_ISR_IN_IRAM=y
+# CONFIG_UART_ISR_IN_IRAM is not set
 # end of UART Configuration
 
 #
 # GPIO Configuration
 #
-CONFIG_GPIO_CTRL_FUNC_IN_IRAM=y
+# CONFIG_GPIO_CTRL_FUNC_IN_IRAM is not set
 # end of GPIO Configuration
 
 #
@@ -631,7 +631,8 @@ CONFIG_ESP_TLS_USE_DS_PERIPHERAL=y
 # CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS is not set
 # CONFIG_ESP_TLS_SERVER is not set
 # CONFIG_ESP_TLS_PSK_VERIFICATION is not set
-# CONFIG_ESP_TLS_INSECURE is not set
+CONFIG_ESP_TLS_INSECURE=y
+CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y
 # end of ESP-TLS
 
 #
@@ -1355,7 +1356,8 @@ CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
 CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y
 # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set
 # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE is not set
-# CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE is not set
+CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE=y
+CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH="/home/jweigele/localDownloads/certbundle/bundle.pem"
 CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200
 # end of Certificate Bundle
 
@@ -1447,7 +1449,7 @@ CONFIG_MBEDTLS_ECP_DP_CURVE25519_ENABLED=y
 CONFIG_MBEDTLS_ECP_NIST_OPTIM=y
 # CONFIG_MBEDTLS_POLY1305_C is not set
 # CONFIG_MBEDTLS_CHACHA20_C is not set
-# CONFIG_MBEDTLS_HKDF_C is not set
+CONFIG_MBEDTLS_HKDF_C=y
 # CONFIG_MBEDTLS_THREADING_C is not set
 # CONFIG_MBEDTLS_SECURITY_RISKS is not set
 # end of mbedTLS
@@ -1673,7 +1675,11 @@ CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y
 # AQI Program Configuration
 #
 # CONFIG_TEMP_ENABLED is not set
+# CONFIG_ZIG_ENABLED is not set
+CONFIG_WIFI_ENABLED=y
 CONFIG_LED_ENABLED=y
+CONFIG_BROKER_URL="mqtts://esp32:remoteaccess@rabbitmq:8883"
+CONFIG_LOCATION="ESP32wifitest"
 # end of AQI Program Configuration
 
 #
@@ -1682,6 +1688,24 @@ CONFIG_LED_ENABLED=y
 CONFIG_DELAY_BLOCKING_TIME=100
 # end of ESP TM1637
 
+#
+# Wifi Manager Configuration
+#
+CONFIG_WIFI_MANAGER_TASK_PRIORITY=5
+CONFIG_WIFI_MANAGER_RETRY_TIMER=5000
+CONFIG_WIFI_MANAGER_MAX_RETRY_START_AP=3
+CONFIG_WIFI_MANAGER_SHUTDOWN_AP_TIMER=60000
+CONFIG_WEBAPP_LOCATION="/"
+CONFIG_DEFAULT_AP_SSID="aqimonitor"
+CONFIG_DEFAULT_AP_PASSWORD="aqimonitorpw"
+CONFIG_DEFAULT_AP_CHANNEL=1
+CONFIG_DEFAULT_AP_IP="10.10.0.1"
+CONFIG_DEFAULT_AP_GATEWAY="10.10.0.1"
+CONFIG_DEFAULT_AP_NETMASK="255.255.255.0"
+CONFIG_DEFAULT_AP_MAX_CONNECTIONS=4
+CONFIG_DEFAULT_AP_BEACON_INTERVAL=100
+# end of Wifi Manager Configuration
+
 #
 # Zigbee
 #
@@ -1692,6 +1716,33 @@ CONFIG_ZB_ZED=y
 CONFIG_ZB_RADIO_NATIVE=y
 # CONFIG_ZB_RADIO_MACSPLIT_UART is not set
 # end of Zigbee
+
+#
+# mDNS
+#
+CONFIG_MDNS_MAX_INTERFACES=3
+CONFIG_MDNS_MAX_SERVICES=10
+CONFIG_MDNS_TASK_PRIORITY=1
+CONFIG_MDNS_TASK_STACK_SIZE=4096
+# CONFIG_MDNS_TASK_AFFINITY_NO_AFFINITY is not set
+CONFIG_MDNS_TASK_AFFINITY_CPU0=y
+CONFIG_MDNS_TASK_AFFINITY=0x0
+CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS=2000
+CONFIG_MDNS_TIMER_PERIOD_MS=100
+# CONFIG_MDNS_NETWORKING_SOCKET is not set
+# CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES is not set
+# CONFIG_MDNS_ENABLE_DEBUG_PRINTS is not set
+# CONFIG_MDNS_RESPOND_REVERSE_QUERIES is not set
+CONFIG_MDNS_MULTIPLE_INSTANCE=y
+
+#
+# MDNS Predefined interfaces
+#
+CONFIG_MDNS_PREDEF_NETIF_STA=y
+CONFIG_MDNS_PREDEF_NETIF_AP=y
+CONFIG_MDNS_PREDEF_NETIF_ETH=y
+# end of MDNS Predefined interfaces
+# end of mDNS
 # end of Component config
 
 # CONFIG_IDF_EXPERIMENTAL_FEATURES is not set
index 3fca4204a051d43208ebd66e831d2d0c66b01ecd..4fbf7bfaf0fc98473e6ce4d5eb974533f4fb28ef 100644 (file)
@@ -542,13 +542,13 @@ CONFIG_SPI_SLAVE_ISR_IN_IRAM=y
 #
 # UART Configuration
 #
-CONFIG_UART_ISR_IN_IRAM=y
+# CONFIG_UART_ISR_IN_IRAM is not set
 # end of UART Configuration
 
 #
 # GPIO Configuration
 #
-CONFIG_GPIO_CTRL_FUNC_IN_IRAM=y
+# CONFIG_GPIO_CTRL_FUNC_IN_IRAM is not set
 # end of GPIO Configuration
 
 #
@@ -631,7 +631,8 @@ CONFIG_ESP_TLS_USE_DS_PERIPHERAL=y
 # CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS is not set
 # CONFIG_ESP_TLS_SERVER is not set
 # CONFIG_ESP_TLS_PSK_VERIFICATION is not set
-# CONFIG_ESP_TLS_INSECURE is not set
+CONFIG_ESP_TLS_INSECURE=y
+CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y
 # end of ESP-TLS
 
 #
@@ -1355,7 +1356,8 @@ CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
 CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y
 # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_CMN is not set
 # CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE is not set
-# CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE is not set
+CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE=y
+CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH="/home/jweigele/localDownloads/certbundle/bundle.pem"
 CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_MAX_CERTS=200
 # end of Certificate Bundle
 
@@ -1447,7 +1449,7 @@ CONFIG_MBEDTLS_ECP_DP_CURVE25519_ENABLED=y
 CONFIG_MBEDTLS_ECP_NIST_OPTIM=y
 # CONFIG_MBEDTLS_POLY1305_C is not set
 # CONFIG_MBEDTLS_CHACHA20_C is not set
-# CONFIG_MBEDTLS_HKDF_C is not set
+CONFIG_MBEDTLS_HKDF_C=y
 # CONFIG_MBEDTLS_THREADING_C is not set
 # CONFIG_MBEDTLS_SECURITY_RISKS is not set
 # end of mbedTLS
@@ -1672,16 +1674,38 @@ CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y
 #
 # AQI Program Configuration
 #
-# CONFIG_TEMP_ENABLED is not set
+CONFIG_TEMP_ENABLED=y
+# CONFIG_ZIG_ENABLED is not set
+CONFIG_WIFI_ENABLED=y
 CONFIG_LED_ENABLED=y
+CONFIG_BROKER_URL="mqtts://esp32:remoteaccess@rabbitmq:8883"
+CONFIG_LOCATION="ESP32wifitest"
 # end of AQI Program Configuration
 
 #
 # ESP TM1637
 #
-CONFIG_DELAY_BLOCKING_TIME=3
+CONFIG_DELAY_BLOCKING_TIME=100
 # end of ESP TM1637
 
+#
+# Wifi Manager Configuration
+#
+CONFIG_WIFI_MANAGER_TASK_PRIORITY=5
+CONFIG_WIFI_MANAGER_RETRY_TIMER=5000
+CONFIG_WIFI_MANAGER_MAX_RETRY_START_AP=3
+CONFIG_WIFI_MANAGER_SHUTDOWN_AP_TIMER=60000
+CONFIG_WEBAPP_LOCATION="/"
+CONFIG_DEFAULT_AP_SSID="aqimonitor"
+CONFIG_DEFAULT_AP_PASSWORD="aqimonitorpw"
+CONFIG_DEFAULT_AP_CHANNEL=1
+CONFIG_DEFAULT_AP_IP="10.10.0.1"
+CONFIG_DEFAULT_AP_GATEWAY="10.10.0.1"
+CONFIG_DEFAULT_AP_NETMASK="255.255.255.0"
+CONFIG_DEFAULT_AP_MAX_CONNECTIONS=4
+CONFIG_DEFAULT_AP_BEACON_INTERVAL=100
+# end of Wifi Manager Configuration
+
 #
 # Zigbee
 #
@@ -1692,6 +1716,33 @@ CONFIG_ZB_ZED=y
 CONFIG_ZB_RADIO_NATIVE=y
 # CONFIG_ZB_RADIO_MACSPLIT_UART is not set
 # end of Zigbee
+
+#
+# mDNS
+#
+CONFIG_MDNS_MAX_INTERFACES=3
+CONFIG_MDNS_MAX_SERVICES=10
+CONFIG_MDNS_TASK_PRIORITY=1
+CONFIG_MDNS_TASK_STACK_SIZE=4096
+# CONFIG_MDNS_TASK_AFFINITY_NO_AFFINITY is not set
+CONFIG_MDNS_TASK_AFFINITY_CPU0=y
+CONFIG_MDNS_TASK_AFFINITY=0x0
+CONFIG_MDNS_SERVICE_ADD_TIMEOUT_MS=2000
+CONFIG_MDNS_TIMER_PERIOD_MS=100
+# CONFIG_MDNS_NETWORKING_SOCKET is not set
+# CONFIG_MDNS_SKIP_SUPPRESSING_OWN_QUERIES is not set
+# CONFIG_MDNS_ENABLE_DEBUG_PRINTS is not set
+# CONFIG_MDNS_RESPOND_REVERSE_QUERIES is not set
+CONFIG_MDNS_MULTIPLE_INSTANCE=y
+
+#
+# MDNS Predefined interfaces
+#
+CONFIG_MDNS_PREDEF_NETIF_STA=y
+CONFIG_MDNS_PREDEF_NETIF_AP=y
+CONFIG_MDNS_PREDEF_NETIF_ETH=y
+# end of MDNS Predefined interfaces
+# end of mDNS
 # end of Component config
 
 # CONFIG_IDF_EXPERIMENTAL_FEATURES is not set