diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-05-27 22:49:27 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-05-27 22:49:27 +0200 |
commit | 8bc4865669ae030f355d1bea29b32bf673565cf1 (patch) | |
tree | 667e01299e9181d8e9915557887f8090f6891feb | |
parent | f79851a748e0156f21025e2f5f3b21e7c1ce3ed9 (diff) | |
download | modern-esp-sandbox-8bc4865669ae030f355d1bea29b32bf673565cf1.tar.gz modern-esp-sandbox-8bc4865669ae030f355d1bea29b32bf673565cf1.tar.bz2 modern-esp-sandbox-8bc4865669ae030f355d1bea29b32bf673565cf1.tar.xz modern-esp-sandbox-8bc4865669ae030f355d1bea29b32bf673565cf1.zip |
wip
-rw-r--r-- | main/component.mk | 21 | ||||
-rw-r--r-- | main/main.cpp | 112 |
2 files changed, 96 insertions, 37 deletions
diff --git a/main/component.mk b/main/component.mk index 500a63f..e102e7e 100644 --- a/main/component.mk +++ b/main/component.mk @@ -1,9 +1,14 @@ - -ifdef WIFI_SSID -CFLAGS += '-DWIFI_SSID="$(WIFI_SSID)"' -CPPFLAGS += '-DWIFI_SSID="$(WIFI_SSID)"' -endif -ifdef WIFI_PASSWORD -CFLAGS += '-DWIFI_PASSWORD="$(WIFI_PASSWORD)"' -CPPFLAGS += '-DWIFI_PASSWORD="$(WIFI_PASSWORD)"' +define FIX = +ifdef $(1) +CFLAGS += '-D$(1)="$$($(1))"' +CPPFLAGS += '-D$(1)="$$($(1))"' +#$$(info USING $(1)=$$($(1))) endif +endef + +$(eval $(call FIX,WIFI_SSID)) +$(eval $(call FIX,WIFI_PASSWORD)) +$(eval $(call FIX,MQTT_HOST)) +$(eval $(call FIX,MQTT_PORT)) + +fix: diff --git a/main/main.cpp b/main/main.cpp index 3acf080..ffccc99 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -7,6 +7,8 @@ #include "spiffs.h" #include "sdkconfig.h" +#include "MQTTClient.h" + #include <assert.h> /* @@ -17,6 +19,20 @@ #include <errno.h> */ +// Allow overriding configuration from Kconfig. +#ifndef WIFI_SSID +#define WIFI_SSID CONFIG_MAIN_DEFAULT_WIFI_SSID +#endif +#ifndef WIFI_PASSWORD +#define WIFI_PASSWORD CONFIG_MAIN_DEFAULT_WIFI_PASSWORD +#endif +#ifndef MQTT_HOST +#define MQTT_HOST CONFIG_MAIN_DEFAULT_MQTT_HOST +#endif +#ifndef MQTT_PORT +#define MQTT_PORT CONFIG_MAIN_DEFAULT_MQTT_PORT +#endif + extern "C" uint32_t user_rf_cal_sector_set(); @@ -35,7 +51,11 @@ struct config { char mqtt_host[20]; int mqtt_port; + + // Generated field + char mqtt_client_id[20]; }; +struct config main_config; struct _reent fs_reent; #define open(...) _spiffs_open_r(&fs_reent, __VA_ARGS__) @@ -263,7 +283,6 @@ void wifi_event_handler_cb(System_Event_t *event) printf("STA GOT IP\n"); if (main_task_handle) { xTaskNotify(main_task_handle, EVENTS_GOT_IP, eSetBits); - printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle()); } } else if (event->event_id == EVENT_STAMODE_CONNECTED) { os_printf("STA CONNECTED\n"); @@ -292,6 +311,46 @@ static int fs_init() return esp_spiffs_init(&config); } +static MQTTClient mqtt_client; +static Network mqtt_network; +unsigned char mqtt_tx_buf[80], mqtt_rx_buf[80]; + +static int connect_mqtt(struct config &config) { + int ret; + MQTTPacket_connectData cd = MQTTPacket_connectData_initializer; + + NetworkInit(&mqtt_network); + MQTTClientInit(&mqtt_client, &mqtt_network, 30000, mqtt_tx_buf, sizeof(mqtt_tx_buf), mqtt_rx_buf, sizeof(mqtt_rx_buf)); + + if ((ret = NetworkConnect(&mqtt_network, config.mqtt_host, config.mqtt_port)) != 0) { + printf("%s: NetworkConnect:%d\n", __FUNCTION__, ret); + goto fail; + } + + printf("%s: network connected\n", __FUNCTION__); + +#if defined(MQTT_TASK) + if ((ret = MQTTStartTask(&mqtt_client)) != pdPASS) { + printf("%s: MQTTStartTask:%d\n", __FUNCTION__, ret); + goto fail; + } +#endif + + cd.MQTTVersion = 3; + cd.clientID.cstring = main_config.mqtt_client_id; + + if ((ret = MQTTConnect(&mqtt_client, &cd)) != 0) { + printf("%s: MQTTConnect:%d\n", __FUNCTION__, ret); + goto fail; + } + printf("%s: mqtt connected\n", __FUNCTION__); + + return 0; + +fail: + return ret; +} + static void set_station_mode(struct config &config) { printf("STATION_MODE\n"); if (wifi_get_opmode_default() != NULL_MODE) { @@ -307,7 +366,7 @@ static void set_station_mode(struct config &config) { sprintf((char *)sc.password, config.wifi_password); wifi_station_set_config(&sc); - wifi_set_event_handler_cb(wifi_event_handler_cb); + wifi_set_event_handler_cb(wifi_event_handler_cb); wifi_station_connect(); } @@ -315,20 +374,20 @@ static void set_station_mode(struct config &config) { void main_task(void* ctx) { (void) ctx; - printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle()); - int count = 0; uint32_t notification_value; while (1) { - int timeout = xTaskNotifyWait(0, UINT32_MAX, ¬ification_value, pdMS_TO_TICKS(500)) == pdFALSE; + int timeout = xTaskNotifyWait(0, UINT32_MAX, ¬ification_value, pdMS_TO_TICKS(1000)) == pdFALSE; if (notification_value & EVENTS_GOT_IP) { struct ip_info info; wifi_get_ip_info(STATION_IF, &info); printf("ip=" IPSTR ", nm=" IPSTR ", gw=" IPSTR "\n", IP2STR(&info.ip), IP2STR(&info.netmask), IP2STR(&info.gw)); + connect_mqtt(main_config); } if (timeout) { + /* printf("Hello World! %d\n", count); if (count == 2) { // pvShowMalloc(); @@ -338,6 +397,7 @@ void main_task(void* ctx) { // set_station_mode(); } + */ count++; } @@ -356,45 +416,39 @@ void user_init() { fs_init(); - struct config config; - bzero(&config, sizeof(config)); - ret = config_read(on_config_item, &config); + bzero(&main_config, sizeof(struct config)); + ret = config_read(on_config_item, &main_config); if (ret) { printf("Loading file failed, errno=%d\n", fs_reent._errno); } - if (config.ok_count != 5) { - printf("Loading file failed, loaded bad number of items=%d\n", config.ok_count); + if (main_config.ok_count != 4) { + printf("Loading file failed, loaded bad number of items=%d\n", main_config.ok_count); } else { - config.loaded = 1; + main_config.loaded = 1; } - if (!config.loaded) { + if (!main_config.loaded) { printf("No config loaded, writing new\n"); -#ifndef WIFI_SSID -#define WIFI_SSID CONFIG_MAIN_DEFAULT_WIFI_SSID -#endif -#ifndef WIFI_PASSWORD -#define WIFI_PASSWORD CONFIG_MAIN_DEFAULT_WIFI_PASSWORD -#endif - strncpy(config.wifi_ssid, WIFI_SSID, sizeof(config.wifi_ssid)); - strncpy(config.wifi_password, WIFI_PASSWORD, sizeof(config.wifi_password)); - strncpy(config.mqtt_host, CONFIG_MAIN_DEFAULT_MQTT_HOST, sizeof(config.mqtt_host)); - config.mqtt_port = CONFIG_MAIN_DEFAULT_MQTT_PORT; - ret = config_write(config); + strncpy(main_config.wifi_ssid, WIFI_SSID, sizeof(((struct config*)0)->wifi_ssid)); + strncpy(main_config.wifi_password, WIFI_PASSWORD, sizeof(((struct config*)0)->wifi_password)); + strncpy(main_config.mqtt_host, MQTT_HOST, sizeof(((struct config*)0)->mqtt_host)); + main_config.mqtt_port = MQTT_PORT; + ret = config_write(main_config); if (ret) { printf("Writing file failed, errno=%d\n", fs_reent._errno); } } - printf("Configuration:\n"); - printf(" wifi-ssid=%s\n wifi-password=%s\n", config.wifi_ssid, config.wifi_password); - printf("\n mqtt-host=%s\n mqtt-port=%d\n", config.mqtt_host, config.mqtt_port); + uint32_t chip_id = system_get_chip_id(); + snprintf(main_config.mqtt_client_id, sizeof(main_config.mqtt_client_id), "esp-%02x%02x%02x", + (chip_id >> 16) & 0xff, (chip_id >> 8) & 0xff, chip_id & 0xff); - set_station_mode(config); + printf("Configuration:\n"); + printf(" wifi-ssid=%s\n wifi-password=%s\n\n", main_config.wifi_ssid, main_config.wifi_password); + printf(" mqtt-host=%s\n mqtt-port=%d\n mqtt-client-id=%s\n\n", main_config.mqtt_host, main_config.mqtt_port, main_config.mqtt_client_id); - return; + set_station_mode(main_config); - printf("%s: xTaskGetCurrentTaskHandle=%p\n", __FUNCTION__, xTaskGetCurrentTaskHandle()); ret = xTaskCreate(main_task, THREAD_NAME, THREAD_STACK_WORDS, NULL, THREAD_PRIO, &main_task_handle); assert(ret == pdPASS); } |