From 7be98a2983c6a0f976591fab36d5b0eeff98bd71 Mon Sep 17 00:00:00 2001 From: flykhan Date: Fri, 27 Oct 2023 09:01:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E5=B9=B6=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=8F=92=E5=85=A5mqtt=E6=B6=88=E6=81=AF=E5=88=B0sqlite3?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=EF=BC=8C=E6=97=B6=E9=97=B4=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=8F=92=E5=85=A5=E6=88=90=E5=8A=9F;=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=83=A8=E5=88=86=E6=B8=A9=E6=B9=BF=E5=BA=A6=E8=A1=A8?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cgi-bin/Makefile | 5 ++++- cgi-bin/mqtt_sub_ctos.c | 24 +++++++++++------------- sql_base/green_house.db | Bin 5120 -> 5120 bytes 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cgi-bin/Makefile b/cgi-bin/Makefile index f08e059..903f166 100644 --- a/cgi-bin/Makefile +++ b/cgi-bin/Makefile @@ -11,7 +11,10 @@ CJSON_CFLAGS = -lcjson all: - + $(cc) sqlite3.c mqtt_sub_ctos.c -o mqtt_sub_ctos.cgi $(MQTT_CFLAGS) $(CJSON_CFLAGS) $(SQLITE3_CFLAGS) + $(cc) mqtt_pub_stoc.c -o mqtt_pub_stoc.cgi $(MQTT_CFLAGS) + $(cc) sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) + $(cc) sqlite3.c sqlite_cgi_insert_base.c -o sqlite_cgi_insert_base.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) sqlite_cgi_base: $(cc) sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) diff --git a/cgi-bin/mqtt_sub_ctos.c b/cgi-bin/mqtt_sub_ctos.c index 49f37d8..5bfdf48 100644 --- a/cgi-bin/mqtt_sub_ctos.c +++ b/cgi-bin/mqtt_sub_ctos.c @@ -74,16 +74,14 @@ void on_message(struct mosquitto *mosq, double temperature_value = temperature->valuedouble; double humidity_value = humidity->valuedouble; - // // 获取当前日期和时间 - // time_t current_time; - // time(¤t_time); + // 获取存入数据库的时间 + time_t now = time(NULL); // 获取当前时间 + struct tm *local = localtime(&now); // 转换为本地时间 + char time_str[80]; // 存储时间的字符串 + strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local); - // // 将时间转换为本地时间 - // struct tm *localtime = localetime(¤t_time); - - - - printf("温度: %.2lf, 湿度: %.2lf\n", temperature_value, humidity_value); + // printf("温度: %.2lf, 湿度: %.2lf, 时间: %s\n", temperature_value, humidity_value, local); + printf("温度: %.2lf, 湿度: %.2lf, 时间: %s\n", temperature_value, humidity_value, time_str); // 打开数据库 sqlite3 *db = NULL; // 数据库连接句柄 @@ -97,8 +95,8 @@ void on_message(struct mosquitto *mosq, // 构建插入语句 char insert_sql[128] = ""; sprintf(insert_sql, - "insert into temp_hum_info (temperature,humidity) values(%.2lf,%.2lf);", - temperature_value, humidity_value); // 构建插入语句 + "insert into temp_hum_info (temperature,humidity,th_date_time) values(%.2lf,%.2lf,\"%s\");", + temperature_value, humidity_value, time_str); // 构建插入语句 printf("insert_sql is %s\n", insert_sql); // 执行插入语句 @@ -130,7 +128,7 @@ int main(int argc, char const *argv[]) // 2. 创建mosquitto的客户端 unsigned char userdata[128] = "1"; - client = mosquitto_new("166", true, userdata); // 创建客户端句柄 + client = mosquitto_new("167", true, userdata); // 创建客户端句柄 // struct mosquitto* mosquitto_new( // const char *id,//用户自定义标识ID // bool clean_session, //断开后是否保留订阅信息true/false @@ -151,7 +149,7 @@ int main(int argc, char const *argv[]) // 3. 连接mqtt broker int flag = mosquitto_connect(client, "localhost", 1883, 60); - //int flag = mosquitto_connect(client, "flykhan.com", 1883, 60*60*24*30); // 连接服务器(地址,端口,超时时间) : 超时时间为一个月 + //int flag = mosquitto_connect(client, "flykhan.com", 1883, 60*60*24*30); // 连接服务器(地址,端口,超时时间) : 超时时间为一个月 if (flag == MOSQ_ERR_SUCCESS) { printf("-----连接MQTT 服务器成功!-----\n"); diff --git a/sql_base/green_house.db b/sql_base/green_house.db index b3ad242111200eae735c2a3800f187d9540f6231..a6bb9cb7133d3e4f6c85f9d1cbdfd92b9c93c6d1 100644 GIT binary patch delta 329 zcmZqBXwcZe!otJCe29Vh1M>ssE6j&B3kqyvo_vViiG_vv7Q<#9j?K&yC&*8}#jPd4 z#>~RNe2sYxa}#p}vmP_cW<{0{O!XRUtPJez#tsfVXMo_WgRc&lHZm|W)-^QHH8NK) zu&^?ssE6j(Ow=vJ#EXdNutRlw7!k{T?Vrm3JAPA;Rjks7@ z7=(oljf_o9%_bjWcLHkP&%k_%c|Y@a=C{nZn4bc*uV$7OV%B6t)y&4uEX)Yh$2$2I zw-yfz^AZN;mq2~HnU?@{Oks`@WnpDtXJ>G5+H4F04#CM_T9g@8I07ongevTFEDZ!i L8L