static bool _start_network_event_task(){ if(!_network_event_group){ _network_event_group = xEventGroupCreate(); if(!_network_event_group){ log_e("Network Event Group Create Failed!"); return false; } xEventGroupSetBits(_network_event_group, WIFI_DNS_IDLE_BIT); } if(!_network_event_queue){ _network_event_queue = xQueueCreate(32, sizeof(system_event_t *)); if(!_network_event_queue){ log_e("Network Event Queue Create Failed!"); return false; } } if(!_network_event_task_handle){ xTaskCreatePinnedToCore(_network_event_task, "network_event", 4096, NULL, ESP_TASKD_EVENT_PRIO - 1, &_network_event_task_handle, ARDUINO_RUNNING_CORE); if(!_network_event_task_handle){ log_e("Network Event Task Start Failed!"); return false; } } return esp_event_loop_init(&_network_event_cb, NULL) == ESP_OK; }
void app_main() { esp_err_t ret = ESP_OK; tcpip_adapter_init(); esp_event_loop_init(NULL, NULL); eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; /* Set the PHY address in the example configuration */ config.phy_addr = CONFIG_PHY_ADDRESS; config.gpio_config = eth_gpio_config_rmii; config.tcpip_input = tcpip_adapter_eth_input; config.clock_mode = CONFIG_PHY_CLOCK_MODE; #ifdef CONFIG_PHY_USE_POWER_PIN /* Replace the default 'power enable' function with an example-specific one that toggles a power GPIO. */ config.phy_power_enable = phy_device_power_enable_via_gpio; #endif ret = esp_eth_init(&config); if(ret == ESP_OK) { esp_eth_enable(); xTaskCreate(eth_task, "eth_task", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL); } }
//wifi_init_softap void wifi_init_softap() { tcp_event_group = xEventGroupCreate(); 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 = { .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()); }
STATIC mp_obj_t esp_initialize() { static int initialized = 0; if (!initialized) { ESP_LOGD("modnetwork", "Initializing TCP/IP"); tcpip_adapter_init(); ESP_LOGD("modnetwork", "Initializing Event Loop"); ESP_EXCEPTIONS( esp_event_loop_init(event_handler, NULL) ); ESP_LOGD("modnetwork", "esp_event_loop_init done"); initialized = 1; } return mp_const_none; }
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
void register_ethernet() { eth_event_group = xEventGroupCreate(); tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL)); eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; config.phy_addr = CONFIG_PHY_ADDRESS; config.gpio_config = eth_gpio_config_rmii; config.tcpip_input = tcpip_adapter_eth_input; config.clock_mode = CONFIG_PHY_CLOCK_MODE; #ifdef CONFIG_PHY_USE_POWER_PIN /* Replace the default 'power enable' function with an example-specific one that toggles a power GPIO. */ config.phy_power_enable = phy_device_power_enable_via_gpio; #endif ESP_ERROR_CHECK(esp_eth_init(&config)); eth_control_args.control = arg_str1(NULL, NULL, "<start|stop|info>", "Start/Stop Ethernet or Get info of Ethernet"); eth_control_args.end = arg_end(1); const esp_console_cmd_t cmd = { .command = "ethernet", .help = "Control Ethernet interface", .hint = NULL, .func = eth_cmd_control, .argtable = ð_control_args }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); iperf_args.ip = arg_str0("c", "client", "<ip>", "run in client mode, connecting to <host>"); iperf_args.server = arg_lit0("s", "server", "run in server mode"); iperf_args.udp = arg_lit0("u", "udp", "use UDP rather than TCP"); iperf_args.port = arg_int0("p", "port", "<port>", "server port to listen on/connect to"); iperf_args.interval = arg_int0("i", "interval", "<interval>", "seconds between periodic bandwidth reports"); iperf_args.time = arg_int0("t", "time", "<time>", "time in seconds to transmit for (default 10 secs)"); iperf_args.abort = arg_lit0("a", "abort", "abort running iperf"); iperf_args.end = arg_end(1); const esp_console_cmd_t iperf_cmd = { .command = "iperf", .help = "iperf command", .hint = NULL, .func = ð_cmd_iperf, .argtable = &iperf_args }; ESP_ERROR_CHECK(esp_console_cmd_register(&iperf_cmd)); }
/*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 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()); }
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 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()); }
//wifi_init_sta void wifi_init_sta() { tcp_event_group = xEventGroupCreate(); 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 = 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); }
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() ); }
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() ); }