mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-06-01 16:16:54 +08:00
Merge branch 'feature/add_coap_component' into 'master'
Add coap component and example of client and server See merge request sdk/ESP8266_RTOS_SDK!542
This commit is contained in:
8
examples/protocols/coap_client/CMakeLists.txt
Normal file
8
examples/protocols/coap_client/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# The following four 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.5)
|
||||
|
||||
set(MAIN_SRCS main/coap_client_example_main.c)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(coap_client)
|
9
examples/protocols/coap_client/Makefile
Normal file
9
examples/protocols/coap_client/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := coap_client
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
65
examples/protocols/coap_client/README.md
Normal file
65
examples/protocols/coap_client/README.md
Normal file
@ -0,0 +1,65 @@
|
||||
|
||||
# CoAP client example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
this CoAP client example is adaptation of one of the [libcoap](https://github.com/obgm/libcoap) example.
|
||||
|
||||
CoAP client example would connect your ESP32 device to any CoAP server, fetch data from CoAP server and upstream data to CoAP server.
|
||||
|
||||
The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things.
|
||||
The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.
|
||||
|
||||
please refer to [RFC7252](https://www.rfc-editor.org/rfc/pdfrfc/rfc7252.txt.pdf) for more details.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
make menuconfig
|
||||
```
|
||||
|
||||
* Set serial port under Serial Flasher config
|
||||
* Set Target Uri under Example Configuration
|
||||
* Set WiFi SSID under Example Configuration
|
||||
* Set WiFi Password under Example Configuration
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
make -j4 flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
Prerequisite: we startup a CoAP server on coap server example.
|
||||
|
||||
and you could receive data from CoAP server if succeed,
|
||||
such as the following log:
|
||||
|
||||
```
|
||||
...
|
||||
I (332) wifi: mode : sta (30:ae:a4:04:1b:7c)
|
||||
I (1672) wifi: n:11 0, o:1 0, ap:255 255, sta:11 0, prof:1
|
||||
I (1672) wifi: state: init -> auth (b0)
|
||||
I (1682) wifi: state: auth -> assoc (0)
|
||||
I (1692) wifi: state: assoc -> run (10)
|
||||
I (1692) wifi: connected with huawei_cw, channel 11
|
||||
I (1692) wifi: pm start, type: 1
|
||||
|
||||
I (2582) event: sta ip: 192.168.3.89, mask: 255.255.255.0, gw: 192.168.3.1
|
||||
I (2582) CoAP_client: Connected to AP
|
||||
I (2582) CoAP_client: DNS lookup succeeded. IP=192.168.3.84
|
||||
Received: Hello World!
|
||||
E (8102) CoAP_client: select timeout
|
||||
E (13102) CoAP_client: select timeout
|
||||
...
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
* Please make sure Target Url includes valid `host`, `port`, `path`, and begins with `coap://`
|
21
examples/protocols/coap_client/main/Kconfig.projbuild
Normal file
21
examples/protocols/coap_client/main/Kconfig.projbuild
Normal file
@ -0,0 +1,21 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
config TARGET_DOMAIN_URI
|
||||
string "Target Uri"
|
||||
default "coap://californium.eclipse.org"
|
||||
help
|
||||
Target uri for the example to use.
|
||||
|
||||
config WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
config WIFI_PASSWORD
|
||||
string "WiFi Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the example to use.
|
||||
|
||||
endmenu
|
205
examples/protocols/coap_client/main/coap_client_example_main.c
Normal file
205
examples/protocols/coap_client/main/coap_client_example_main.c
Normal file
@ -0,0 +1,205 @@
|
||||
/* CoAP 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 <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event_loop.h"
|
||||
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "coap.h"
|
||||
|
||||
/* The examples use simple WiFi configuration that you can set via
|
||||
'make menuconfig'.
|
||||
|
||||
If you'd rather not, just change the below entries to strings with
|
||||
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
||||
*/
|
||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
||||
|
||||
#define COAP_DEFAULT_TIME_SEC 5
|
||||
#define COAP_DEFAULT_TIME_USEC 0
|
||||
|
||||
/* The examples use uri "coap://californium.eclipse.org" that
|
||||
you can set via 'make menuconfig'.
|
||||
|
||||
If you'd rather not, just change the below entries to strings with
|
||||
the config you want - ie #define COAP_DEFAULT_DEMO_URI "coap://californium.eclipse.org"
|
||||
*/
|
||||
#define COAP_DEFAULT_DEMO_URI CONFIG_TARGET_DOMAIN_URI
|
||||
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
/* The event group allows multiple bits for each event,
|
||||
but we only care about one event - are we connected
|
||||
to the AP with an IP? */
|
||||
const static int CONNECTED_BIT = BIT0;
|
||||
|
||||
const static char *TAG = "CoAP_client";
|
||||
|
||||
static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *local_interface, const coap_address_t *remote,
|
||||
coap_pdu_t *sent, coap_pdu_t *received,
|
||||
const coap_tid_t id)
|
||||
{
|
||||
unsigned char* data = NULL;
|
||||
size_t data_len;
|
||||
if (COAP_RESPONSE_CLASS(received->hdr->code) == 2) {
|
||||
if (coap_get_data(received, &data_len, &data)) {
|
||||
printf("Received: %s\n", data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void coap_example_task(void *p)
|
||||
{
|
||||
struct hostent *hp;
|
||||
struct ip4_addr *ip4_addr;
|
||||
|
||||
coap_context_t* ctx = NULL;
|
||||
coap_address_t dst_addr, src_addr;
|
||||
static coap_uri_t uri;
|
||||
fd_set readfds;
|
||||
struct timeval tv;
|
||||
int flags, result;
|
||||
coap_pdu_t* request = NULL;
|
||||
const char* server_uri = COAP_DEFAULT_DEMO_URI;
|
||||
uint8_t get_method = 1;
|
||||
|
||||
while (1) {
|
||||
/* Wait for the callback to set the CONNECTED_BIT in the
|
||||
event group.
|
||||
*/
|
||||
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||
false, true, portMAX_DELAY);
|
||||
ESP_LOGI(TAG, "Connected to AP");
|
||||
|
||||
if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) {
|
||||
ESP_LOGE(TAG, "CoAP server uri error");
|
||||
break;
|
||||
}
|
||||
|
||||
hp = gethostbyname((const char *)uri.host.s);
|
||||
|
||||
if (hp == NULL) {
|
||||
ESP_LOGE(TAG, "DNS lookup failed");
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Code to print the resolved IP.
|
||||
|
||||
Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
|
||||
ip4_addr = (struct ip4_addr *)hp->h_addr;
|
||||
ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*ip4_addr));
|
||||
|
||||
coap_address_init(&src_addr);
|
||||
src_addr.addr.sin.sin_family = AF_INET;
|
||||
src_addr.addr.sin.sin_port = htons(0);
|
||||
src_addr.addr.sin.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
ctx = coap_new_context(&src_addr);
|
||||
if (ctx) {
|
||||
coap_address_init(&dst_addr);
|
||||
dst_addr.addr.sin.sin_family = AF_INET;
|
||||
dst_addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
|
||||
dst_addr.addr.sin.sin_addr.s_addr = ip4_addr->addr;
|
||||
|
||||
request = coap_new_pdu();
|
||||
if (request){
|
||||
request->hdr->type = COAP_MESSAGE_CON;
|
||||
request->hdr->id = coap_new_message_id(ctx);
|
||||
request->hdr->code = get_method;
|
||||
coap_add_option(request, COAP_OPTION_URI_PATH, uri.path.length, uri.path.s);
|
||||
|
||||
coap_register_response_handler(ctx, message_handler);
|
||||
coap_send_confirmed(ctx, ctx->endpoint, &dst_addr, request);
|
||||
|
||||
flags = fcntl(ctx->sockfd, F_GETFL, 0);
|
||||
fcntl(ctx->sockfd, F_SETFL, flags|O_NONBLOCK);
|
||||
|
||||
tv.tv_usec = COAP_DEFAULT_TIME_USEC;
|
||||
tv.tv_sec = COAP_DEFAULT_TIME_SEC;
|
||||
|
||||
for(;;) {
|
||||
FD_ZERO(&readfds);
|
||||
FD_CLR( ctx->sockfd, &readfds );
|
||||
FD_SET( ctx->sockfd, &readfds );
|
||||
result = select( ctx->sockfd+1, &readfds, 0, 0, &tv );
|
||||
if (result > 0) {
|
||||
if (FD_ISSET( ctx->sockfd, &readfds ))
|
||||
coap_read(ctx);
|
||||
} else if (result < 0) {
|
||||
break;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "select timeout");
|
||||
}
|
||||
}
|
||||
}
|
||||
coap_free_context(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
switch(event->event_id) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||
auto-reassociate. */
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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 app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK( nvs_flash_init() );
|
||||
wifi_conn_init();
|
||||
xTaskCreate(coap_example_task, "coap", 2048, NULL, 5, NULL);
|
||||
}
|
5
examples/protocols/coap_client/main/component.mk
Normal file
5
examples/protocols/coap_client/main/component.mk
Normal file
@ -0,0 +1,5 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
|
8
examples/protocols/coap_server/CMakeLists.txt
Normal file
8
examples/protocols/coap_server/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# The following four 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.5)
|
||||
|
||||
set(MAIN_SRCS main/coap_server_example_main.c)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(coap_server)
|
9
examples/protocols/coap_server/Makefile
Normal file
9
examples/protocols/coap_server/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := coap_server
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
63
examples/protocols/coap_server/README.md
Normal file
63
examples/protocols/coap_server/README.md
Normal file
@ -0,0 +1,63 @@
|
||||
|
||||
# CoAP server example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
This CoAP server example is adaptation of one of the [libcoap](https://github.com/obgm/libcoap) example.
|
||||
|
||||
CoAP server example would startup a daemon task, receive data from CoAP client and transmit data to CoAP client.
|
||||
|
||||
The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things.
|
||||
The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.
|
||||
|
||||
please refer to [RFC7252](https://www.rfc-editor.org/rfc/pdfrfc/rfc7252.txt.pdf) for more details.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
make menuconfig
|
||||
```
|
||||
|
||||
* Set default serial port under Serial Flasher config
|
||||
* Set WiFi SSID under Example Configuration
|
||||
* Set WiFi Password under Example Configuration
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
make -j4 flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
current CoAP server would startup a daemon task,
|
||||
and the log is such as the following:
|
||||
|
||||
```
|
||||
...
|
||||
I (332) wifi: mode : sta (30:ae:a4:04:1b:7c)
|
||||
I (1672) wifi: n:11 0, o:1 0, ap:255 255, sta:11 0, prof:1
|
||||
I (1672) wifi: state: init -> auth (b0)
|
||||
I (1682) wifi: state: auth -> assoc (0)
|
||||
I (1692) wifi: state: assoc -> run (10)
|
||||
I (1692) wifi: connected with huawei_cw, channel 11
|
||||
I (1692) wifi: pm start, type: 1
|
||||
|
||||
I (2622) event: sta ip: 192.168.3.84, mask: 255.255.255.0, gw: 192.168.3.1
|
||||
I (2622) CoAP_server: Connected to AP
|
||||
E (7622) CoAP_server: select timeout
|
||||
E (12622) CoAP_server: select timeout
|
||||
E (17622) CoAP_server: select timeout
|
||||
...
|
||||
```
|
||||
|
||||
if a CoAP client query `/Espressif` resource, CoAP server would return `"Hello World!"`
|
||||
|
||||
## Troubleshooting
|
||||
* Please make sure CoAP client fetchs data under path: `/Espressif`
|
15
examples/protocols/coap_server/main/Kconfig.projbuild
Normal file
15
examples/protocols/coap_server/main/Kconfig.projbuild
Normal file
@ -0,0 +1,15 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
config WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
|
||||
config WIFI_PASSWORD
|
||||
string "WiFi Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the example to use.
|
||||
|
||||
endmenu
|
192
examples/protocols/coap_server/main/coap_server_example_main.c
Normal file
192
examples/protocols/coap_server/main/coap_server_example_main.c
Normal file
@ -0,0 +1,192 @@
|
||||
/* CoAP server 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 <string.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event_loop.h"
|
||||
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "coap.h"
|
||||
|
||||
/* The examples use simple WiFi configuration that you can set via
|
||||
'make menuconfig'.
|
||||
|
||||
If you'd rather not, just change the below entries to strings with
|
||||
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
||||
*/
|
||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
||||
|
||||
#define COAP_DEFAULT_TIME_SEC 5
|
||||
#define COAP_DEFAULT_TIME_USEC 0
|
||||
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
/* The event group allows multiple bits for each event,
|
||||
but we only care about one event - are we connected
|
||||
to the AP with an IP? */
|
||||
const static int CONNECTED_BIT = BIT0;
|
||||
|
||||
const static char *TAG = "CoAP_server";
|
||||
|
||||
static coap_async_state_t *async = NULL;
|
||||
|
||||
static void
|
||||
send_async_response(coap_context_t *ctx, const coap_endpoint_t *local_if)
|
||||
{
|
||||
coap_pdu_t *response;
|
||||
unsigned char buf[3];
|
||||
const char* response_data = "Hello World!";
|
||||
size_t size = sizeof(coap_hdr_t) + 20;
|
||||
response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, size);
|
||||
response->hdr->id = coap_new_message_id(ctx);
|
||||
if (async->tokenlen)
|
||||
coap_add_token(response, async->tokenlen, async->token);
|
||||
coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf);
|
||||
coap_add_data (response, strlen(response_data), (unsigned char *)response_data);
|
||||
|
||||
if (coap_send(ctx, local_if, &async->peer, response) == COAP_INVALID_TID) {
|
||||
|
||||
}
|
||||
coap_delete_pdu(response);
|
||||
coap_async_state_t *tmp;
|
||||
coap_remove_async(ctx, async->id, &tmp);
|
||||
coap_free_async(async);
|
||||
async = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* The resource handler
|
||||
*/
|
||||
static void
|
||||
async_handler(coap_context_t *ctx, struct coap_resource_t *resource,
|
||||
const coap_endpoint_t *local_interface, coap_address_t *peer,
|
||||
coap_pdu_t *request, str *token, coap_pdu_t *response)
|
||||
{
|
||||
async = coap_register_async(ctx, peer, request, COAP_ASYNC_SEPARATE | COAP_ASYNC_CONFIRM, (void*)"no data");
|
||||
}
|
||||
|
||||
static void coap_example_thread(void *p)
|
||||
{
|
||||
coap_context_t* ctx = NULL;
|
||||
coap_address_t serv_addr;
|
||||
coap_resource_t* resource = NULL;
|
||||
fd_set readfds;
|
||||
struct timeval tv;
|
||||
int flags = 0;
|
||||
|
||||
while (1) {
|
||||
/* Wait for the callback to set the CONNECTED_BIT in the
|
||||
event group.
|
||||
*/
|
||||
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
||||
false, true, portMAX_DELAY);
|
||||
ESP_LOGI(TAG, "Connected to AP");
|
||||
|
||||
/* Prepare the CoAP server socket */
|
||||
coap_address_init(&serv_addr);
|
||||
serv_addr.addr.sin.sin_family = AF_INET;
|
||||
serv_addr.addr.sin.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
|
||||
ctx = coap_new_context(&serv_addr);
|
||||
if (ctx) {
|
||||
flags = fcntl(ctx->sockfd, F_GETFL, 0);
|
||||
fcntl(ctx->sockfd, F_SETFL, flags|O_NONBLOCK);
|
||||
|
||||
tv.tv_usec = COAP_DEFAULT_TIME_USEC;
|
||||
tv.tv_sec = COAP_DEFAULT_TIME_SEC;
|
||||
/* Initialize the resource */
|
||||
resource = coap_resource_init((unsigned char *)"Espressif", 9, 0);
|
||||
if (resource){
|
||||
coap_register_handler(resource, COAP_REQUEST_GET, async_handler);
|
||||
coap_add_resource(ctx, resource);
|
||||
/*For incoming connections*/
|
||||
for (;;) {
|
||||
FD_ZERO(&readfds);
|
||||
FD_CLR( ctx->sockfd, &readfds);
|
||||
FD_SET( ctx->sockfd, &readfds);
|
||||
|
||||
int result = select( ctx->sockfd+1, &readfds, 0, 0, &tv );
|
||||
if (result > 0){
|
||||
if (FD_ISSET( ctx->sockfd, &readfds ))
|
||||
coap_read(ctx);
|
||||
} else if (result < 0){
|
||||
break;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "select timeout");
|
||||
}
|
||||
|
||||
if (async) {
|
||||
send_async_response(ctx, ctx->endpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
coap_free_context(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
||||
{
|
||||
switch(event->event_id) {
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
||||
auto-reassociate. */
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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 app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK( nvs_flash_init() );
|
||||
wifi_conn_init();
|
||||
|
||||
xTaskCreate(coap_example_thread, "coap", 2048, NULL, 5, NULL);
|
||||
}
|
5
examples/protocols/coap_server/main/component.mk
Normal file
5
examples/protocols/coap_server/main/component.mk
Normal file
@ -0,0 +1,5 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
|
Reference in New Issue
Block a user