订阅并解析插入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:
$(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)

View File

@ -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(&current_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(&current_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

Binary file not shown.