summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main/main.cpp45
1 files changed, 37 insertions, 8 deletions
diff --git a/main/main.cpp b/main/main.cpp
index 9f864cb..7858a77 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -293,7 +293,7 @@ void wifi_event_handler_cb(System_Event_t *event)
}
wifi_station_connect();
} else {
- os_printf("Unknown event\n");
+ os_printf("unhandled event=%d\n", event->event_id);
}
}
@@ -318,16 +318,23 @@ static MQTTClient mqtt_client;
static Network mqtt_network;
unsigned char mqtt_tx_buf[80], mqtt_rx_buf[80];
-static int mqtt_connect(struct config &config) {
+static int mqtt_connect() {
int ret;
MQTTPacket_connectData cd = MQTTPacket_connectData_initializer;
+ cd.keepAliveInterval = 15;
+ cd.willFlag = 1;
+ cd.will.retained = 1;
+ cd.will.topicName = MQTTString_initializer;
+ cd.will.topicName.cstring = (char *)"esp/esp-245398/online";
+ cd.will.message = MQTTString_initializer;
+ cd.will.message.cstring = (char *)"0";
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));
- if ((ret = NetworkConnect(&mqtt_network, config.mqtt_host, config.mqtt_port)) != 0) {
+ if ((ret = NetworkConnect(&mqtt_network, main_config.mqtt_host, main_config.mqtt_port)) != 0) {
printf("%s: NetworkConnect:%d\n", __FUNCTION__, ret);
goto fail;
}
@@ -341,7 +348,7 @@ static int mqtt_connect(struct config &config) {
}
#endif
- cd.MQTTVersion = 3;
+ cd.MQTTVersion = 4;
cd.clientID.cstring = main_config.mqtt_client_id;
if ((ret = MQTTConnect(&mqtt_client, &cd)) != 0) {
@@ -356,12 +363,29 @@ fail:
return ret;
}
-static int mqtt_disconnect(struct config &config) {
+static int mqtt_disconnect() {
printf("%s\n", __FUNCTION__);
+ // TODO: start timer to reconnect to mqtt
+
return 0;
}
+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);
+
+ MQTTMessage m;
+ bzero(&m, sizeof(m));
+ m.qos = QOS0;
+ m.payloadlen = strlen(value);
+ m.payload = (void *)value;
+ return MQTTPublish(&mqtt_client, buf, &m);
+}
+
static void set_station_mode(struct config &config) {
printf("STATION_MODE\n");
if (wifi_get_opmode_default() != NULL_MODE) {
@@ -394,11 +418,16 @@ void main_task(void* ctx) {
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));
- mqtt_connect(main_config);
+ if (mqtt_connect()) {
+ printf("mqtt_connect failed\n");
+ mqtt_disconnect();
+ } else {
+ int ret = mqtt_publish("online", "1");
+ printf("%s: mqtt_publish:%d\n", __FUNCTION__, ret);
+ }
}
if (notification_value & EVENTS_LOST_IP) {
- mqtt_disconnect(main_config);
- // TODO: start timer to reconnect to mqtt
+ mqtt_disconnect();
}
if (timeout) {