mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-08-06 15:15:15 +08:00
feat: add coap server
This commit is contained in:
126
examples/coap_server/Makefile
Normal file
126
examples/coap_server/Makefile
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
#############################################################
|
||||||
|
# Required variables for each makefile
|
||||||
|
# Discard this section from all parent makefiles
|
||||||
|
# Expected variables (with automatic defaults):
|
||||||
|
# CSRCS (all "C" files in the dir)
|
||||||
|
# SUBDIRS (all subdirs with a Makefile)
|
||||||
|
# GEN_LIBS - list of libs to be generated ()
|
||||||
|
# GEN_IMAGES - list of object file images to be generated ()
|
||||||
|
# GEN_BINS - list of binaries to be generated ()
|
||||||
|
# COMPONENTS_xxx - a list of libs/objs in the form
|
||||||
|
# subdir/lib to be extracted and rolled up into
|
||||||
|
# a generated lib/image xxx.a ()
|
||||||
|
#
|
||||||
|
TARGET = eagle
|
||||||
|
#FLAVOR = release
|
||||||
|
FLAVOR = debug
|
||||||
|
|
||||||
|
#EXTRA_CCFLAGS += -u
|
||||||
|
|
||||||
|
ifndef PDIR # {
|
||||||
|
GEN_IMAGES= eagle.app.v6.out
|
||||||
|
GEN_BINS= eagle.app.v6.bin
|
||||||
|
SPECIAL_MKTARGETS=$(APP_MKTARGETS)
|
||||||
|
SUBDIRS= \
|
||||||
|
user
|
||||||
|
|
||||||
|
endif # } PDIR
|
||||||
|
|
||||||
|
LDDIR = $(SDK_PATH)/ld
|
||||||
|
|
||||||
|
CCFLAGS += -Os
|
||||||
|
|
||||||
|
TARGET_LDFLAGS = \
|
||||||
|
-nostdlib \
|
||||||
|
-Wl,-EL \
|
||||||
|
--longcalls \
|
||||||
|
--text-section-literals
|
||||||
|
|
||||||
|
ifeq ($(FLAVOR),debug)
|
||||||
|
TARGET_LDFLAGS += -g -O2
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(FLAVOR),release)
|
||||||
|
TARGET_LDFLAGS += -g -O0
|
||||||
|
endif
|
||||||
|
|
||||||
|
COMPONENTS_eagle.app.v6 = \
|
||||||
|
user/libuser.a
|
||||||
|
|
||||||
|
LINKFLAGS_eagle.app.v6 = \
|
||||||
|
-L$(SDK_PATH)/lib \
|
||||||
|
-Wl,--gc-sections \
|
||||||
|
-nostdlib \
|
||||||
|
-T$(LD_FILE) \
|
||||||
|
-Wl,--no-check-sections \
|
||||||
|
-u call_user_start \
|
||||||
|
-Wl,-static \
|
||||||
|
-Wl,--start-group \
|
||||||
|
-lcirom \
|
||||||
|
-lgcc \
|
||||||
|
-lhal \
|
||||||
|
-lcrypto \
|
||||||
|
-lfreertos \
|
||||||
|
-llwip \
|
||||||
|
-lmain \
|
||||||
|
-lnet80211 \
|
||||||
|
-lphy \
|
||||||
|
-lpp \
|
||||||
|
-lmbedtls \
|
||||||
|
-lopenssl \
|
||||||
|
-lcoap \
|
||||||
|
-lwpa \
|
||||||
|
$(DEP_LIBS_eagle.app.v6)\
|
||||||
|
-Wl,--end-group
|
||||||
|
|
||||||
|
DEPENDS_eagle.app.v6 = \
|
||||||
|
$(LD_FILE) \
|
||||||
|
$(LDDIR)/eagle.rom.addr.v6.ld
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# Configuration i.e. compile options etc.
|
||||||
|
# Target specific stuff (defines etc.) goes in here!
|
||||||
|
# Generally values applying to a tree are captured in the
|
||||||
|
# makefile at its root level - these are then overridden
|
||||||
|
# for a subtree within the makefile rooted therein
|
||||||
|
#
|
||||||
|
|
||||||
|
#UNIVERSAL_TARGET_DEFINES = \
|
||||||
|
|
||||||
|
# Other potential configuration flags include:
|
||||||
|
# -DTXRX_TXBUF_DEBUG
|
||||||
|
# -DTXRX_RXBUF_DEBUG
|
||||||
|
# -DWLAN_CONFIG_CCX
|
||||||
|
# -DMQTT_TASK: Define MQTT_TASK to enable MQTT start background
|
||||||
|
# thread for a client.
|
||||||
|
CONFIGURATION_DEFINES = -DICACHE_FLASH -DMQTT_TASK
|
||||||
|
|
||||||
|
DEFINES += \
|
||||||
|
$(UNIVERSAL_TARGET_DEFINES) \
|
||||||
|
$(CONFIGURATION_DEFINES)
|
||||||
|
|
||||||
|
DDEFINES += \
|
||||||
|
$(UNIVERSAL_TARGET_DEFINES) \
|
||||||
|
$(CONFIGURATION_DEFINES)
|
||||||
|
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# Recursion Magic - Don't touch this!!
|
||||||
|
#
|
||||||
|
# Each subtree potentially has an include directory
|
||||||
|
# corresponding to the common APIs applicable to modules
|
||||||
|
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
||||||
|
# of a module can only contain the include directories up
|
||||||
|
# its parent path, and not its siblings
|
||||||
|
#
|
||||||
|
# Required for each makefile to inherit from the parent
|
||||||
|
#
|
||||||
|
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||||
|
INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include
|
||||||
|
INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include/coap
|
||||||
|
INCLUDES += -I $(SDK_PATH)/third_party/coap/library/include/coap
|
||||||
|
sinclude $(SDK_PATH)/Makefile
|
||||||
|
|
||||||
|
.PHONY: FORCE
|
||||||
|
FORCE:
|
||||||
|
|
192
examples/coap_server/gen_misc.sh
Executable file
192
examples/coap_server/gen_misc.sh
Executable file
@ -0,0 +1,192 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
:<<!
|
||||||
|
******NOTICE******
|
||||||
|
MUST set SDK_PATH & BIN_PATH firstly!!!
|
||||||
|
example:
|
||||||
|
export SDK_PATH=~/esp_iot_sdk_freertos
|
||||||
|
export BIN_PATH=~/esp8266_bin
|
||||||
|
!
|
||||||
|
|
||||||
|
export SDK_PATH=$SDK_PATH
|
||||||
|
export BIN_PATH=$BIN_PATH
|
||||||
|
|
||||||
|
echo "gen_misc.sh version 20150911"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
if [ $SDK_PATH ]; then
|
||||||
|
echo "SDK_PATH:"
|
||||||
|
echo "$SDK_PATH"
|
||||||
|
echo ""
|
||||||
|
else
|
||||||
|
echo "ERROR: Please export SDK_PATH in gen_misc.sh firstly, exit!!!"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $BIN_PATH ]; then
|
||||||
|
echo "BIN_PATH:"
|
||||||
|
echo "$BIN_PATH"
|
||||||
|
echo ""
|
||||||
|
else
|
||||||
|
echo "ERROR: Please export BIN_PATH in gen_misc.sh firstly, exit!!!"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Please check SDK_PATH & BIN_PATH, enter (Y/y) to continue:"
|
||||||
|
#read input
|
||||||
|
input=y
|
||||||
|
if [[ $input != Y ]] && [[ $input != y ]]; then
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "Please follow below steps(1-5) to generate specific bin(s):"
|
||||||
|
echo "STEP 1: use boot_v1.2+ by default"
|
||||||
|
boot=new
|
||||||
|
|
||||||
|
echo "boot mode: $boot"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "STEP 2: choose bin generate(0=eagle.flash.bin+eagle.irom0text.bin, 1=user1.bin, 2=user2.bin)"
|
||||||
|
echo "enter (0/1/2, default 0):"
|
||||||
|
#read input
|
||||||
|
input=1
|
||||||
|
if [ -z "$input" ]; then
|
||||||
|
if [ $boot != none ]; then
|
||||||
|
boot=none
|
||||||
|
echo "ignore boot"
|
||||||
|
fi
|
||||||
|
app=0
|
||||||
|
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
||||||
|
elif [ $input == 1 ]; then
|
||||||
|
if [ $boot == none ]; then
|
||||||
|
app=0
|
||||||
|
echo "choose no boot before"
|
||||||
|
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
||||||
|
else
|
||||||
|
app=1
|
||||||
|
echo "generate bin: user1.bin"
|
||||||
|
fi
|
||||||
|
elif [ $input == 2 ]; then
|
||||||
|
if [ $boot == none ]; then
|
||||||
|
app=0
|
||||||
|
echo "choose no boot before"
|
||||||
|
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
||||||
|
else
|
||||||
|
app=2
|
||||||
|
echo "generate bin: user2.bin"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ $boot != none ]; then
|
||||||
|
boot=none
|
||||||
|
echo "ignore boot"
|
||||||
|
fi
|
||||||
|
app=0
|
||||||
|
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "STEP 3: choose spi speed(0=20MHz, 1=26.7MHz, 2=40MHz, 3=80MHz)"
|
||||||
|
echo "enter (0/1/2/3, default 2):"
|
||||||
|
#read input
|
||||||
|
input=2
|
||||||
|
if [ -z "$input" ]; then
|
||||||
|
spi_speed=40
|
||||||
|
elif [ $input == 0 ]; then
|
||||||
|
spi_speed=20
|
||||||
|
elif [ $input == 1 ]; then
|
||||||
|
spi_speed=26.7
|
||||||
|
elif [ $input == 3 ]; then
|
||||||
|
spi_speed=80
|
||||||
|
else
|
||||||
|
spi_speed=40
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "spi speed: $spi_speed MHz"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "STEP 4: choose spi mode(0=QIO, 1=QOUT, 2=DIO, 3=DOUT)"
|
||||||
|
echo "enter (0/1/2/3, default 0):"
|
||||||
|
#read input
|
||||||
|
input=0
|
||||||
|
if [ -z "$input" ]; then
|
||||||
|
spi_mode=QIO
|
||||||
|
elif [ $input == 1 ]; then
|
||||||
|
spi_mode=QOUT
|
||||||
|
elif [ $input == 2 ]; then
|
||||||
|
spi_mode=DIO
|
||||||
|
elif [ $input == 3 ]; then
|
||||||
|
spi_mode=DOUT
|
||||||
|
else
|
||||||
|
spi_mode=QIO
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "spi mode: $spi_mode"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "STEP 5: choose spi size and map"
|
||||||
|
echo " 0= 512KB( 256KB+ 256KB)"
|
||||||
|
echo " 2=1024KB( 512KB+ 512KB)"
|
||||||
|
echo " 3=2048KB( 512KB+ 512KB)"
|
||||||
|
echo " 4=4096KB( 512KB+ 512KB)"
|
||||||
|
echo " 5=2048KB(1024KB+1024KB)"
|
||||||
|
echo " 6=4096KB(1024KB+1024KB)"
|
||||||
|
echo " 7=4096KB(2048KB+2048KB) not support ,just for compatible with nodeMCU board"
|
||||||
|
echo " 8=8192KB(1024KB+1024KB)"
|
||||||
|
echo " 9=16384KB(1024KB+1024KB)"
|
||||||
|
echo "enter (0/2/3/4/5/6/7/8/9, default 0):"
|
||||||
|
#read input
|
||||||
|
input=5
|
||||||
|
|
||||||
|
if [ -z "$input" ]; then
|
||||||
|
spi_size_map=0
|
||||||
|
echo "spi size: 512KB"
|
||||||
|
echo "spi ota map: 256KB + 256KB"
|
||||||
|
elif [ $input == 2 ]; then
|
||||||
|
spi_size_map=2
|
||||||
|
echo "spi size: 1024KB"
|
||||||
|
echo "spi ota map: 512KB + 512KB"
|
||||||
|
elif [ $input == 3 ]; then
|
||||||
|
spi_size_map=3
|
||||||
|
echo "spi size: 2048KB"
|
||||||
|
echo "spi ota map: 512KB + 512KB"
|
||||||
|
elif [ $input == 4 ]; then
|
||||||
|
spi_size_map=4
|
||||||
|
echo "spi size: 4096KB"
|
||||||
|
echo "spi ota map: 512KB + 512KB"
|
||||||
|
elif [ $input == 5 ]; then
|
||||||
|
spi_size_map=5
|
||||||
|
echo "spi size: 2048KB"
|
||||||
|
echo "spi ota map: 1024KB + 1024KB"
|
||||||
|
elif [ $input == 6 ]; then
|
||||||
|
spi_size_map=6
|
||||||
|
echo "spi size: 4096KB"
|
||||||
|
echo "spi ota map: 1024KB + 1024KB"
|
||||||
|
elif [ $input == 7 ]; then
|
||||||
|
spi_size_map=7
|
||||||
|
echo"not support ,just for compatible with nodeMCU board"
|
||||||
|
exit
|
||||||
|
elif [ $input == 8 ]; then
|
||||||
|
spi_size_map=8
|
||||||
|
echo "spi size: 8192KB"
|
||||||
|
echo "spi ota map: 1024KB + 1024KB"
|
||||||
|
elif [ $input == 9 ]; then
|
||||||
|
spi_size_map=9
|
||||||
|
echo "spi size: 16384KB"
|
||||||
|
echo "spi ota map: 1024KB + 1024KB"
|
||||||
|
else
|
||||||
|
spi_size_map=0
|
||||||
|
echo "spi size: 512KB"
|
||||||
|
echo "spi ota map: 256KB + 256KB"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo "start..."
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
make clean
|
||||||
|
|
||||||
|
make BOOT=$boot APP=$app SPI_SPEED=$spi_speed SPI_MODE=$spi_mode SPI_SIZE_MAP=$spi_size_map
|
32
examples/coap_server/include/user_config.h
Normal file
32
examples/coap_server/include/user_config.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* ESPRESSIF MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
|
||||||
|
* it is free of charge, to any person obtaining a copy of this software and associated
|
||||||
|
* documentation files (the "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
|
||||||
|
* to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
* substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __USER_CONFIG_H__
|
||||||
|
#define __USER_CONFIG_H__
|
||||||
|
|
||||||
|
#define SSID "BL_841R" /* Wi-Fi SSID */
|
||||||
|
#define PASSWORD "1234567890" /* Wi-Fi Password */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
47
examples/coap_server/user/Makefile
Normal file
47
examples/coap_server/user/Makefile
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
|
||||||
|
#############################################################
|
||||||
|
# Required variables for each makefile
|
||||||
|
# Discard this section from all parent makefiles
|
||||||
|
# Expected variables (with automatic defaults):
|
||||||
|
# CSRCS (all "C" files in the dir)
|
||||||
|
# SUBDIRS (all subdirs with a Makefile)
|
||||||
|
# GEN_LIBS - list of libs to be generated ()
|
||||||
|
# GEN_IMAGES - list of images to be generated ()
|
||||||
|
# COMPONENTS_xxx - a list of libs/objs in the form
|
||||||
|
# subdir/lib to be extracted and rolled up into
|
||||||
|
# a generated lib/image xxx.a ()
|
||||||
|
#
|
||||||
|
ifndef PDIR
|
||||||
|
GEN_LIBS = libuser.a
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# Configuration i.e. compile options etc.
|
||||||
|
# Target specific stuff (defines etc.) goes in here!
|
||||||
|
# Generally values applying to a tree are captured in the
|
||||||
|
# makefile at its root level - these are then overridden
|
||||||
|
# for a subtree within the makefile rooted therein
|
||||||
|
#
|
||||||
|
#DEFINES +=
|
||||||
|
DEFINES += -DWITH_POSIX
|
||||||
|
#############################################################
|
||||||
|
# Recursion Magic - Don't touch this!!
|
||||||
|
#
|
||||||
|
# Each subtree potentially has an include directory
|
||||||
|
# corresponding to the common APIs applicable to modules
|
||||||
|
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
||||||
|
# of a module can only contain the include directories up
|
||||||
|
# its parent path, and not its siblings
|
||||||
|
#
|
||||||
|
# Required for each makefile to inherit from the parent
|
||||||
|
#
|
||||||
|
|
||||||
|
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||||
|
INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include
|
||||||
|
INCLUDES += -I $(SDK_PATH)/third_party/coap/platform/include/coap
|
||||||
|
INCLUDES += -I $(SDK_PATH)/third_party/coap/library/include/coap
|
||||||
|
INCLUDES += -I ./
|
||||||
|
PDIR := ../$(PDIR)
|
||||||
|
sinclude $(PDIR)Makefile
|
||||||
|
|
102
examples/coap_server/user/coap_server_example_main.c
Normal file → Executable file
102
examples/coap_server/user/coap_server_example_main.c
Normal file → Executable file
@ -12,37 +12,29 @@
|
|||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/event_groups.h"
|
|
||||||
|
|
||||||
#include "esp_log.h"
|
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_event_loop.h"
|
|
||||||
|
|
||||||
#include "nvs_flash.h"
|
|
||||||
|
|
||||||
#include "coap.h"
|
#include "coap.h"
|
||||||
|
|
||||||
/* The examples use simple WiFi configuration that you can set via
|
// for libcirom.a
|
||||||
'make menuconfig'.
|
int _getpid_r(struct _reent *r)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int _kill_r(struct _reent *r, int pid, int sig)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
If you'd rather not, just change the below entries to strings with
|
#define COAP_SERVER_THREAD_NAME "coap_server_thread"
|
||||||
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
#define COAP_SERVER_THREAD_STACK_WORDS 2048
|
||||||
*/
|
#define COAP_SERVER_THREAD_PRIO 8
|
||||||
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
||||||
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
LOCAL xTaskHandle coap_server_handle;
|
||||||
|
|
||||||
#define COAP_DEFAULT_TIME_SEC 5
|
#define COAP_DEFAULT_TIME_SEC 5
|
||||||
#define COAP_DEFAULT_TIME_USEC 0
|
#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 coap_async_state_t *async = NULL;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -50,7 +42,7 @@ send_async_response(coap_context_t *ctx, const coap_endpoint_t *local_if)
|
|||||||
{
|
{
|
||||||
coap_pdu_t *response;
|
coap_pdu_t *response;
|
||||||
unsigned char buf[3];
|
unsigned char buf[3];
|
||||||
const char* response_data = "Hello World WTF!";
|
const char* response_data = "Hello esp8266!";
|
||||||
size_t size = sizeof(coap_hdr_t) + 20;
|
size_t size = sizeof(coap_hdr_t) + 20;
|
||||||
response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, size);
|
response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, size);
|
||||||
response->hdr->id = coap_new_message_id(ctx);
|
response->hdr->id = coap_new_message_id(ctx);
|
||||||
@ -90,13 +82,6 @@ static void coap_example_thread(void *p)
|
|||||||
int flags = 0;
|
int flags = 0;
|
||||||
|
|
||||||
while (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");
|
|
||||||
|
|
||||||
/* Prepare the CoAP server socket */
|
/* Prepare the CoAP server socket */
|
||||||
coap_address_init(&serv_addr);
|
coap_address_init(&serv_addr);
|
||||||
serv_addr.addr.sin.sin_family = AF_INET;
|
serv_addr.addr.sin.sin_family = AF_INET;
|
||||||
@ -127,7 +112,7 @@ static void coap_example_thread(void *p)
|
|||||||
} else if (result < 0){
|
} else if (result < 0){
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, "select timeout");
|
printf("select timeout\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (async) {
|
if (async) {
|
||||||
@ -143,50 +128,17 @@ static void coap_example_thread(void *p)
|
|||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
|
void user_conn_init(void)
|
||||||
{
|
{
|
||||||
switch(event->event_id) {
|
int ret;
|
||||||
case SYSTEM_EVENT_STA_START:
|
ret = xTaskCreate(coap_example_thread,
|
||||||
esp_wifi_connect();
|
COAP_SERVER_THREAD_NAME,
|
||||||
break;
|
COAP_SERVER_THREAD_STACK_WORDS,
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
NULL,
|
||||||
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
COAP_SERVER_THREAD_PRIO,
|
||||||
break;
|
&coap_server_handle);
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
|
||||||
/* This is a workaround as ESP32 WiFi libs don't currently
|
if (ret != pdPASS) {
|
||||||
auto-reassociate. */
|
printf("create coap server thread %s failed\n", COAP_SERVER_THREAD_NAME);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
122
examples/coap_server/user/user_main.c
Normal file
122
examples/coap_server/user/user_main.c
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* ESPRESSIF MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
|
||||||
|
* it is free of charge, to any person obtaining a copy of this software and associated
|
||||||
|
* documentation files (the "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
|
||||||
|
* to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
* substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "esp_common.h"
|
||||||
|
#include "user_config.h"
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* FunctionName : user_rf_cal_sector_set
|
||||||
|
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
|
||||||
|
* We add this function to force users to set rf cal sector, since
|
||||||
|
* we don't know which sector is free in user's application.
|
||||||
|
* sector map for last several sectors : ABCCC
|
||||||
|
* A : rf cal
|
||||||
|
* B : rf init data
|
||||||
|
* C : sdk parameters
|
||||||
|
* Parameters : none
|
||||||
|
* Returns : rf cal sector
|
||||||
|
*******************************************************************************/
|
||||||
|
uint32 user_rf_cal_sector_set(void)
|
||||||
|
{
|
||||||
|
flash_size_map size_map = system_get_flash_size_map();
|
||||||
|
uint32 rf_cal_sec = 0;
|
||||||
|
|
||||||
|
switch (size_map) {
|
||||||
|
case FLASH_SIZE_4M_MAP_256_256:
|
||||||
|
rf_cal_sec = 128 - 5;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FLASH_SIZE_8M_MAP_512_512:
|
||||||
|
rf_cal_sec = 256 - 5;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FLASH_SIZE_16M_MAP_512_512:
|
||||||
|
case FLASH_SIZE_16M_MAP_1024_1024:
|
||||||
|
rf_cal_sec = 512 - 5;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FLASH_SIZE_32M_MAP_512_512:
|
||||||
|
case FLASH_SIZE_32M_MAP_1024_1024:
|
||||||
|
rf_cal_sec = 1024 - 5;
|
||||||
|
break;
|
||||||
|
case FLASH_SIZE_64M_MAP_1024_1024:
|
||||||
|
rf_cal_sec = 2048 - 5;
|
||||||
|
break;
|
||||||
|
case FLASH_SIZE_128M_MAP_1024_1024:
|
||||||
|
rf_cal_sec = 4096 - 5;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
rf_cal_sec = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rf_cal_sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wifi_event_handler_cb(System_Event_t *event)
|
||||||
|
{
|
||||||
|
if (event == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (event->event_id) {
|
||||||
|
case EVENT_STAMODE_GOT_IP:
|
||||||
|
os_printf("sta got ip ,create task and free heap size is %d\n", system_get_free_heap_size());
|
||||||
|
user_conn_init();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EVENT_STAMODE_CONNECTED:
|
||||||
|
os_printf("sta connected\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EVENT_STAMODE_DISCONNECTED:
|
||||||
|
wifi_station_connect();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* FunctionName : user_init
|
||||||
|
* Description : entry of user application, init user function here
|
||||||
|
* Parameters : none
|
||||||
|
* Returns : none
|
||||||
|
*******************************************************************************/
|
||||||
|
void user_init(void)
|
||||||
|
{
|
||||||
|
os_printf("SDK version:%s %d\n", system_get_sdk_version(), system_get_free_heap_size());
|
||||||
|
wifi_set_opmode(STATION_MODE);
|
||||||
|
|
||||||
|
struct station_config config;
|
||||||
|
bzero(&config, sizeof(struct station_config));
|
||||||
|
sprintf(config.ssid, SSID);
|
||||||
|
sprintf(config.password, PASSWORD);
|
||||||
|
wifi_station_set_config(&config);
|
||||||
|
|
||||||
|
wifi_set_event_handler_cb(wifi_event_handler_cb);
|
||||||
|
|
||||||
|
wifi_station_connect();
|
||||||
|
}
|
BIN
lib/libcoap.a
BIN
lib/libcoap.a
Binary file not shown.
1
third_party/coap/library/Makefile
vendored
1
third_party/coap/library/Makefile
vendored
@ -26,6 +26,7 @@ endif
|
|||||||
# for a subtree within the makefile rooted therein
|
# for a subtree within the makefile rooted therein
|
||||||
#
|
#
|
||||||
#DEFINES +=
|
#DEFINES +=
|
||||||
|
DEFINES += -DWITH_POSIX
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# Recursion Magic - Don't touch this!!
|
# Recursion Magic - Don't touch this!!
|
||||||
|
3
third_party/coap/library/address.c
vendored
3
third_party/coap/library/address.c
vendored
@ -12,6 +12,9 @@
|
|||||||
|
|
||||||
#include "address.h"
|
#include "address.h"
|
||||||
|
|
||||||
|
#ifndef IN6_IS_ADDR_MULTICAST
|
||||||
|
#define IN6_IS_ADDR_MULTICAST(a) IN_MULTICAST(a)
|
||||||
|
#endif
|
||||||
int
|
int
|
||||||
coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
|
coap_address_equals(const coap_address_t *a, const coap_address_t *b) {
|
||||||
assert(a); assert(b);
|
assert(a); assert(b);
|
||||||
|
2
third_party/coap/library/coap_time.c
vendored
2
third_party/coap/library/coap_time.c
vendored
@ -19,7 +19,7 @@ static coap_time_t coap_clock_offset = 0;
|
|||||||
/* _POSIX_TIMERS is > 0 when clock_gettime() is available */
|
/* _POSIX_TIMERS is > 0 when clock_gettime() is available */
|
||||||
|
|
||||||
/* Use real-time clock for correct timestamps in coap_log(). */
|
/* Use real-time clock for correct timestamps in coap_log(). */
|
||||||
#define COAP_CLOCK CLOCK_REALTIME
|
//#define COAP_CLOCK CLOCK_REALTIME
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void
|
void
|
||||||
|
6
third_party/coap/library/debug.c
vendored
6
third_party/coap/library/debug.c
vendored
@ -154,6 +154,10 @@ print_readable( const unsigned char *data, unsigned int len,
|
|||||||
#define min(a,b) ((a) < (b) ? (a) : (b))
|
#define min(a,b) ((a) < (b) ? (a) : (b))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef inet_ntop
|
||||||
|
#define inet_ntop(af,src,dst,size) (((af) == AF_INET) ? ipaddr_ntoa_r((const ip_addr_t *)(src),(dst),(size)) : NULL)
|
||||||
|
#endif
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len) {
|
coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len) {
|
||||||
#ifdef HAVE_ARPA_INET_H
|
#ifdef HAVE_ARPA_INET_H
|
||||||
@ -163,7 +167,7 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le
|
|||||||
|
|
||||||
switch (addr->addr.sa.sa_family) {
|
switch (addr->addr.sa.sa_family) {
|
||||||
case AF_INET:
|
case AF_INET:
|
||||||
addrptr = &addr->addr.sin.sin_addr;
|
addrptr = &addr->addr.sin.sin_addr.s_addr;
|
||||||
port = ntohs(addr->addr.sin.sin_port);
|
port = ntohs(addr->addr.sin.sin_port);
|
||||||
break;
|
break;
|
||||||
case AF_INET6:
|
case AF_INET6:
|
||||||
|
1
third_party/coap/platform/Makefile
vendored
1
third_party/coap/platform/Makefile
vendored
@ -26,6 +26,7 @@ endif
|
|||||||
# for a subtree within the makefile rooted therein
|
# for a subtree within the makefile rooted therein
|
||||||
#
|
#
|
||||||
#DEFINES +=
|
#DEFINES +=
|
||||||
|
DEFINES += -DWITH_POSIX
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# Recursion Magic - Don't touch this!!
|
# Recursion Magic - Don't touch this!!
|
||||||
|
Reference in New Issue
Block a user