2019-06-20 07:37:40 +00:00
|
|
|
/* ESP HTTP Client Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-07-05 08:58:04 +00:00
|
|
|
#include "esp_event.h"
|
2019-06-20 07:37:40 +00:00
|
|
|
#include "protocol_examples_common.h"
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2019-11-14 10:09:38 +00:00
|
|
|
#include "freertos/semphr.h"
|
2019-06-20 07:37:40 +00:00
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_websocket_client.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
|
2019-11-14 10:09:38 +00:00
|
|
|
#define NO_DATA_TIMEOUT_SEC 10
|
|
|
|
|
2019-06-20 07:37:40 +00:00
|
|
|
static const char *TAG = "WEBSOCKET";
|
|
|
|
|
2019-11-14 10:09:38 +00:00
|
|
|
static TimerHandle_t shutdown_signal_timer;
|
|
|
|
static SemaphoreHandle_t shutdown_sema;
|
|
|
|
|
|
|
|
static void shutdown_signaler(TimerHandle_t xTimer)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "No data received for %d seconds, signaling shutdown", NO_DATA_TIMEOUT_SEC);
|
|
|
|
xSemaphoreGive(shutdown_sema);
|
|
|
|
}
|
|
|
|
|
2019-10-15 09:27:47 +00:00
|
|
|
#if CONFIG_WEBSOCKET_URI_FROM_STDIN
|
|
|
|
static void get_string(char *line, size_t size)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
while (count < size) {
|
|
|
|
int c = fgetc(stdin);
|
|
|
|
if (c == '\n') {
|
|
|
|
line[count] = '\0';
|
|
|
|
break;
|
|
|
|
} else if (c > 0 && c < 127) {
|
|
|
|
line[count] = c;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_WEBSOCKET_URI_FROM_STDIN */
|
2019-06-20 07:37:40 +00:00
|
|
|
|
|
|
|
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) {
|
2019-11-14 10:09:38 +00:00
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
|
|
|
xTimerReset(shutdown_signal_timer, portMAX_DELAY);
|
|
|
|
break;
|
|
|
|
case WEBSOCKET_EVENT_ERROR:
|
|
|
|
ESP_LOGI(TAG, "WEBSOCKET_EVENT_ERROR");
|
|
|
|
break;
|
2019-06-20 07:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void websocket_app_start(void)
|
|
|
|
{
|
2019-10-15 09:27:47 +00:00
|
|
|
esp_websocket_client_config_t websocket_cfg = {};
|
|
|
|
|
2019-11-14 10:09:38 +00:00
|
|
|
shutdown_signal_timer = xTimerCreate("Websocket shutdown timer", NO_DATA_TIMEOUT_SEC * 1000 / portTICK_PERIOD_MS,
|
|
|
|
pdFALSE, NULL, shutdown_signaler);
|
|
|
|
shutdown_sema = xSemaphoreCreateBinary();
|
|
|
|
|
|
|
|
#if CONFIG_WEBSOCKET_URI_FROM_STDIN
|
2019-10-15 09:27:47 +00:00
|
|
|
char line[128];
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Please enter uri of websocket endpoint");
|
|
|
|
get_string(line, sizeof(line));
|
|
|
|
|
|
|
|
websocket_cfg.uri = line;
|
|
|
|
ESP_LOGI(TAG, "Endpoint uri: %s\n", line);
|
|
|
|
|
2019-11-14 10:09:38 +00:00
|
|
|
#else
|
2019-10-15 09:27:47 +00:00
|
|
|
websocket_cfg.uri = CONFIG_WEBSOCKET_URI;
|
|
|
|
|
2019-11-14 10:09:38 +00:00
|
|
|
#endif /* CONFIG_WEBSOCKET_URI_FROM_STDIN */
|
2019-06-20 07:37:40 +00:00
|
|
|
|
2019-10-15 09:27:47 +00:00
|
|
|
ESP_LOGI(TAG, "Connecting to %s...", websocket_cfg.uri);
|
2019-06-20 07:37:40 +00:00
|
|
|
|
|
|
|
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);
|
2019-11-14 10:09:38 +00:00
|
|
|
xTimerStart(shutdown_signal_timer, portMAX_DELAY);
|
2019-06-20 07:37:40 +00:00
|
|
|
char data[32];
|
|
|
|
int i = 0;
|
|
|
|
while (i < 10) {
|
|
|
|
if (esp_websocket_client_is_connected(client)) {
|
|
|
|
int len = sprintf(data, "hello %04d", i++);
|
|
|
|
ESP_LOGI(TAG, "Sending %s", data);
|
2020-03-05 15:26:26 +00:00
|
|
|
esp_websocket_client_send_text(client, data, len, portMAX_DELAY);
|
2019-06-20 07:37:40 +00:00
|
|
|
}
|
|
|
|
vTaskDelay(1000 / portTICK_RATE_MS);
|
|
|
|
}
|
2019-11-14 10:09:38 +00:00
|
|
|
|
|
|
|
xSemaphoreTake(shutdown_sema, portMAX_DELAY);
|
2019-06-20 07:37:40 +00:00
|
|
|
esp_websocket_client_stop(client);
|
|
|
|
ESP_LOGI(TAG, "Websocket Stopped");
|
|
|
|
esp_websocket_client_destroy(client);
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void app_main(void)
|
2019-06-20 07:37:40 +00:00
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "[APP] Startup..");
|
|
|
|
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
|
|
|
|
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
|
|
|
esp_log_level_set("*", ESP_LOG_INFO);
|
|
|
|
esp_log_level_set("WEBSOCKET_CLIENT", ESP_LOG_DEBUG);
|
|
|
|
esp_log_level_set("TRANS_TCP", ESP_LOG_DEBUG);
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 09:54:02 +00:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2019-06-20 07:37:40 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
|
|
|
websocket_app_start();
|
|
|
|
}
|