Skip to content

Instantly share code, notes, and snippets.

@kzyapkov
Last active May 25, 2017 07:01
Show Gist options
  • Save kzyapkov/48f5cbb77362658ade981bb7becbba16 to your computer and use it in GitHub Desktop.
Save kzyapkov/48f5cbb77362658ade981bb7becbba16 to your computer and use it in GitHub Desktop.
esp_udp_send_crash
[
["wifi.sta.enable", true],
["wifi.ap.enable", false],
["wifi.sta.ssid", "oh'shelly"],
["wifi.sta.pass", "ShellyShelly"],
["wifi.ap.ssid", "shellycrash-??????"],
["wifi.ap.pass", ""],
["wifi.ap.ip", "192.168.33.1"],
["wifi.ap.gw", "192.168.33.1"],
["wifi.ap.dhcp_start", "192.168.33.2"],
["wifi.ap.dhcp_end", "192.168.33.100"],
["debug.level", 4]
]
#include "fw/src/mgos_app.h"
#include "fw/src/mgos_mongoose.h"
#include "fw/src/mgos_sys_config.h"
#include "fw/src/mgos_wifi.h"
#include "fw/src/mgos_timers.h"
static const char* str_connect_uri = "udp://pool.ntp.org:123";
static struct mg_connection* sntp_nc = NULL;
static char str_sntp_request[48];
struct mg_connection* sntp_connect();
// Main network connection handler
void sntp_connection_handler(struct mg_connection *nc, int ev, void *ev_data, void *user_data) {
(void) user_data;
(void) nc;
(void) ev_data;
if (ev != MG_EV_POLL) {
LOG(LL_DEBUG, ("sntp nc event %d", ev));
}
switch (ev) {
case MG_EV_CLOSE: {
LOG(LL_INFO, ("%p Connection closed", nc));
if (nc == sntp_nc) {
sntp_nc = sntp_connect();
}
break;
}
case MG_EV_RECV: {
LOG(LL_INFO, ("%d bytes", nc->recv_mbuf.len));
mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len);
}
}
}
struct mg_connection* sntp_connect() {
struct mg_connection* nc = mg_connect(mgos_get_mgr(), str_connect_uri, sntp_connection_handler, NULL);
if (nc == NULL) {
LOG(LL_ERROR, ("unable to create SNTP UDP socket"));
}
return nc;
}
static void send_timer(void* arg) {
(void)arg;
char* ip = mgos_wifi_get_sta_ip();
if (!sntp_nc) return;
if(mgos_wifi_get_status() == MGOS_WIFI_IP_ACQUIRED && ip != NULL) {
LOG(LL_INFO,("Try send"));
mg_send(sntp_nc, str_sntp_request, sizeof(str_sntp_request));
}
free(ip);
}
enum mgos_app_init_result mgos_app_init(void) {
sntp_nc = sntp_connect();
if (!sntp_nc) return MGOS_APP_INIT_ERROR;
// Initialize sntp request buffer, which never mutates
memset(str_sntp_request, 0, sizeof(str_sntp_request));
str_sntp_request[0] = '\x1b';
// Start send timer
mgos_set_timer(500, true, send_timer, NULL);
return MGOS_APP_INIT_SUCCESS;
}
APP = esp_crash
APP_PLATFORM = esp8266
MGOS_PATH = ../mongoose-os
APP_CONF_SCHEMA = $(CURDIR)/src/conf_schema.yaml
# This defines "all" and "clean" targets.
include $(MGOS_PATH)/fw/platforms/$(APP_PLATFORM)/Makefile
version: "1.0"
arch: esp8266
mongoose_os_version: master
sources:
- src
filesystem:
- fs
extra_files: []
skeleton_version: 2017-05-16
ffi_symbols: []
config_schema: []
build_vars:
APP_CONF_SCHEMA: src/conf_schema.yaml
MGOS_ENABLE_SNTP: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment