订阅并解析插入mqtt消息到sqlite3数据库,时间获取插入成功; 增加部分温湿度表测试数据

This commit is contained in:
flykhan 2023-10-27 09:01:28 +08:00
parent 769064c24e
commit 7be98a2983
3 changed files with 15 additions and 14 deletions

View File

@ -11,7 +11,10 @@ CJSON_CFLAGS = -lcjson
all: 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: sqlite_cgi_base:
$(cc) sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS) $(cc) sqlite3.c sqlite_cgi_base.c -o sqlite_cgi_base.cgi $(SQLITE3_CFLAGS) $(CJSON_CFLAGS)

View File

@ -74,16 +74,14 @@ void on_message(struct mosquitto *mosq,
double temperature_value = temperature->valuedouble; double temperature_value = temperature->valuedouble;
double humidity_value = humidity->valuedouble; double humidity_value = humidity->valuedouble;
// // 获取当前日期和时间 // 获取存入数据库的时间
// time_t current_time; time_t now = time(NULL); // 获取当前时间
// time(&current_time); struct tm *local = localtime(&now); // 转换为本地时间
char time_str[80]; // 存储时间的字符串
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local);
// // 将时间转换为本地时间 // printf("温度: %.2lf, 湿度: %.2lf, 时间: %s\n", temperature_value, humidity_value, local);
// struct tm *localtime = localetime(&current_time); printf("温度: %.2lf, 湿度: %.2lf, 时间: %s\n", temperature_value, humidity_value, time_str);
printf("温度: %.2lf, 湿度: %.2lf\n", temperature_value, humidity_value);
// 打开数据库 // 打开数据库
sqlite3 *db = NULL; // 数据库连接句柄 sqlite3 *db = NULL; // 数据库连接句柄
@ -97,8 +95,8 @@ void on_message(struct mosquitto *mosq,
// 构建插入语句 // 构建插入语句
char insert_sql[128] = ""; char insert_sql[128] = "";
sprintf(insert_sql, sprintf(insert_sql,
"insert into temp_hum_info (temperature,humidity) values(%.2lf,%.2lf);", "insert into temp_hum_info (temperature,humidity,th_date_time) values(%.2lf,%.2lf,\"%s\");",
temperature_value, humidity_value); // 构建插入语句 temperature_value, humidity_value, time_str); // 构建插入语句
printf("insert_sql is %s\n", insert_sql); printf("insert_sql is %s\n", insert_sql);
// 执行插入语句 // 执行插入语句
@ -130,7 +128,7 @@ int main(int argc, char const *argv[])
// 2. 创建mosquitto的客户端 // 2. 创建mosquitto的客户端
unsigned char userdata[128] = "1"; unsigned char userdata[128] = "1";
client = mosquitto_new("166", true, userdata); // 创建客户端句柄 client = mosquitto_new("167", true, userdata); // 创建客户端句柄
// struct mosquitto* mosquitto_new( // struct mosquitto* mosquitto_new(
// const char *id,//用户自定义标识ID // const char *id,//用户自定义标识ID
// bool clean_session, //断开后是否保留订阅信息true/false // bool clean_session, //断开后是否保留订阅信息true/false
@ -151,7 +149,7 @@ int main(int argc, char const *argv[])
// 3. 连接mqtt broker // 3. 连接mqtt broker
int flag = mosquitto_connect(client, "localhost", 1883, 60); 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) if (flag == MOSQ_ERR_SUCCESS)
{ {
printf("-----连接MQTT 服务器成功!-----\n"); printf("-----连接MQTT 服务器成功!-----\n");

Binary file not shown.