添加传感器上传数据库

This commit is contained in:
aixiao 2023-03-25 09:01:17 +08:00
parent 008d3f03bc
commit d3f887d658
15 changed files with 219 additions and 149 deletions

View File

@ -6,11 +6,3 @@ raspberrypi zero w GPIO 实现的智能家居
- 我发现传感器的数据是实时的,传感器的工作必须是实时的
- 有时做不到数据的准确
- 就做了多进程的shell(不同pid)来满足传感器数据现状,一个进程收集传感器数据,另一个进程处理数据.
- 谢谢@威威君帮写的app
app配合socket服务器设置
<img src="https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_20200910-131104_LightControl.png" height="1000px" width="500px" >
app主界面
<img src="https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_20200910-131834_LightControl.png" height="1000px" width="500px" >

BIN
circuits/Smart home.fzz Normal file

Binary file not shown.

Binary file not shown.

BIN
circuits/p20856.db Normal file

Binary file not shown.

View File

@ -67,25 +67,17 @@ int dht11_read_val(int *h, int *t, int pin)
}
}
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
@ -94,7 +86,7 @@ int main(int argc, char *argv[])
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}

View File

@ -78,25 +78,17 @@ int read_dht_data(float *h, float *c, float *f, int PIN)
}
}
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
@ -105,7 +97,7 @@ int main(int argc, char *argv[])
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}

View File

@ -9,25 +9,17 @@
#include <stdbool.h>
#include <string.h>
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
@ -37,7 +29,8 @@ int main(int argc, char *argv[])
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}

View File

@ -9,25 +9,17 @@
#include <stdbool.h>
#include <string.h>
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
@ -36,7 +28,8 @@ int main(int argc, char *argv[])
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}

View File

@ -7,26 +7,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <type.h>
#include <ctype.h>
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
@ -35,11 +30,13 @@ int main(int argc, char *argv[])
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}
if (is_num(2, argv) == 0) {
if (is_num(argv[2]) == 1) {
printf("Parameter error, parameter 2 is true or false\n");
exit(1);
}

View File

@ -8,25 +8,17 @@
#include <stdlib.h>
#include <string.h>
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
@ -35,7 +27,8 @@ int main(int argc, char *argv[])
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}

View File

@ -9,25 +9,17 @@
#include <stdbool.h>
#include <string.h>
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
@ -36,7 +28,7 @@ int main(int argc, char *argv[])
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}

View File

@ -9,41 +9,34 @@
#include <stdio.h>
#include <string.h>
int is_num(int n, char *argv[])
static int is_num(char *str)
{
int j = 0;
int i;
char parameter[10];
strcpy(parameter, argv[n]);
int len = strlen(parameter);
for (i = 0; i < len; i++) {
if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57
{
j++;
int i, len;
for (i = 0, len = strlen(str); i < len; i++) {
if (isdigit(str[i]) == 0) {
printf("不是数字\n");
return 1;
}
}
if (j == len) {
return 1;
} else {
return 0;
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("Parameter error.\n");
exit(1);
}
if (is_num(1, argv) == 0) {
printf("Parameter error, Parameter 1 is the tring pin\n");
if (is_num(argv[1]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}
if (is_num(2, argv) == 0) {
printf("Parameter error, Parameter 2 is the echo pin\n");
if (is_num(argv[2]) == 1) {
printf("Parameter error, parameter 1 is PIN pin value\n");
exit(1);
}
}
int pin_tring = atol(argv[1]);
int pin_echo = atol(argv[2]);

View File

@ -0,0 +1,20 @@
CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc
STRIP := $(CROSS_COMPILE)strip
CFLAGS = -Wall -g -O3
LIB =
OBJ = sensor-upload
MYSQL_LIB := $(shell /opt/mysql/bin/mysql_config --libs)
MYSQL_CFLAGS := $(shell /opt/mysql/bin/mysql_config --cflags)
all: main.o
$(CC) $(CFLAGS) $(MYSQL_CFLAGS) -o $(OBJ) $^ $(MYSQL_LIB) $(LIB)
$(STRIP) $(OBJ)
.c.o:
$(CC) $(CFLAGS) $(MYSQL_CFLAGS) -c $<
clean:
rm -rf *.o
rm $(OBJ)

100
sensor-data-upload/main.c Normal file
View File

@ -0,0 +1,100 @@
#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;
}

13
sensor-data-upload/main.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mysql.h>
#include <mysqld_error.h>
#include <errmsg.h>
#define BUFFER 1024
#endif