mongoose coap: CON from Server to Client fails
Posted: Sun Oct 08, 2017 8:06 pm
Hello Forum,
I'm playing around with the mongoose coap example:
https://cesanta.com/blog/coap-client-an ... eb-server/
After implementing the example to ESP32 I'd like to send a CON Message to the Client, but the Client don't raise the event.
(expantion beween comments)
Server:
Client:
The Server sends the ACK successfuly through the binded connection, but not the following CON.
Any idea what went wrong?
linuxpaul
I'm playing around with the mongoose coap example:
https://cesanta.com/blog/coap-client-an ... eb-server/
After implementing the example to ESP32 I'd like to send a CON Message to the Client, but the Client don't raise the event.
(expantion beween comments)
Server:
Code: Select all
#include "freertos/FreeRTOS.h"
#include "sdkconfig.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "string.h"
#include "mongoose.h"
#define WIFI_SSID CONFIG_WIFI_SSID
#define WIFI_PW CONFIG_WIFI_PASSWORD
#define GPIO_LED 5
static struct mg_connection *nc;
static char *s_default_address = "udp://192.168.3.1:5683";
static int WIFI_UP = 0;
static void coap_handler(struct mg_connection *nc, int ev, void *p) {
switch (ev) {
case MG_EV_COAP_CON: {
uint32_t res;
struct mg_coap_message *cm = (struct mg_coap_message *) p;
printf("CON with msg_id = %d received\n", cm->msg_id);
res = mg_coap_send_ack(nc, cm->msg_id);
if (res == 0) {
printf("Successfully sent ACK for message with msg_id = %d\n",
cm->msg_id);
} else {
printf("Error: %d\n", res);
}
// sending another CON
struct mg_coap_message ccm;
memset(&ccm, 0, sizeof(ccm));
ccm.msg_id = 2;
ccm.msg_type = MG_COAP_MSG_CON;
printf("Sending CON...\n");
res = mg_coap_send_message(nc, &ccm);
if (res == 0) {
printf("Sent CON with msg_id = %d\n", ccm.msg_id);
} else {
printf("Error: %d\n", res);
}
// ####################
break;
}
case MG_EV_COAP_NOC:
case MG_EV_COAP_ACK:
case MG_EV_COAP_RST: {
struct mg_coap_message *cm = (struct mg_coap_message *) p;
printf("ACK/RST/NOC with msg_id = %d received\n", cm->msg_id);
break;
}
}
}
esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id){
case SYSTEM_EVENT_AP_START:
printf("Accesspoint connected\n");
WIFI_UP = 1;
break;
case SYSTEM_EVENT_AP_STOP:
printf("Accesspoint stopped\n");
break;
case SYSTEM_EVENT_AP_STACONNECTED:
printf("Accesspoint Station connected\n");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
printf("Accesspoint Station disconnected\n");
break;
case SYSTEM_EVENT_AP_PROBEREQRECVED:
printf("Accesspoint Probe Request received\n");
break;
default:
break;
}
return ESP_OK;
}
static void init_wifi(void)
{
tcpip_adapter_init();
tcpip_adapter_ip_info_t info = { 0, };
IP4_ADDR(&info.ip, 192, 168, 3, 1);
IP4_ADDR(&info.gw, 192, 168, 3, 1);
IP4_ADDR(&info.netmask, 255, 255, 255, 0);
ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
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) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
wifi_config_t ap_config = {
.ap = {
.ssid=WIFI_SSID,
.ssid_len=0,
.password=WIFI_PW,
.channel=0,
.authmode=WIFI_AUTH_WPA2_PSK,
.ssid_hidden=0,
.max_connection=4,
.beacon_interval=100
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &ap_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
void app_main(void)
{
nvs_flash_init();
init_wifi();
while(! WIFI_UP) {
vTaskDelay(50 / portTICK_PERIOD_MS);
}
struct mg_mgr mgr;
mg_mgr_init(&mgr, 0);
nc = mg_bind(&mgr, s_default_address, coap_handler);
if (nc == NULL) {
printf("Unable to start listener at %s\n", s_default_address);
return;
}
printf("Listening for CoAP messages at %s\n", s_default_address);
mg_set_protocol_coap(nc);
while (true) {
mg_mgr_poll(&mgr, 2);
}
mg_mgr_free(&mgr);
}
Code: Select all
#include "freertos/FreeRTOS.h"
#include "sdkconfig.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "string.h"
#include "mongoose.h"
#define WIFI_SSID CONFIG_WIFI_SSID
#define WIFI_PW CONFIG_WIFI_PASSWORD
#define GPIO_LED 5
static struct mg_connection *nc;
static char *s_default_address = "udp://192.168.3.1:5683";
static int s_time_to_exit = 0;
static int WIFI_UP = 0;
static void coap_handler(struct mg_connection *nc, int ev, void *p) {
switch (ev) {
case MG_EV_CONNECT: {
struct mg_coap_message cm;
uint32_t res;
memset(&cm, 0, sizeof(cm));
cm.msg_id = 1;
cm.msg_type = MG_COAP_MSG_CON;
printf("Sending CON...\n");
res = mg_coap_send_message(nc, &cm);
if (res == 0) {
printf("Sent CON with msg_id = %d\n", cm.msg_id);
} else {
printf("Error: %d\n", res);
s_time_to_exit = 1;
}
break;
}
// expand EV Handler for Mesages from Server
case MG_EV_COAP_CON: {
uint32_t res;
struct mg_coap_message *cm = (struct mg_coap_message *) p;
printf("CON with msg_id = %d received\n", cm->msg_id);
res = mg_coap_send_ack(nc, cm->msg_id);
if (res == 0) {
printf("Successfully sent ACK for message with msg_id = %d\n",
cm->msg_id);
} else {
printf("Error: %d\n", res);
}
break;
}
// ##########################################
case MG_EV_COAP_ACK:
case MG_EV_COAP_RST: {
struct mg_coap_message *cm = (struct mg_coap_message *) p;
printf("ACK/RST for message with msg_id = %d received\n", cm->msg_id);
nc->flags |= MG_F_SEND_AND_CLOSE;
s_time_to_exit = 1;
break;
}
case MG_EV_CLOSE: {
if (s_time_to_exit == 0) {
printf("Server closed connection\n");
s_time_to_exit = 1;
}
break;
}
}
}
esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id){
case SYSTEM_EVENT_STA_GOT_IP:
printf("Our IP address is " IPSTR "\n", IP2STR(&event->event_info.got_ip.ip_info.ip));
printf("We have now connected to a station and can do things...\n");
WIFI_UP = 1;
break;
case SYSTEM_EVENT_WIFI_READY:
printf("Wifi ready\n");
break;
case SYSTEM_EVENT_STA_START:
printf("Station started\n");
break;
case SYSTEM_EVENT_STA_CONNECTED:
printf("Station connected\n");
break;
default:
break;
}
return ESP_OK;
}
static void init_wifi(void)
{
tcpip_adapter_init();
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) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PW,
.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 app_main(void)
{
nvs_flash_init();
init_wifi();
while(! WIFI_UP) {
vTaskDelay(50 / portTICK_PERIOD_MS);
}
struct mg_mgr mgr;
mg_mgr_init(&mgr, 0);
nc = mg_connect(&mgr, s_default_address, coap_handler);
if (nc == NULL) {
printf("Unable to start listener at %s\n", s_default_address);
return;
}
printf("Listening for CoAP messages at %s\n", s_default_address);
mg_set_protocol_coap(nc);
while (true) {
mg_mgr_poll(&mgr, 2);
}
mg_mgr_free(&mgr);
}
Any idea what went wrong?
linuxpaul