summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2018-06-01 08:30:09 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2018-06-01 08:30:09 +0200
commitf94f9afbedb95fa2dacf33072954764e1a6d787e (patch)
treea564a541549ae072444410d795b664bbeeafaa1c
parentc374814673bb6e000074d8a82447f171b13d5499 (diff)
downloadmodern-esp-sandbox-f94f9afbedb95fa2dacf33072954764e1a6d787e.tar.gz
modern-esp-sandbox-f94f9afbedb95fa2dacf33072954764e1a6d787e.tar.bz2
modern-esp-sandbox-f94f9afbedb95fa2dacf33072954764e1a6d787e.tar.xz
modern-esp-sandbox-f94f9afbedb95fa2dacf33072954764e1a6d787e.zip
wip
-rw-r--r--main/main.cpp58
1 files changed, 52 insertions, 6 deletions
diff --git a/main/main.cpp b/main/main.cpp
index 354d015..1a021b2 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -67,6 +67,10 @@ struct _reent fs_reent;
xTaskHandle main_task_handle = 0;
+static const char *failok(int ret) {
+ return ret ? "FAIL" : "OK";
+}
+
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
@@ -331,8 +335,9 @@ static int mqtt_connect() {
NetworkInit(&mqtt_network);
- unsigned int command_timeout_ms = 30000;
- MQTTClientInit(&mqtt_client, &mqtt_network, command_timeout_ms, mqtt_tx_buf, sizeof(mqtt_tx_buf), mqtt_rx_buf, sizeof(mqtt_rx_buf));
+ unsigned int command_timeout_ms = 3000;
+ MQTTClientInit(&mqtt_client, &mqtt_network, command_timeout_ms,
+ mqtt_tx_buf, sizeof(mqtt_tx_buf), mqtt_rx_buf, sizeof(mqtt_rx_buf));
if ((ret = NetworkConnect(&mqtt_network, main_config.mqtt_host, main_config.mqtt_port)) != 0) {
printf("%s: NetworkConnect:%d\n", __FUNCTION__, ret);
@@ -371,12 +376,44 @@ static int mqtt_disconnect() {
return 0;
}
+char topic_ota[100];
+
+static int format_topic(char *buf, size_t sz, const char *suffix)
+{
+ uint32_t chip_id = system_get_chip_id();
+ int count = snprintf(buf, sz, "esp/esp-%02x%02x%02x/%s",
+ (chip_id >> 16) & 0xff, (chip_id >> 8) & 0xff, chip_id & 0xff, suffix);
+ return count >= sz;
+}
+
+static int mqtt_init() {
+ int ret = format_topic(topic_ota, sizeof(topic_ota), "ota");
+
+ return ret;
+}
+
+static void on_ota(MessageData *data) {
+ char buf[100];
+
+ int bz = sizeof(buf) - 1;
+ int sz = data->message->payloadlen <= bz ? data->message->payloadlen : bz;
+ memcpy(buf, data->message->payload, sz);
+ buf[sz] = 0;
+
+ printf("%s: len=%d, str=%p\n", __FUNCTION__, sz, buf);
+}
+
+static int mqtt_subscribe(const char *topic, messageHandler callback) {
+ printf("%s: %s\n", __FUNCTION__, topic);
+ return MQTTSubscribe(&mqtt_client, topic, QOS2, callback);
+}
+
static int mqtt_publish(const char *topic, const char *value) {
uint32_t chip_id = system_get_chip_id();
char buf[100];
snprintf(buf, sizeof(buf), "esp/esp-%02x%02x%02x/%s", (chip_id >> 16) & 0xff, (chip_id >> 8) & 0xff, chip_id & 0xff, topic);
- printf("=> %s: %s\n", buf, value);
+ printf("%s: %s: %s\n", __FUNCTION__, buf, value);
MQTTMessage m;
bzero(&m, sizeof(m));
@@ -411,6 +448,7 @@ void main_task(void* ctx) {
(void) ctx;
int count = 0;
+ int ret;
uint32_t notification_value;
while (1) {
int timeout = xTaskNotifyWait(0, UINT32_MAX, &notification_value, pdMS_TO_TICKS(1000)) == pdFALSE;
@@ -423,8 +461,13 @@ void main_task(void* ctx) {
printf("mqtt_connect failed\n");
mqtt_disconnect();
} else {
- int ret = mqtt_publish("online", "1");
- printf("%s: mqtt_publish:%d\n", __FUNCTION__, ret);
+ ret = mqtt_subscribe(topic_ota, on_ota);
+ printf("%s: mqtt_subscribe: %s\n", __FUNCTION__, failok(ret));
+ vTaskDelay(pdMS_TO_TICKS(500));
+
+ ret = mqtt_publish("online", "1");
+ printf("%s: mqtt_publish: %s\n", __FUNCTION__, failok(ret));
+ vTaskDelay(pdMS_TO_TICKS(500));
}
}
if (notification_value & EVENTS_LOST_IP) {
@@ -432,8 +475,9 @@ void main_task(void* ctx) {
}
if (timeout) {
+ if (count % 10 == 0)
+ printf("Hello World! connected=%d\n", mqtt_client.isconnected);
/*
- printf("Hello World! %d\n", count);
if (count == 2) {
// pvShowMalloc();
}
@@ -463,6 +507,8 @@ void user_init() {
fs_init();
+ assert(mqtt_init() == 0);
+
bzero(&main_config, sizeof(struct config));
ret = config_read(on_config_item, &main_config);
if (ret) {