esp_err_t app_prov_is_provisioned(bool *provisioned) { *provisioned = false; #ifdef CONFIG_RESET_PROVISIONED nvs_flash_erase(); #endif if (nvs_flash_init() != ESP_OK) { ESP_LOGE(TAG, "Failed to init NVS"); return ESP_FAIL; } wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); if (esp_wifi_init(&cfg) != ESP_OK) { ESP_LOGE(TAG, "Failed to init wifi"); return ESP_FAIL; } /* Get WiFi Station configuration */ wifi_config_t wifi_cfg; if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) { return ESP_FAIL; } if (strlen((const char*) wifi_cfg.sta.ssid)) { *provisioned = true; ESP_LOGI(TAG, "Found ssid %s", (const char*) wifi_cfg.sta.ssid); ESP_LOGI(TAG, "Found password %s", (const char*) wifi_cfg.sta.password); } return ESP_OK; }
void wifi_init_softap() { ap_event_group = xEventGroupCreate(); //tcpip_adapter_init(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { .ap = { .ssid = EXAMPLE_DEFAULT_SSID, .ssid_len = 0, .max_connection=EXAMPLE_MAX_STA_CONN, .password = EXAMPLE_DEFAULT_PWD, .authmode = WIFI_AUTH_WPA_WPA2_PSK }, }; if (strlen(EXAMPLE_DEFAULT_PWD) ==0) { wifi_config.ap.authmode = WIFI_AUTH_OPEN; } ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n", EXAMPLE_DEFAULT_SSID, EXAMPLE_DEFAULT_PWD); }
static void initialise_wifi(void) { wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); }
int app_main(void) { nvs_flash_init(); system_init(); tcpip_adapter_init(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); I2CScanner(); return 0; }
/** * Main entry into bootWiFi */ void bootWiFi(bootwifi_callback_t callback) { ESP_LOGD(tag, ">> bootWiFi"); g_callback = callback; nvs_flash_init(); tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_init(esp32_wifi_eventHandler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); bootWiFi2(); ESP_LOGD(tag, "<< bootWiFi"); } // bootWiFi
static bool wifiLowLevelInit(bool persistent){ if(!lowLevelInitDone){ tcpipInit(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); esp_err_t err = esp_wifi_init(&cfg); if(err){ log_e("esp_wifi_init %d", err); return false; } if(!persistent){ esp_wifi_set_storage(WIFI_STORAGE_RAM); } lowLevelInitDone = true; } return true; }
static void initialise_wifi(void *arg) { tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, arg)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_WIFI_SSID, .password = EXAMPLE_WIFI_PASS, }, }; ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); }
/*init wifi as sta and start wps*/ static void start_wps(void) { tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "start wps..."); ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_ERROR_CHECK(esp_wifi_wps_start(0)); }
static void wifi_conn_init(void) { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_WIFI_SSID, .password = EXAMPLE_WIFI_PASS, }, }; ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); }
void initialise_wifi(void) { esp_log_level_set("wifi", ESP_LOG_WARN); static bool initialized = false; if (initialized) { return; } tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) ); ESP_ERROR_CHECK( esp_wifi_start() ); initialized = true; }
static void setup_wifi() { tcpip_adapter_init(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); wifi_config_t sta_config = { .sta = { .ssid = "queque2", .password = "******", .bssid_set = false } }; ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); ESP_ERROR_CHECK( esp_wifi_connect() ); }
void wifiInit() { ESP_ERROR_CHECK(nvs_flash_init()); tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_start()); wifi_config_t wifi_config = { .sta = { .ssid = CONFIG_WIFI_SSID, .password = CONFIG_WIFI_PASSWORD}}; esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); ESP_ERROR_CHECK(esp_wifi_start()) }
STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) { static int initialized = 0; if (!initialized) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_LOGD("modnetwork", "Initializing WiFi"); ESP_EXCEPTIONS( esp_wifi_init(&cfg) ); ESP_EXCEPTIONS( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_LOGD("modnetwork", "Initialized"); initialized = 1; } int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA; if (idx == WIFI_IF_STA) { return MP_OBJ_FROM_PTR(&wlan_sta_obj); } else if (idx == WIFI_IF_AP) { return MP_OBJ_FROM_PTR(&wlan_ap_obj); } else { mp_raise_ValueError("invalid WLAN interface identifier"); } }
static void wifi_init(void) { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); wifi_config_t wifi_config = { .sta = { .ssid = CONFIG_WIFI_SSID, .password = CONFIG_WIFI_PASSWORD, }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "Waiting for wifi"); xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); }
/* Initialize Wi-Fi as sta and set scan method */ static void wifi_scan(void) { tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { .sta = { .ssid = DEFAULT_SSID, .password = DEFAULT_PWD, .scan_method = DEFAULT_SCAN_METHOD, .sort_method = DEFAULT_SORT_METHOD, .threshold.rssi = DEFAULT_RSSI, .threshold.authmode = DEFAULT_AUTHMODE, }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); }
void wifi_init_sta() { station_event_group = xEventGroupCreate(); //tcpip_adapter_init(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_FLASH)); wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_DEFAULT_SSID, .password = EXAMPLE_DEFAULT_PWD }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); ESP_ERROR_CHECK(esp_wifi_start() ); ESP_LOGI(TAG, "wifi_init_sta finished."); ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n", EXAMPLE_DEFAULT_SSID,EXAMPLE_DEFAULT_PWD); }
esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg) { /* Initialize WiFi with default config */ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); if (esp_wifi_init(&cfg) != ESP_OK) { ESP_LOGE(TAG, "Failed to init WiFi"); return ESP_FAIL; } /* Configure WiFi as station */ if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi mode"); return ESP_FAIL; } /* Configure WiFi station with host credentials * provided during provisioning */ if (esp_wifi_set_config(ESP_IF_WIFI_STA, wifi_cfg) != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi configuration"); return ESP_FAIL; } /* Start WiFi */ if (esp_wifi_start() != ESP_OK) { ESP_LOGE(TAG, "Failed to set WiFi configuration"); return ESP_FAIL; } /* Connect to AP */ if (esp_wifi_connect() != ESP_OK) { ESP_LOGE(TAG, "Failed to connect WiFi"); return ESP_FAIL; } if (g_prov) { /* Reset wifi station state for provisioning app */ g_prov->wifi_state = WIFI_PROV_STA_CONNECTING; } return ESP_OK; }
} return ESP_OK; } static void initialise_wifi(void) { wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_WIFI_SSID, .password = EXAMPLE_WIFI_PASS, }, }; tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); esp_event_loop_init(event_handler, NULL); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); } static err_t tcp_server_sent(void *arg, struct tcp_pcb *pcb, u16_t len) { ESP_LOGI(TAG, "sent data"); return ERR_OK; } static err_t tcp_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
void aws_iot_task(void *param) { char cPayload[100]; int32_t i = 0; IoT_Error_t rc = FAILURE; AWS_IoT_Client client; IoT_Client_Init_Params mqttInitParams = iotClientInitParamsDefault; IoT_Client_Connect_Params connectParams = iotClientConnectParamsDefault; IoT_Publish_Message_Params paramsQOS0; IoT_Publish_Message_Params paramsQOS1; ESP_LOGI(TAG, "AWS IoT SDK Version %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); mqttInitParams.enableAutoReconnect = false; // We enable this later below mqttInitParams.pHostURL = HostAddress; mqttInitParams.port = port; #if defined(CONFIG_EXAMPLE_EMBEDDED_CERTS) mqttInitParams.pRootCALocation = (const char *)aws_root_ca_pem_start; mqttInitParams.pDeviceCertLocation = (const char *)certificate_pem_crt_start; mqttInitParams.pDevicePrivateKeyLocation = (const char *)private_pem_key_start; #elif defined(CONFIG_EXAMPLE_FILESYSTEM_CERTS) mqttInitParams.pRootCALocation = ROOT_CA_PATH; mqttInitParams.pDeviceCertLocation = DEVICE_CERTIFICATE_PATH; mqttInitParams.pDevicePrivateKeyLocation = DEVICE_PRIVATE_KEY_PATH; #endif mqttInitParams.mqttCommandTimeout_ms = 20000; mqttInitParams.tlsHandshakeTimeout_ms = 5000; mqttInitParams.isSSLHostnameVerify = true; mqttInitParams.disconnectHandler = disconnectCallbackHandler; mqttInitParams.disconnectHandlerData = NULL; #ifdef CONFIG_EXAMPLE_SDCARD_CERTS ESP_LOGI(TAG, "Mounting SD card..."); sdmmc_host_t host = SDMMC_HOST_DEFAULT(); sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = false, .max_files = 3, }; sdmmc_card_t* card; esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to mount SD card VFAT filesystem. Error: %s", esp_err_to_name(ret)); abort(); } #endif rc = aws_iot_mqtt_init(&client, &mqttInitParams); if(SUCCESS != rc) { ESP_LOGE(TAG, "aws_iot_mqtt_init returned error : %d ", rc); abort(); } /* Wait for WiFI to show as connected */ xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); connectParams.keepAliveIntervalInSec = 10; connectParams.isCleanSession = true; connectParams.MQTTVersion = MQTT_3_1_1; /* Client ID is set in the menuconfig of the example */ connectParams.pClientID = CONFIG_AWS_EXAMPLE_CLIENT_ID; connectParams.clientIDLen = (uint16_t) strlen(CONFIG_AWS_EXAMPLE_CLIENT_ID); connectParams.isWillMsgPresent = false; ESP_LOGI(TAG, "Connecting to AWS..."); do { rc = aws_iot_mqtt_connect(&client, &connectParams); if(SUCCESS != rc) { ESP_LOGE(TAG, "Error(%d) connecting to %s:%d", rc, mqttInitParams.pHostURL, mqttInitParams.port); vTaskDelay(1000 / portTICK_RATE_MS); } } while(SUCCESS != rc); /* * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL */ rc = aws_iot_mqtt_autoreconnect_set_status(&client, true); if(SUCCESS != rc) { ESP_LOGE(TAG, "Unable to set Auto Reconnect to true - %d", rc); abort(); } const char *TOPIC = "test_topic/esp32"; const int TOPIC_LEN = strlen(TOPIC); ESP_LOGI(TAG, "Subscribing..."); rc = aws_iot_mqtt_subscribe(&client, TOPIC, TOPIC_LEN, QOS0, iot_subscribe_callback_handler, NULL); if(SUCCESS != rc) { ESP_LOGE(TAG, "Error subscribing : %d ", rc); abort(); } sprintf(cPayload, "%s : %d ", "hello from SDK", i); paramsQOS0.qos = QOS0; paramsQOS0.payload = (void *) cPayload; paramsQOS0.isRetained = 0; paramsQOS1.qos = QOS1; paramsQOS1.payload = (void *) cPayload; paramsQOS1.isRetained = 0; while(1) { //Max time the yield function will wait for read messages rc = aws_iot_mqtt_yield(&client, 100); if(NETWORK_ATTEMPTING_RECONNECT == rc) { // If the client is attempting to reconnect we will skip the rest of the loop. continue; } ESP_LOGI(TAG, "Stack remaining for task '%s' is %lu bytes", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL)); vTaskDelay(1000 / portTICK_RATE_MS); sprintf(cPayload, "%s : %d ", "hello from ESP32 (QOS0)", i++); paramsQOS0.payloadLen = strlen(cPayload); rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, ¶msQOS0); sprintf(cPayload, "%s : %d ", "hello from ESP32 (QOS1)", i++); paramsQOS1.payloadLen = strlen(cPayload); rc = aws_iot_mqtt_publish(&client, TOPIC, TOPIC_LEN, ¶msQOS1); if (rc == MQTT_REQUEST_TIMEOUT_ERROR) { ESP_LOGW(TAG, "QOS1 publish ack not received."); rc = SUCCESS; } } ESP_LOGE(TAG, "An error occurred in the main loop."); abort(); } static void initialise_wifi(void) { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_WIFI_SSID, .password = EXAMPLE_WIFI_PASS, }, }; ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); }
void aws_iot_task(void *param) { IoT_Error_t rc = FAILURE; char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); float temperature = 0.0; bool windowOpen = false; jsonStruct_t windowActuator; windowActuator.cb = windowActuate_Callback; windowActuator.pData = &windowOpen; windowActuator.pKey = "windowOpen"; windowActuator.type = SHADOW_JSON_BOOL; jsonStruct_t temperatureHandler; temperatureHandler.cb = NULL; temperatureHandler.pKey = "temperature"; temperatureHandler.pData = &temperature; temperatureHandler.type = SHADOW_JSON_FLOAT; ESP_LOGI(TAG, "AWS IoT SDK Version %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); // initialize the mqtt client AWS_IoT_Client mqttClient; ShadowInitParameters_t sp = ShadowInitParametersDefault; sp.pHost = AWS_IOT_MQTT_HOST; sp.port = AWS_IOT_MQTT_PORT; #if defined(CONFIG_EXAMPLE_EMBEDDED_CERTS) sp.pClientCRT = (const char *)certificate_pem_crt_start; sp.pClientKey = (const char *)private_pem_key_start; sp.pRootCA = (const char *)aws_root_ca_pem_start; #elif defined(CONFIG_EXAMPLE_FILESYSTEM_CERTS) sp.pClientCRT = DEVICE_CERTIFICATE_PATH; sp.pClientKey = DEVICE_PRIVATE_KEY_PATH; sp.pRootCA = ROOT_CA_PATH; #endif sp.enableAutoReconnect = false; sp.disconnectHandler = NULL; #ifdef CONFIG_EXAMPLE_SDCARD_CERTS ESP_LOGI(TAG, "Mounting SD card..."); sdmmc_host_t host = SDMMC_HOST_DEFAULT(); sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = false, .max_files = 3, }; sdmmc_card_t* card; esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to mount SD card VFAT filesystem."); abort(); } #endif /* Wait for WiFI to show as connected */ xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); ESP_LOGI(TAG, "Shadow Init"); rc = aws_iot_shadow_init(&mqttClient, &sp); if(SUCCESS != rc) { ESP_LOGE(TAG, "aws_iot_shadow_init returned error %d, aborting...", rc); abort(); } ShadowConnectParameters_t scp = ShadowConnectParametersDefault; scp.pMyThingName = CONFIG_AWS_EXAMPLE_THING_NAME; scp.pMqttClientId = CONFIG_AWS_EXAMPLE_CLIENT_ID; scp.mqttClientIdLen = (uint16_t) strlen(CONFIG_AWS_EXAMPLE_CLIENT_ID); ESP_LOGI(TAG, "Shadow Connect"); rc = aws_iot_shadow_connect(&mqttClient, &scp); if(SUCCESS != rc) { ESP_LOGE(TAG, "aws_iot_shadow_connect returned error %d, aborting...", rc); abort(); } /* * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL */ rc = aws_iot_shadow_set_autoreconnect_status(&mqttClient, true); if(SUCCESS != rc) { ESP_LOGE(TAG, "Unable to set Auto Reconnect to true - %d, aborting...", rc); abort(); } rc = aws_iot_shadow_register_delta(&mqttClient, &windowActuator); if(SUCCESS != rc) { ESP_LOGE(TAG, "Shadow Register Delta Error"); } temperature = STARTING_ROOMTEMPERATURE; // loop and publish a change in temperature while(NETWORK_ATTEMPTING_RECONNECT == rc || NETWORK_RECONNECTED == rc || SUCCESS == rc) { rc = aws_iot_shadow_yield(&mqttClient, 200); if(NETWORK_ATTEMPTING_RECONNECT == rc || shadowUpdateInProgress) { rc = aws_iot_shadow_yield(&mqttClient, 1000); // If the client is attempting to reconnect, or already waiting on a shadow update, // we will skip the rest of the loop. continue; } ESP_LOGI(TAG, "======================================================================================="); ESP_LOGI(TAG, "On Device: window state %s", windowOpen ? "true" : "false"); simulateRoomTemperature(&temperature); rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); if(SUCCESS == rc) { rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 2, &temperatureHandler, &windowActuator); if(SUCCESS == rc) { rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); if(SUCCESS == rc) { ESP_LOGI(TAG, "Update Shadow: %s", JsonDocumentBuffer); rc = aws_iot_shadow_update(&mqttClient, CONFIG_AWS_EXAMPLE_THING_NAME, JsonDocumentBuffer, ShadowUpdateStatusCallback, NULL, 4, true); shadowUpdateInProgress = true; } } } ESP_LOGI(TAG, "*****************************************************************************************"); vTaskDelay(1000 / portTICK_RATE_MS); } if(SUCCESS != rc) { ESP_LOGE(TAG, "An error occurred in the loop %d", rc); } ESP_LOGI(TAG, "Disconnecting"); rc = aws_iot_shadow_disconnect(&mqttClient); if(SUCCESS != rc) { ESP_LOGE(TAG, "Disconnect error %d", rc); } vTaskDelete(NULL); } static void initialise_wifi(void) { tcpip_adapter_init(); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); wifi_config_t wifi_config = { .sta = { .ssid = EXAMPLE_WIFI_SSID, .password = EXAMPLE_WIFI_PASS, }, }; ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) ); ESP_ERROR_CHECK( esp_wifi_start() ); }
static void esp_now_setup (esp_now_netdev_t* dev) { DEBUG("%s: %p\n", __func__, dev); /* * Init the WiFi driver. TODO It is not only required before ESP_NOW is * initialized but also before other WiFi functions are used. Once other * WiFi functions are realized it has to be moved to a more common place. */ extern portMUX_TYPE g_intr_lock_mux; mutex_init(&g_intr_lock_mux); esp_system_event_add_handler (_esp_system_event_handler, NULL); esp_err_t result; #if CONFIG_ESP32_WIFI_NVS_ENABLED result = nvs_flash_init(); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "nfs_flash_init failed with return value %d\n", result); return; } #endif wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); result = esp_wifi_init(&cfg); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_init failed with return value %d\n", result); return; } #ifdef CONFIG_WIFI_COUNTRY /* TODO */ #endif /* we use predefined station configuration */ wifi_config_t wifi_config_sta = { .sta = { .ssid = CONFIG_WIFI_STA_SSID, .password = CONFIG_WIFI_STA_PASSWORD, .channel = CONFIG_WIFI_STA_CHANNEL, .scan_method = CONFIG_WIFI_STA_SCAN_METHOD, .sort_method = CONFIG_WIFI_STA_SORT_METHOD, .threshold.rssi = CONFIG_WIFI_STA_RSSI, .threshold.authmode = CONFIG_WIFI_STA_AUTHMODE } }; /* get SoftAP interface mac address and store it as device addresss */ esp_read_mac(dev->addr, ESP_MAC_WIFI_SOFTAP); /* prepare the ESP_NOW configuration for SoftAP */ wifi_config_t wifi_config_ap = {}; strcpy ((char*)wifi_config_ap.ap.password, esp_now_params.softap_pass); sprintf((char*)wifi_config_ap.ap.ssid, "%s%02x%02x%02x%02x%02x%02x", ESP_NOW_AP_PREFIX, dev->addr[0], dev->addr[1], dev->addr[2], dev->addr[3], dev->addr[4], dev->addr[5]); wifi_config_ap.ap.ssid_len = strlen((char*)wifi_config_ap.ap.ssid); wifi_config_ap.ap.channel = esp_now_params.channel; wifi_config_ap.ap.authmode = CONFIG_WIFI_AP_AUTH; wifi_config_ap.ap.ssid_hidden = CONFIG_WIFI_AP_HIDDEN; wifi_config_ap.ap.max_connection = CONFIG_WIFI_AP_MAX_CONN; wifi_config_ap.ap.beacon_interval = CONFIG_WIFI_AP_BEACON; /* set the WiFi interface to Station + SoftAP mode without DHCP */ result = esp_wifi_set_mode(WIFI_MODE_STA | WIFI_MODE_AP); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_set_mode failed with return value %d\n", result); return; } /* set the Station and SoftAP configuration */ result = esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config_sta); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_set_config station failed with " "return value %d\n", result); return; } result = esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config_ap); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_set_mode softap failed with return value %d\n", result); return; } /* start the WiFi driver */ result = esp_wifi_start(); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_wifi_start failed with return value %d\n", result); return; } #if ESP_NOW_UNICAST==0 /* TODO */ /* all ESP-NOW nodes get the shared mac address on their station interface */ wifi_set_macaddr(STATION_IF, (uint8_t*)_esp_now_mac); #endif /* set the netdev driver */ dev->netdev.driver = &_esp_now_driver; /* initialize netdev data structure */ dev->peers_all = 0; dev->peers_enc = 0; mutex_init(&dev->dev_lock); /* initialize ESP-NOW and register callback functions */ result = esp_now_init(); if (result != ESP_OK) { LOG_TAG_ERROR("esp_now", "esp_now_init failed with return value %d\n", result); return; } esp_now_register_send_cb (esp_now_send_cb); esp_now_register_recv_cb (esp_now_recv_cb); #if ESP_NOW_UNICAST /* create the ESP_NOW event handler thread */ esp_now_event_handler_pid = thread_create(esp_now_event_handler_stack, sizeof(esp_now_event_handler_stack), ESP_NOW_PRIO + 1, THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, esp_now_event_handler, NULL, "net-esp-now-event"); /* timer for peer scan initialization */ _esp_now_scan_peers_done = false; _esp_now_scan_peers_timer.callback = &esp_now_scan_peers_timer_cb; _esp_now_scan_peers_timer.arg = dev; /* execute the first scan */ esp_now_scan_peers_done(); #else /* ESP_NOW_UNICAST */ #if 0 int res = esp_now_add_peer((uint8_t*)_esp_now_mac, ESP_NOW_ROLE_COMBO, esp_now_params.channel, NULL, 0); DEBUG("%s: multicast node added %d\n", __func__, res); #endif #endif /* ESP_NOW_UNICAST */ }