Signed-off-by: 李彦亲 <李彦亲@163.com>

This commit is contained in:
李彦亲 2022-02-11 16:31:53 +08:00
parent 441ce0d4a7
commit caa2e49a01
8 changed files with 270 additions and 226 deletions

View File

@ -1,7 +1,5 @@
{
"files.associations": {
"*.S": "arm",
"*.dts": "arm",
"signal.h": "c"
"server.h": "c"
}
}

View File

@ -2,7 +2,7 @@
# OBJS= main.c server.c convert.c srv.c tcp.c sqlite.c sqlite3.c modul.c shm.c serial.c
OBJS= ./src/*.c
CC=gcc
TARGET=main
TARGET=smart_greenhouse
$(TARGET):$(OBJS)
$(CC) $^ -o $@ -lpthread -lsqlite3 -ldl

Binary file not shown.

Binary file not shown.

View File

@ -1,13 +1,6 @@
/*************************************************************************
> File Name: main.c
> Author: lei
> Mail:
> Created Time: Thu 30 Aug 2018 09:14:46 AM CST
************************************************************************/
#include <stdio.h>
#include "../inc/server.h"
//#include "./include/camera_process.h"
//#include "../include/camera_process.h"
#include <sys/types.h>
#include <unistd.h>

View File

@ -6,13 +6,14 @@
#include "../inc/server.h"
#define ZGB_DATA_SIZE 36
// #define ZGB_NWK_ID 0x02
// extern int zgbfd;
extern char *p_shm;
extern char cmd_buf[1];
extern float value[8];
// m0的环境变量
struct env_char{
struct env_char
{
// char smog;
char led_one;
char led_two;
@ -33,7 +34,7 @@ struct env_char env_char_data;
void read_cmd()
{
read_shm(p_shm + 11, cmd_buf, 1);
printf("cmd_buf[0] = %d\r\n",cmd_buf[0]);
// printf("cmd_buf[0] = %d\r\n", cmd_buf[0]);
}
/*void read_secure()
@ -55,9 +56,9 @@ int zigbee_get_dat(int fd)
ssize_t ret;
unsigned char buf[ZGB_DATA_SIZE];
memset(buf, 0, sizeof(buf));
//fprintf(stdout,"hi\n");
// fprintf(stdout,"tip\n");
ret = serial_recv_exact_nbytes(fd, buf, sizeof(buf));
//fprintf(stdout,"hi1\n");
// fprintf(stdout,"tips\n");
if (ret == sizeof(buf))
{
if (buf[0] == 0xBB)
@ -85,7 +86,6 @@ int zigbee_get_dat(int fd)
data.fan = buf[25];
// data.buzz = buf[26];
env_char_data.led_one = buf[24];
env_char_data.fan = buf[25];
// env_char_data.buzz = buf[26];
@ -98,67 +98,91 @@ int zigbee_get_dat(int fd)
// zigbee发送命令
int zigbee_exe_cmd(int fd, unsigned char *p)
{
unsigned char buf[ZGB_DATA_SIZE] = {0xdd, 0x06, 0x24, 0x00, 0x00};
if (strstr(p, "LIGHT_ON") != NULL){
unsigned char buf[ZGB_DATA_SIZE] = {0xdd, data.id, 0x24, 0x00, 0x00};
if (strstr(p, "LIGHT_ON") != NULL)
{
buf[4] = 0x00;
} else if (strstr(p, "LIGHT_OFF") != NULL){
}
else if (strstr(p, "LIGHT_OFF") != NULL)
{
buf[4] = 0x01;
} else if (strstr(p, "BUZZ_ON") != NULL){
}
else if (strstr(p, "BUZZ_ON") != NULL)
{
buf[4] = 0x02;
} else if (strstr(p, "BUZZ_OFF") != NULL){
}
else if (strstr(p, "BUZZ_OFF") != NULL)
{
buf[4] = 0x03;
} else if (strstr(p, "FAN_ON") != NULL){
}
else if (strstr(p, "FAN_ON") != NULL)
{
buf[4] = 0x04;
} else if (strstr(p, "FAN_OFF") != NULL){
}
else if (strstr(p, "FAN_OFF") != NULL)
{
buf[4] = 0x08;
} else if (strstr(p, "SHU_ON") != NULL){
}
else if (strstr(p, "SHU_ON") != NULL)
{
buf[4] = 0x09;
} else if (strstr(p, "SHU_OFF") != NULL){
}
else if (strstr(p, "SHU_OFF") != NULL)
{
buf[4] = 0x0a;
}
int ret = serial_send_exact_nbytes(fd, buf, sizeof(buf));
return ret;
}
//AP发送给m0命令
//发送给m0命令
void send_cmd_m0(int signum)
{
read_cmd();
if (cmd_buf[0] == 1)
{
printf("Living now:ON = %d\r\n",cmd_buf[0]);
printf("光照灯:ON = %d\r\n", cmd_buf[0]);
zigbee_exe_cmd(zgbfd, "LIGHT_ON"); // led1
}
else if (cmd_buf[0] == 2)
{
printf("Living now:OFF = %d\r\n",cmd_buf[0]);
printf("光照灯:OFF = %d\r\n", cmd_buf[0]);
zigbee_exe_cmd(zgbfd, "LIGHT_OFF"); // led1
}
else if (cmd_buf[0] == 3)
{
printf("Bedroom now:ON = %d\r\n",cmd_buf[0]);
zigbee_exe_cmd(zgbfd,"SHU_ON"); //led2
printf("数码管:ON = %d\r\n", cmd_buf[0]);
zigbee_exe_cmd(zgbfd, "SHU_ON"); //数码管
}
else if (cmd_buf[0] == 4)
{
printf("Bedroom now:OFF = %d\r\n",cmd_buf[0]);
zigbee_exe_cmd(zgbfd,"SHU_OFF"); //led2
printf("数码管:OFF = %d\r\n", cmd_buf[0]);
zigbee_exe_cmd(zgbfd, "SHU_OFF"); //数码管
}
else if (cmd_buf[0] == 5)
{
printf("Aircondition now:ON = %d\r\n",cmd_buf[0]);
printf("风扇:ON = %d\r\n", cmd_buf[0]);
zigbee_exe_cmd(zgbfd, "FAN_ON"); // fan
}
else if (cmd_buf[0] == 6)
{
printf("Aircondition now:OFF%d\r\n",cmd_buf[0]);
zigbee_exe_cmd(zgbfd, "FAN_OFF"); // fan
printf("Aircondition now:OFF%d\r\n",cmd_buf[0]);
printf("风扇:OFF=%d\r\n", cmd_buf[0]);
}
else if (cmd_buf[0] == 7)
{
zigbee_exe_cmd(zgbfd, "BUZZ_ON"); // BUZZ
printf("蜂鸣器:ON=%d\r\n", cmd_buf[0]);
}
else if (cmd_buf[0] == 8)
{
zigbee_exe_cmd(zgbfd, "BUZZ_OFF"); // BUZZ
printf("蜂鸣器:OFF=%d\r\n", cmd_buf[0]);
}
}
@ -172,13 +196,11 @@ void send_cmd_m0(int signum)
zigbee_exe_cmd(zgbfd, "BUZZ_ON");
else
zigbee_exe_cmd(zgbfd, "BUZZ_OFF");
}*/
}
*/
// zigbee退出
int zigbee_exit(int fd)
{
//处理申请的内存等
return serial_exit(fd);
}

View File

@ -203,7 +203,7 @@ int serial_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int pari
int serial_init(char *devpath)
{
printf("串口设备地址:%s\n",devpath);
// printf("串口设备地址:%s\n",devpath);
int fd_comport = FALSE;
//打开串口
if((fd_comport=serial_open(devpath))<0)
@ -228,14 +228,14 @@ ssize_t serial_recv_exact_nbytes(int fd, void *buf, size_t count)
assert(buf != NULL);
while (total != count) {
fprintf(stdout,"%d\n",fd);
fprintf(stdout,"hi..\n");
// fprintf(stdout,"%d\n",fd);
// fprintf(stdout,"hi..\n");
ret = read(fd, buf + total, count - total);
/*int i = 0;
for (i = 0; i < 36; i++){
printf("%.2x ", buf1[i]);
}*/
fprintf(stdout,"hi2\n");
// fprintf(stdout,"hi2\n");
if (ret == -1)
{
perror("serial->recv");
@ -259,6 +259,7 @@ ssize_t serial_send_exact_nbytes(int fd, unsigned char *buf, size_t count)
ssize_t total = 0;
assert(buf != NULL);
//循环打印一下
//int i = 0;
/*for (i = 0; i < 36; i++){
printf("%.2x ", buf[i]);

View File

@ -1,4 +1,3 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
@ -36,7 +35,7 @@ void read_env(int signo)
int service_process()
{
int i = 0;
char led1,led2,fan;
char led1, led2, fan , buzz_l;
int socketId, connfd = 0;
int ret = 0;
int recv_len = 0;
@ -72,16 +71,19 @@ int service_process()
{
int i = 0;
char buf[1] = {'\0'};
AA: zgbfd = serial_init("/dev/ttyUSB0");
//等待打开串口设备
AA:
zgbfd = serial_init("/dev/ttyUSB0");
if (zgbfd == -1)
{
sleep(1);
sleep(2);
goto AA;
}
else
fprintf(stdout, "init zigbee success\n");
//创建共享内存
//创建共享内存(ipcs -m ,ipcrm -m shmid ,共享内存查看管理 )
p_shm = create_shm();
while (1)
{
@ -96,8 +98,9 @@ int service_process()
// sqlSelected(tabname1,pdb);
//写入共享内存
write_shm(p_shm, &env_char_data, sizeof(env_char_data));
//进程间通信信号
signal(SIGUSR2, read_env);
sleep(2);
sleep(1);
}
}
//建立socket连接
@ -148,7 +151,6 @@ int service_process()
}
printf("connected succeed!\r\n");
//通信
pid = fork();
if (pid < 0)
@ -161,13 +163,14 @@ int service_process()
recv_len = recv(connfd, cmd, BUFFER_SIZE, 0);
while (recv_len > 0)
{
printf("套接字数据:%s\n", cmd);
//查询历史数据
if (0 == strncmp(cmd, "SearchHistoryData", 17))
{
// p_shm = create_shm();
// read_env(signo);
// send(connfd,env_buf,sizeof(env_buf),0);
printf("cmd = %s\r\n",cmd);
// printf("cmd = %s\r\n", cmd);
for (i = 0; i < 11; i++)
{
printf("buf = %d\r\n", env_buf[i]);
@ -194,102 +197,129 @@ int service_process()
SqlInsert1(pdb, tabname2, *value, *(value + 1), *(value + 2), *(value + 3), *(value + 4), *(value + 5), *(value + 6), *(value + 7));
// sqlSelected(tabname2,pdb);
}
//解析到是主界面
else if(strncmp(cmd,"ThisIsWidget",12))
else if (0 == strncmp(cmd, "ThisIsWidget", 12))
{
p_shm = create_shm();
read_env(signo);
send(connfd, env_buf, sizeof(env_buf), 0);
printf("cmd = %s\r\n",cmd);
for(i = 0; i < 11; i++)
printf("buf = %d\r\n",env_buf[i]);
printf("主界面刷新\r\n");
// for (i = 0; i < 11; i++)
// printf("buf = %d\r\n", env_buf[i]);
}
//解析客厅灯
else if(0 == strncmp(cmd,"Livingroom:ON",13))
//解析光照灯
else if (0 == strncmp(cmd, "Lamp:ON\n", 8))
{
printf("cmd = %s\r\n",cmd);
// printf("lyq = %s\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd,"Livingroom:open",15);
send(connfd,cmd,sizeof(cmd),0);
strncpy(cmd, "Lamp:open", 10);
// printf("cmdlyq: %s\r\n", cmd);
// lyq send(connfd, cmd, sizeof(cmd), 0);
led1 = 1;
write_shm(p_shm + 11, &led1, 1);
printf("p_shm = %d\r\n",p_shm[11]);
printf("p_shm:\n %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
else if(0 == strncmp(cmd,"Livingroom:OFF",14))
else if (0 == strncmp(cmd, "Lamp:OFF\n", 9))
{
p_shm = create_shm();
printf("cmd = %s\r\n",cmd);
// p_shm = create_shm();
// printf("cmd = %s\r\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd,"Livingroom:close",16);
send(connfd,cmd,sizeof(cmd),0);
strncpy(cmd, "Lamp:close\n", 16);
// lyq send(connfd, cmd, sizeof(cmd), 0);
led1 = 2;
write_shm(p_shm + 11, &led1, 1);
printf("p_shm = %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
//解析卧室灯
else if(0 == strncmp(cmd,"Bedroom:ON",10))
//解析温控灯
else if (0 == strncmp(cmd, "TemCtl:ON\n", 11))
{
p_shm = create_shm();
printf("cmd = %s\r\n",cmd);
// p_shm = create_shm();
// printf("cmd = %s\r\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd,"Bedroom:open",10);
send(connfd,cmd,sizeof(cmd),0);
strncpy(cmd, "TemCtl:open", 12);
// lyq send(connfd, cmd, sizeof(cmd), 0);
led2 = 3;
write_shm(p_shm + 11, &led2, 1);
printf("p_shm = %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
else if(0 == strncmp(cmd,"Bedroom:OFF",11))
else if (0 == strncmp(cmd, "TemCtl:OFF\n", 12))
{
p_shm = create_shm();
printf("cmd = %s\r\n",cmd);
// p_shm = create_shm();
// printf("cmd = %s\r\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd, "Bedroom:close", 13);
send(connfd,cmd,sizeof(cmd),0);
// lyq send(connfd, cmd, sizeof(cmd), 0);
led2 = 4;
write_shm(p_shm + 11, &led2, 1);
printf("p_shm = %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
//解析空调
else if(0 == strncmp(cmd,"Aircondition:ON",15))
//解析风扇
else if (0 == strncmp(cmd, "Fan:ON\n", 8))
{
p_shm = create_shm();
printf("cmd = %s\r\n",cmd);
// p_shm = create_shm();
// printf("cmd = %s\r\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd,"Aircondition:open",17);
send(connfd,cmd,sizeof(cmd),0);
strncpy(cmd, "Fan:open", 9);
// lyq send(connfd, cmd, sizeof(cmd), 0);
fan = 5;
write_shm(p_shm + 11, &fan, 1);
printf("p_shm = %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
else if(0 == strncmp(cmd,"Aircondition:OFF",16))
else if (0 == strncmp(cmd, "Fan:OFF\n", 9))
{
p_shm = create_shm();
printf("cmd = %s\r\n",cmd);
// p_shm = create_shm();
// printf("cmd = %s\r\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd,"Aircondition:close",18);
send(connfd,cmd,sizeof(cmd),0);
strncpy(cmd, "Fan:close", 10);
// lyq send(connfd, cmd, sizeof(cmd), 0);
fan = 6;
write_shm(p_shm + 11, &fan, 1);
printf("p_shm = %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
//解析buzz
else if (0 == strncmp(cmd, "BUZZ:ON\n", 9))
{
// p_shm = create_shm();
// printf("cmd = %s\r\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd, "BUZZ:open", 9);
// lyq send(connfd, cmd, sizeof(cmd), 0);
buzz_l = 7;
write_shm(p_shm + 11, &buzz_l, 1);
printf("p_shm = %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
else if (0 == strncmp(cmd, "BUZZ:OFF\n", 10))
{
// p_shm = create_shm();
// printf("cmd = %s\r\n", cmd);
memset(cmd, 0, sizeof(cmd));
strncpy(cmd, "BUZZ:close", 10);
// lyq send(connfd, cmd, sizeof(cmd), 0);
buzz_l = 8;
write_shm(p_shm + 11, &buzz_l, 1);
printf("p_shm = %d\r\n", p_shm[11]);
signal(SIGUSR1, send_cmd_m0);
}
memset(cmd, 0, sizeof(cmd));
recv_len = recv(connfd, cmd, BUFFER_SIZE, 0);
}
}
//关闭连接
// delete_shm(p_shm);
close(connfd);
}