101 lines
2.7 KiB
C
101 lines
2.7 KiB
C
|
#include "main.h"
|
||
|
|
||
|
void printResult(MYSQL * mysqlPrint) // 打印结果集(此处传入指针,而非内容)
|
||
|
{
|
||
|
MYSQL_RES *result;
|
||
|
int i;
|
||
|
int numFields = 0;
|
||
|
int numRows = 0;
|
||
|
MYSQL_FIELD *field;
|
||
|
MYSQL_ROW row;
|
||
|
|
||
|
result = mysql_store_result(mysqlPrint); // 将查询的全部结果读取到客户端
|
||
|
numFields = mysql_num_fields(result); // 统计结果集中的字段数
|
||
|
numRows = mysql_num_rows(result); // 统计结果集的行数
|
||
|
while ((field = mysql_fetch_field(result))) // 返回结果集中的列信息(字段)
|
||
|
printf("%s\t", field->name);
|
||
|
printf("\n");
|
||
|
if (result) {
|
||
|
while ((row = mysql_fetch_row(result))) // 返回结果集中行的记录
|
||
|
{
|
||
|
for (i = 0; i < numFields; i++) {
|
||
|
printf("%s\t", row[i]);
|
||
|
}
|
||
|
printf("\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mysql_free_result(result); // 释放result空间
|
||
|
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[], char **env)
|
||
|
{
|
||
|
FILE *fp;
|
||
|
char temperature[BUFFER];
|
||
|
char time[BUFFER];
|
||
|
char dht11[BUFFER];
|
||
|
char sql_insert[BUFFER];
|
||
|
char system[BUFFER];
|
||
|
|
||
|
fp = NULL;
|
||
|
bzero(temperature, 0);
|
||
|
bzero(time, 0);
|
||
|
bzero(dht11, 0);
|
||
|
bzero(sql_insert, 0);
|
||
|
bzero(system, 0);
|
||
|
|
||
|
fp = popen("/root/gpio/wind -d 28", "r");
|
||
|
fgets(temperature, sizeof(temperature), fp);
|
||
|
temperature[strlen(temperature) - 1] = 0;
|
||
|
fp = popen("date '+%y%m%d%H%M%S'", "r");
|
||
|
fgets(time, sizeof(time), fp);
|
||
|
time[strlen(time) - 1] = 0;
|
||
|
pclose(fp);
|
||
|
|
||
|
fp = popen("/root/gpio/dht11 29", "r");
|
||
|
fgets(dht11, sizeof(dht11), fp);
|
||
|
dht11[strlen(dht11) - 1] = 0;
|
||
|
printf("%s\n", dht11);
|
||
|
pclose(fp);
|
||
|
|
||
|
|
||
|
fp = popen("getcpudata | xargs", "r");
|
||
|
fgets(system, sizeof(system), fp);
|
||
|
system[strlen(system) - 1] = 0;
|
||
|
printf("%s\n", system);
|
||
|
pclose(fp);
|
||
|
|
||
|
sprintf(sql_insert, "INSERT INTO temperature (time, temperature, dht11, SYSTEM) VALUES ('%s', '%s', '%s', '%s');", time, temperature, dht11, system);
|
||
|
puts(sql_insert);
|
||
|
|
||
|
|
||
|
|
||
|
MYSQL mysql;
|
||
|
mysql_init(&mysql);
|
||
|
if (mysql_real_connect(&mysql, "git.aixiao.me", "root", "198", "mysql", 3306, NULL, 0)) {
|
||
|
;
|
||
|
} else {
|
||
|
fprintf(stderr, "Connect failed:\n");
|
||
|
if (mysql_errno(&mysql)) {
|
||
|
printf("\terror code is %d\n\treason:%s\n", mysql_errno(&mysql), mysql_error(&mysql));
|
||
|
}
|
||
|
goto shutdown;
|
||
|
}
|
||
|
|
||
|
if (0 != mysql_set_character_set(&mysql, "utf8"))
|
||
|
{
|
||
|
perror("mysql_set_character_set");
|
||
|
}
|
||
|
|
||
|
mysql_query(&mysql, "use raspberrypi;");
|
||
|
mysql_query(&mysql, sql_insert);
|
||
|
mysql_query(&mysql, "select *from temperature;");
|
||
|
//printResult(&mysql);
|
||
|
|
||
|
shutdown:
|
||
|
mysql_close(&mysql);
|
||
|
|
||
|
return 0;
|
||
|
}
|