ESP-MDF + WebSocket
Posted: Tue Jul 12, 2022 9:56 am
Hi i want to use ESP-MDF mesh and web socket to exchange data between computer and nodes.
I implement websocket as in an example in ESP-IDF and I cannot connect. I have connected WIFI and set everything as on the example just pasted it into mesh example.
my error:
E (615505) esp-tls: couldn't get hostname for :ws-postman.eu-gb.mybluemix.net: getaddrinfo() returns 202, addrinfo=0x0
E (615506) TRANSPORT_BASE: Failed to open a new connection: 32769
E (615513) TRANSPORT_WS: Error connecting to host ws-postman.eu-gb.mybluemix.net:80
E (615521) WEBSOCKET_CLIENT: Error transport connect
I (615527) WEBSOCKET_CLIENT: Reconnect after 10000 ms
I (615532) get_started: WEBSOCKET_EVENT_DISCONNECTED
I checked this hostname on example and it is working good - ws://ws-postman.eu-gb.mybluemix.net/ws/echo
What am I doing wrong?
I implement websocket as in an example in ESP-IDF and I cannot connect. I have connected WIFI and set everything as on the example just pasted it into mesh example.
my error:
E (615505) esp-tls: couldn't get hostname for :ws-postman.eu-gb.mybluemix.net: getaddrinfo() returns 202, addrinfo=0x0
E (615506) TRANSPORT_BASE: Failed to open a new connection: 32769
E (615513) TRANSPORT_WS: Error connecting to host ws-postman.eu-gb.mybluemix.net:80
E (615521) WEBSOCKET_CLIENT: Error transport connect
I (615527) WEBSOCKET_CLIENT: Reconnect after 10000 ms
I (615532) get_started: WEBSOCKET_EVENT_DISCONNECTED
I checked this hostname on example and it is working good - ws://ws-postman.eu-gb.mybluemix.net/ws/echo
What am I doing wrong?
Code: Select all
#include "mdf_common.h"
#include "mwifi.h"
#include "esp_websocket_client.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "esp_transport.h"
// #define MEMORY_DEBUG
#define NO_DATA_TIMEOUT_SEC 5
static const char *TAG = "get_started";
static void root_task(void *arg)
{
mdf_err_t ret = MDF_OK;
char *data = MDF_MALLOC(MWIFI_PAYLOAD_LEN);
size_t size = MWIFI_PAYLOAD_LEN;
uint8_t src_addr[MWIFI_ADDR_LEN] = {0x0};
mwifi_data_type_t data_type = {0};
MDF_LOGI("Root is running");
for (int i = 0;; ++i) {
if (!mwifi_is_started()) {
vTaskDelay(500 / portTICK_RATE_MS);
continue;
}
size = MWIFI_PAYLOAD_LEN;
memset(data, 0, MWIFI_PAYLOAD_LEN);
ret = mwifi_root_read(src_addr, &data_type, data, &size, portMAX_DELAY);
MDF_ERROR_CONTINUE(ret != MDF_OK, "<%s> mwifi_root_read", mdf_err_to_name(ret));
MDF_LOGI("Root receive, addr: " MACSTR ", size: %d, data: %s", MAC2STR(src_addr), size, data);
size = sprintf(data, "(%d) Hello node!", i);
ret = mwifi_root_write(src_addr, 1, &data_type, data, size, true);
MDF_ERROR_CONTINUE(ret != MDF_OK, "mwifi_root_recv, ret: %x", ret);
MDF_LOGI("Root send, addr: " MACSTR ", size: %d, data: %s", MAC2STR(src_addr), size, data);
}
MDF_LOGW("Root is exit");
MDF_FREE(data);
vTaskDelete(NULL);
}
static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
switch (event_id) {
case WEBSOCKET_EVENT_CONNECTED:
ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
break;
case WEBSOCKET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "WEBSOCKET_EVENT_DISCONNECTED");
break;
case WEBSOCKET_EVENT_DATA:
ESP_LOGI(TAG, "WEBSOCKET_EVENT_DATA");
ESP_LOGI(TAG, "Received opcode=%d", data->op_code);
if (data->op_code == 0x08 && data->data_len == 2) {
ESP_LOGW(TAG, "Received closed message with code=%d", 256*data->data_ptr[0] + data->data_ptr[1]);
} else {
ESP_LOGW(TAG, "Received=%.*s", data->data_len, (char *)data->data_ptr);
}
ESP_LOGW(TAG, "Total payload length=%d, data_len=%d, current payload offset=%d\r\n", data->payload_len, data->data_len, data->payload_offset);
break;
case WEBSOCKET_EVENT_ERROR:
ESP_LOGI(TAG, "WEBSOCKET_EVENT_ERROR");
break;
}
}
static void websocket_app_start(void)
{
esp_websocket_client_config_t websocket_cfg = {};
websocket_cfg.uri = CONFIG_WEBSOCKET_URI;
ESP_LOGI(TAG, "Connecting to %s...", websocket_cfg.uri);
esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg);
esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client);
esp_websocket_client_start(client);
char data[32];
int i = 0;
while (true) {
if (esp_websocket_client_is_connected(client)) {
int len = sprintf(data, "hello %04d", i++);
ESP_LOGI(TAG, "Sending %s", data);
esp_websocket_client_send_text(client, data, len, portMAX_DELAY);
}
vTaskDelay(2000 / portTICK_RATE_MS);
}
esp_websocket_client_close(client, portMAX_DELAY);
ESP_LOGI(TAG, "Websocket Stopped");
esp_websocket_client_destroy(client);
}
static mdf_err_t wifi_init()
{
mdf_err_t ret = nvs_flash_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
MDF_ERROR_ASSERT(nvs_flash_erase());
ret = nvs_flash_init();
}
MDF_ERROR_ASSERT(ret);
MDF_ERROR_ASSERT(esp_netif_init());
MDF_ERROR_ASSERT(esp_event_loop_create_default());
MDF_ERROR_ASSERT(esp_wifi_init(&cfg));
MDF_ERROR_ASSERT(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
MDF_ERROR_ASSERT(esp_wifi_set_mode(WIFI_MODE_STA));
MDF_ERROR_ASSERT(esp_wifi_set_ps(WIFI_PS_NONE));
MDF_ERROR_ASSERT(esp_mesh_set_6m_rate(false));
MDF_ERROR_ASSERT(esp_wifi_start());
return MDF_OK;
}
/**
* @brief All module events will be sent to this task in esp-mdf
*
* @Note:
* 1. Do not block or lengthy operations in the callback function.
* 2. Do not consume a lot of memory in the callback function.
* The task memory of the callback function is only 4KB.
*/
static mdf_err_t event_loop_cb(mdf_event_loop_t event, void *ctx)
{
MDF_LOGI("event_loop_cb, event: %d", event);
switch (event) {
case MDF_EVENT_MWIFI_STARTED:
MDF_LOGI("MESH is started");
break;
case MDF_EVENT_MWIFI_PARENT_CONNECTED:
MDF_LOGI("Parent is connected on station interface");
break;
case MDF_EVENT_MWIFI_PARENT_DISCONNECTED:
MDF_LOGI("Parent is disconnected on station interface");
break;
default:
break;
}
return MDF_OK;
}
void app_main()
{
mwifi_init_config_t cfg = MWIFI_INIT_CONFIG_DEFAULT();
mwifi_config_t config = {
.router_ssid = "MAGLY",
.router_password = "ilovemagly",
.channel = CONFIG_MESH_CHANNEL,
.mesh_id = CONFIG_MESH_ID,
.mesh_type = MESH_ROOT,
};
/**
* @brief Set the log level for serial port printing.
*/
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set(TAG, ESP_LOG_DEBUG);
/**
* @brief Initialize wifi mesh.
*/
MDF_ERROR_ASSERT(mdf_event_loop_init(event_loop_cb));
MDF_ERROR_ASSERT(wifi_init());
MDF_ERROR_ASSERT(mwifi_init(&cfg));
MDF_ERROR_ASSERT(mwifi_set_config(&config));
MDF_ERROR_ASSERT(mwifi_start());
/**
* @brief Data transfer between wifi mesh devices
*/
xTaskCreate(root_task, "root_task", 4 * 1024,
NULL, CONFIG_MDF_TASK_DEFAULT_PRIOTY, NULL);
vTaskDelay(2000 / portTICK_RATE_MS);
websocket_app_start();
}