From f2cc59dd963d5a60ace8d5c8dddedbd40563d816 Mon Sep 17 00:00:00 2001 From: zogodo <742782908@qq.com> Date: Mon, 10 Feb 2020 23:45:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0MQTT=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/http_server/app_httpd.c | 24 +++++++++++++++++++++++- TC1/mqtt_server/user_mqtt_client.c | 17 ++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/TC1/http_server/app_httpd.c b/TC1/http_server/app_httpd.c index c78276e..5ba8474 100644 --- a/TC1/http_server/app_httpd.c +++ b/TC1/http_server/app_httpd.c @@ -108,7 +108,7 @@ static int HttpGetTc1Status(httpd_request_t *req) char* tc1_status = malloc(412); sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode, sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key, - ap_name, ap_key, "MQTT.ADDR", 1883, VERSION, ip_status.ip, ip_status.mask, ip_status.gateway, 0L); + ap_name, ap_key, MQTT_SERVER, MQTT_PORT, VERSION, ip_status.ip, ip_status.mask, ip_status.gateway, 0L); OSStatus err = kNoErr; send_http(tc1_status, strlen(tc1_status), exit, &err); @@ -231,6 +231,27 @@ exit: return err; } +static int HttpSetMqttConfig(httpd_request_t *req) +{ + OSStatus err = kNoErr; + + int buf_size = 97; + char *buf = malloc(buf_size); + int mode = -1; + + err = httpd_get_data(req, buf, buf_size); + require_noerr(err, exit); + + sscanf(buf, "%s %d", MQTT_SERVER, &MQTT_PORT); + user_mqtt_init(); + + send_http("OK", 2, exit, &err); + +exit: + if (buf) free(buf); + return err; +} + static int HttpGetLog(httpd_request_t *req) { OSStatus err = kNoErr; @@ -329,6 +350,7 @@ struct httpd_wsgi_call g_app_handlers[] = { { "/power", HTTPD_HDR_DEFORT, 0, NULL, HttpGetPowerInfo, NULL, NULL }, { "/wifi/config", HTTPD_HDR_DEFORT, 0, HttpGetWifiConfig, HttpSetWifiConfig, NULL, NULL }, { "/wifi/scan", HTTPD_HDR_DEFORT, 0, HttpGetWifiScan, HttpSetWifiScan, NULL, NULL }, + { "/mqtt/config", HTTPD_HDR_DEFORT, 0, NULL, HttpSetMqttConfig, NULL, NULL }, { "/log", HTTPD_HDR_DEFORT, 0, HttpGetLog, NULL, NULL, NULL }, { "/task", HTTPD_HDR_DEFORT, 0, HttpGetTasks, HttpAddTask, NULL, HttpDelTask }, { "/ota", HTTPD_HDR_DEFORT, 0, Otastatus, OtaStart, NULL, NULL }, diff --git a/TC1/mqtt_server/user_mqtt_client.c b/TC1/mqtt_server/user_mqtt_client.c index b8c840f..a2f755b 100644 --- a/TC1/mqtt_server/user_mqtt_client.c +++ b/TC1/mqtt_server/user_mqtt_client.c @@ -37,10 +37,10 @@ #define MAX_MQTT_DATA_SIZE (1024) #define MAX_MQTT_SEND_QUEUE_SIZE (10) -#ifdef MQTT_CLIENT_SSL_ENABLE +char MQTT_SERVER[64] = "192.168.33.219"; +int MQTT_SERVER_PORT = 1883; -#define MQTT_SERVER "192.168.33.201" -#define MQTT_SERVER_PORT 1883 +#ifdef MQTT_CLIENT_SSL_ENABLE char* mqtt_server_ssl_cert_str = "-----BEGIN CERTIFICATE-----\r\n\ MIIC8DCCAlmgAwIBAgIJAOD63PlXjJi8MA0GCSqGSIb3DQEBBQUAMIGQMQswCQYD\r\n\ @@ -60,14 +60,6 @@ A4GBAAqw1rK4NlRUCUBLhEFUQasjP7xfFqlVbE2cRy0Rs4o3KS0JwzQVBwG85xge\r\n\ REyPOFdGdhBY2P1FNRy0MDr6xr+D2ZOwxs63dG1nnAnWZg7qwoLgpZ4fESPD3PkA\r\n\ 1ZgKJc2zbSQ9fCPxt2W3mdVav66c6fsb7els2W2Iz7gERJSX\r\n\ -----END CERTIFICATE-----"; - -#else // ! MQTT_CLIENT_SSL_ENABLE - -//#define MQTT_SERVER user_config->mqtt_ip -//#define MQTT_SERVER_PORT user_config->mqtt_port -#define MQTT_SERVER "192.168.33.201" -#define MQTT_SERVER_PORT 1883 - #endif // MQTT_CLIENT_SSL_ENABLE typedef struct @@ -319,8 +311,7 @@ void mqtt_client_thread(mico_thread_arg_t arg) rc = NewNetwork(&n, MQTT_SERVER, MQTT_SERVER_PORT, ssl_settings); if (rc == MQTT_SUCCESS) break; - //暂时去掉这个日志 - //mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc); + mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc); } mqtt_log("MQTT network connection success!");