Signed-off-by: 李彦亲 <李彦亲@163.com>
This commit is contained in:
parent
441ce0d4a7
commit
caa2e49a01
4
smart_greenhouse_sever/.vscode/settings.json
vendored
4
smart_greenhouse_sever/.vscode/settings.json
vendored
@ -1,7 +1,5 @@
|
|||||||
{
|
{
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
"*.S": "arm",
|
"server.h": "c"
|
||||||
"*.dts": "arm",
|
|
||||||
"signal.h": "c"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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= main.c server.c convert.c srv.c tcp.c sqlite.c sqlite3.c modul.c shm.c serial.c
|
||||||
OBJS= ./src/*.c
|
OBJS= ./src/*.c
|
||||||
CC=gcc
|
CC=gcc
|
||||||
TARGET=main
|
TARGET=smart_greenhouse
|
||||||
|
|
||||||
$(TARGET):$(OBJS)
|
$(TARGET):$(OBJS)
|
||||||
$(CC) $^ -o $@ -lpthread -lsqlite3 -ldl
|
$(CC) $^ -o $@ -lpthread -lsqlite3 -ldl
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -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 <stdio.h>
|
||||||
#include "../inc/server.h"
|
#include "../inc/server.h"
|
||||||
//#include "./include/camera_process.h"
|
//#include "../include/camera_process.h"
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|||||||
@ -5,19 +5,20 @@
|
|||||||
#include "../inc/serial.h"
|
#include "../inc/serial.h"
|
||||||
#include "../inc/server.h"
|
#include "../inc/server.h"
|
||||||
|
|
||||||
#define ZGB_DATA_SIZE 36
|
#define ZGB_DATA_SIZE 36
|
||||||
|
// #define ZGB_NWK_ID 0x02
|
||||||
//extern int zgbfd;
|
// extern int zgbfd;
|
||||||
extern char *p_shm;
|
extern char *p_shm;
|
||||||
extern char cmd_buf[1];
|
extern char cmd_buf[1];
|
||||||
extern float value[8];
|
extern float value[8];
|
||||||
//m0的环境变量
|
// m0的环境变量
|
||||||
struct env_char{
|
struct env_char
|
||||||
//char smog;
|
{
|
||||||
|
// char smog;
|
||||||
char led_one;
|
char led_one;
|
||||||
char led_two;
|
char led_two;
|
||||||
char fan;
|
char fan;
|
||||||
//char buzz;
|
// char buzz;
|
||||||
char h_tem;
|
char h_tem;
|
||||||
char l_tem;
|
char l_tem;
|
||||||
char h_hum;
|
char h_hum;
|
||||||
@ -32,8 +33,8 @@ struct env_char env_char_data;
|
|||||||
//读取共享内存数据
|
//读取共享内存数据
|
||||||
void read_cmd()
|
void read_cmd()
|
||||||
{
|
{
|
||||||
read_shm(p_shm+11,cmd_buf,1);
|
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()
|
/*void read_secure()
|
||||||
@ -42,40 +43,40 @@ void read_cmd()
|
|||||||
printf("p_shm = %.2f\r\n",p_shm[12]);
|
printf("p_shm = %.2f\r\n",p_shm[12]);
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
//zigbee初始化
|
// zigbee初始化
|
||||||
int zigbee_init(char *devpath, int baudrate)
|
int zigbee_init(char *devpath, int baudrate)
|
||||||
{
|
{
|
||||||
return serial_init(devpath);
|
return serial_init(devpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
//zigbee采集数据
|
// zigbee采集数据
|
||||||
int zigbee_get_dat(int fd)
|
int zigbee_get_dat(int fd)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
ssize_t ret;
|
ssize_t ret;
|
||||||
unsigned char buf[ZGB_DATA_SIZE];
|
unsigned char buf[ZGB_DATA_SIZE];
|
||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
//fprintf(stdout,"hi\n");
|
// fprintf(stdout,"tip\n");
|
||||||
ret = serial_recv_exact_nbytes(fd, buf, sizeof(buf));
|
ret = serial_recv_exact_nbytes(fd, buf, sizeof(buf));
|
||||||
//fprintf(stdout,"hi1\n");
|
// fprintf(stdout,"tips\n");
|
||||||
if (ret == sizeof(buf))
|
if (ret == sizeof(buf))
|
||||||
{
|
{
|
||||||
if (buf[0] == 0xBB)
|
if (buf[0] == 0xBB)
|
||||||
{
|
{
|
||||||
memset(&data, 0, sizeof(struct env));
|
memset(&data, 0, sizeof(struct env));
|
||||||
data.id = (int)buf[1];
|
data.id = (int)buf[1];
|
||||||
data.tem = (float)((buf[5]*10 + buf[4])/10) ;
|
data.tem = (float)((buf[5] * 10 + buf[4]) / 10);
|
||||||
data.hum = (float)((buf[7]*10 + buf[6])/10) ;
|
data.hum = (float)((buf[7] * 10 + buf[6]) / 10);
|
||||||
//data.smog = (int)buf[16];
|
// data.smog = (int)buf[16];
|
||||||
data.light = (int)((buf[23] << 24) + (buf[22] << 16) + (buf[21] << 8) + buf[20]);
|
data.light = (int)((buf[23] << 24) + (buf[22] << 16) + (buf[21] << 8) + buf[20]);
|
||||||
//printf("smog = %d\r\n",data.smog);
|
// printf("smog = %d\r\n",data.smog);
|
||||||
|
|
||||||
env_char_data.l_tem = buf[4];
|
env_char_data.l_tem = buf[4];
|
||||||
env_char_data.h_tem = buf[5];
|
env_char_data.h_tem = buf[5];
|
||||||
env_char_data.l_hum = buf[6];
|
env_char_data.l_hum = buf[6];
|
||||||
env_char_data.h_hum = buf[7];
|
env_char_data.h_hum = buf[7];
|
||||||
//env_char_data.smog = buf[16]; //烟雾浓度
|
// env_char_data.smog = buf[16]; //烟雾浓度
|
||||||
//printf("smog = %d\r\n",(int)buf[16]);
|
// printf("smog = %d\r\n",(int)buf[16]);
|
||||||
env_char_data.first_light = buf[20];
|
env_char_data.first_light = buf[20];
|
||||||
env_char_data.second_light = buf[21];
|
env_char_data.second_light = buf[21];
|
||||||
env_char_data.third_light = buf[22];
|
env_char_data.third_light = buf[22];
|
||||||
@ -83,102 +84,123 @@ int zigbee_get_dat(int fd)
|
|||||||
|
|
||||||
data.led = buf[24];
|
data.led = buf[24];
|
||||||
data.fan = buf[25];
|
data.fan = buf[25];
|
||||||
//data.buzz = buf[26];
|
// data.buzz = buf[26];
|
||||||
|
|
||||||
|
|
||||||
env_char_data.led_one = buf[24];
|
env_char_data.led_one = buf[24];
|
||||||
env_char_data.fan = buf[25];
|
env_char_data.fan = buf[25];
|
||||||
//env_char_data.buzz = buf[26];
|
// env_char_data.buzz = buf[26];
|
||||||
env_char_data.led_two = buf[27];
|
env_char_data.led_two = buf[27];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
//zigbee发送命令
|
// zigbee发送命令
|
||||||
int zigbee_exe_cmd(int fd, unsigned char *p)
|
int zigbee_exe_cmd(int fd, unsigned char *p)
|
||||||
{
|
{
|
||||||
unsigned char buf[ZGB_DATA_SIZE] = {0xdd, 0x06, 0x24, 0x00, 0x00};
|
unsigned char buf[ZGB_DATA_SIZE] = {0xdd, data.id, 0x24, 0x00, 0x00};
|
||||||
if (strstr(p, "LIGHT_ON") != NULL){
|
if (strstr(p, "LIGHT_ON") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x00;
|
buf[4] = 0x00;
|
||||||
} else if (strstr(p, "LIGHT_OFF") != NULL){
|
}
|
||||||
|
else if (strstr(p, "LIGHT_OFF") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x01;
|
buf[4] = 0x01;
|
||||||
} else if (strstr(p, "BUZZ_ON") != NULL){
|
}
|
||||||
|
else if (strstr(p, "BUZZ_ON") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x02;
|
buf[4] = 0x02;
|
||||||
} else if (strstr(p, "BUZZ_OFF") != NULL){
|
}
|
||||||
|
else if (strstr(p, "BUZZ_OFF") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x03;
|
buf[4] = 0x03;
|
||||||
} else if (strstr(p, "FAN_ON") != NULL){
|
}
|
||||||
|
else if (strstr(p, "FAN_ON") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x04;
|
buf[4] = 0x04;
|
||||||
} else if (strstr(p, "FAN_OFF") != NULL){
|
}
|
||||||
|
else if (strstr(p, "FAN_OFF") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x08;
|
buf[4] = 0x08;
|
||||||
} else if (strstr(p, "SHU_ON") != NULL){
|
}
|
||||||
|
else if (strstr(p, "SHU_ON") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x09;
|
buf[4] = 0x09;
|
||||||
} else if (strstr(p, "SHU_OFF") != NULL){
|
}
|
||||||
|
else if (strstr(p, "SHU_OFF") != NULL)
|
||||||
|
{
|
||||||
buf[4] = 0x0a;
|
buf[4] = 0x0a;
|
||||||
}
|
}
|
||||||
int ret = serial_send_exact_nbytes(fd, buf, sizeof(buf));
|
int ret = serial_send_exact_nbytes(fd, buf, sizeof(buf));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
//AP发送给m0命令
|
//发送给m0命令
|
||||||
void send_cmd_m0(int signum)
|
void send_cmd_m0(int signum)
|
||||||
{
|
{
|
||||||
read_cmd();
|
read_cmd();
|
||||||
if(cmd_buf[0] == 1)
|
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
|
zigbee_exe_cmd(zgbfd, "LIGHT_ON"); // led1
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(cmd_buf[0] == 2)
|
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
|
zigbee_exe_cmd(zgbfd, "LIGHT_OFF"); // led1
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(cmd_buf[0] == 3)
|
else if (cmd_buf[0] == 3)
|
||||||
{
|
{
|
||||||
printf("Bedroom now:ON = %d\r\n",cmd_buf[0]);
|
printf("数码管:ON = %d\r\n", cmd_buf[0]);
|
||||||
zigbee_exe_cmd(zgbfd,"SHU_ON"); //led2
|
zigbee_exe_cmd(zgbfd, "SHU_ON"); //数码管
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(cmd_buf[0] == 4)
|
else if (cmd_buf[0] == 4)
|
||||||
{
|
{
|
||||||
printf("Bedroom now:OFF = %d\r\n",cmd_buf[0]);
|
printf("数码管:OFF = %d\r\n", cmd_buf[0]);
|
||||||
zigbee_exe_cmd(zgbfd,"SHU_OFF"); //led2
|
zigbee_exe_cmd(zgbfd, "SHU_OFF"); //数码管
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(cmd_buf[0] == 5)
|
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
|
zigbee_exe_cmd(zgbfd, "FAN_ON"); // fan
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(cmd_buf[0] == 6)
|
else if (cmd_buf[0] == 6)
|
||||||
{
|
{
|
||||||
printf("Aircondition now:OFF%d\r\n",cmd_buf[0]);
|
zigbee_exe_cmd(zgbfd, "FAN_OFF"); // fan
|
||||||
zigbee_exe_cmd(zgbfd,"FAN_OFF"); //fan
|
printf("风扇:OFF=%d\r\n", cmd_buf[0]);
|
||||||
printf("Aircondition now: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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*void send_alarm_m0(int signo)
|
/* void send_alarm_m0(int signo)
|
||||||
{
|
{
|
||||||
read_secure();
|
read_secure();
|
||||||
printf("data_tem = %f\r\n",data.tem);
|
printf("data_tem = %f\r\n", data.tem);
|
||||||
printf("value[1] = %.2f\r\n",value[1]);
|
printf("value[1] = %.2f\r\n", value[1]);
|
||||||
printf("value[0] = %.2f\r\n",value[0]);
|
printf("value[0] = %.2f\r\n", value[0]);
|
||||||
if(data.tem < value[0] || data.tem > value[1] || data.hum < value[2] || data.hum > value[3] || data.light < value[4] || data.light > value[5])
|
if (data.tem < value[0] || data.tem > value[1] || data.hum < value[2] || data.hum > value[3] || data.light < value[4] || data.light > value[5])
|
||||||
zigbee_exe_cmd(zgbfd,"BUZZ_ON");
|
zigbee_exe_cmd(zgbfd, "BUZZ_ON");
|
||||||
else
|
else
|
||||||
zigbee_exe_cmd(zgbfd,"BUZZ_OFF");
|
zigbee_exe_cmd(zgbfd, "BUZZ_OFF");
|
||||||
}*/
|
}
|
||||||
|
*/
|
||||||
//zigbee退出
|
// zigbee退出
|
||||||
int zigbee_exit(int fd)
|
int zigbee_exit(int fd)
|
||||||
{
|
{
|
||||||
|
//处理申请的内存等
|
||||||
return serial_exit(fd);
|
return serial_exit(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -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)
|
int serial_init(char *devpath)
|
||||||
{
|
{
|
||||||
printf("串口设备地址:%s\n",devpath);
|
// printf("串口设备地址:%s\n",devpath);
|
||||||
int fd_comport = FALSE;
|
int fd_comport = FALSE;
|
||||||
//打开串口
|
//打开串口
|
||||||
if((fd_comport=serial_open(devpath))<0)
|
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);
|
assert(buf != NULL);
|
||||||
|
|
||||||
while (total != count) {
|
while (total != count) {
|
||||||
fprintf(stdout,"%d\n",fd);
|
// fprintf(stdout,"%d\n",fd);
|
||||||
fprintf(stdout,"hi..\n");
|
// fprintf(stdout,"hi..\n");
|
||||||
ret = read(fd, buf + total, count - total);
|
ret = read(fd, buf + total, count - total);
|
||||||
/*int i = 0;
|
/*int i = 0;
|
||||||
for (i = 0; i < 36; i++){
|
for (i = 0; i < 36; i++){
|
||||||
printf("%.2x ", buf1[i]);
|
printf("%.2x ", buf1[i]);
|
||||||
}*/
|
}*/
|
||||||
fprintf(stdout,"hi2\n");
|
// fprintf(stdout,"hi2\n");
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
{
|
{
|
||||||
perror("serial->recv");
|
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;
|
ssize_t total = 0;
|
||||||
|
|
||||||
assert(buf != NULL);
|
assert(buf != NULL);
|
||||||
|
//循环打印一下
|
||||||
//int i = 0;
|
//int i = 0;
|
||||||
/*for (i = 0; i < 36; i++){
|
/*for (i = 0; i < 36; i++){
|
||||||
printf("%.2x ", buf[i]);
|
printf("%.2x ", buf[i]);
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
@ -17,7 +16,7 @@
|
|||||||
#include "../inc/serial.h"
|
#include "../inc/serial.h"
|
||||||
|
|
||||||
#define SPORT 7777
|
#define SPORT 7777
|
||||||
#define LISNUM 1024
|
#define LISNUM 1024
|
||||||
#define BUFFER_SIZE 128
|
#define BUFFER_SIZE 128
|
||||||
char env_buf[12];
|
char env_buf[12];
|
||||||
char cmd_buf[5];
|
char cmd_buf[5];
|
||||||
@ -30,40 +29,40 @@ char get_history_data[72][25];
|
|||||||
float value[8] = {0};
|
float value[8] = {0};
|
||||||
void read_env(int signo)
|
void read_env(int signo)
|
||||||
{
|
{
|
||||||
read_shm(p_shm,env_buf,sizeof(env_char_data));
|
read_shm(p_shm, env_buf, sizeof(env_char_data));
|
||||||
}
|
}
|
||||||
|
|
||||||
int service_process()
|
int service_process()
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char led1,led2,fan;
|
char led1, led2, fan , buzz_l;
|
||||||
int socketId,connfd = 0;
|
int socketId, connfd = 0;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int recv_len = 0;
|
int recv_len = 0;
|
||||||
struct sockaddr_in serveraddr,clientaddr;
|
struct sockaddr_in serveraddr, clientaddr;
|
||||||
socklen_t server_len,client_len;
|
socklen_t server_len, client_len;
|
||||||
|
|
||||||
char *buf_ope = NULL;
|
char *buf_ope = NULL;
|
||||||
char *s = "//";
|
char *s = "//";
|
||||||
char cmd[BUFFER_SIZE] = {0};
|
char cmd[BUFFER_SIZE] = {0};
|
||||||
pid_t pid,pid_zigbee;
|
pid_t pid, pid_zigbee;
|
||||||
int signo = 0;
|
int signo = 0;
|
||||||
|
|
||||||
char *pPath = NULL;
|
char *pPath = NULL;
|
||||||
struct sqlite3 *pdb = NULL;
|
struct sqlite3 *pdb = NULL;
|
||||||
char dbName[] = "./mysqlite";
|
char dbName[] = "./mysqlite";
|
||||||
char tabname1[] = "rt_data";
|
char tabname1[] = "rt_data";
|
||||||
char tabname2[] = "max_data";
|
char tabname2[] = "max_data";
|
||||||
//获取pPath
|
//获取pPath
|
||||||
pPath = getPath(dbName);
|
pPath = getPath(dbName);
|
||||||
//open or create db
|
// open or create db
|
||||||
pdb = SqlOpen(pPath);
|
pdb = SqlOpen(pPath);
|
||||||
SqlCreatTable(pdb,tabname1);
|
SqlCreatTable(pdb, tabname1);
|
||||||
SqlCreatTable1(pdb,tabname2);
|
SqlCreatTable1(pdb, tabname2);
|
||||||
|
|
||||||
//创建zigbee进程
|
//创建zigbee进程
|
||||||
pid_zigbee = fork();
|
pid_zigbee = fork();
|
||||||
if(pid_zigbee < 0)
|
if (pid_zigbee < 0)
|
||||||
{
|
{
|
||||||
perror("zigbee process fail!");
|
perror("zigbee process fail!");
|
||||||
return -1;
|
return -1;
|
||||||
@ -72,37 +71,41 @@ int service_process()
|
|||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char buf[1] = {'\0'};
|
char buf[1] = {'\0'};
|
||||||
AA: zgbfd = serial_init("/dev/ttyUSB0");
|
|
||||||
|
//等待打开串口设备
|
||||||
|
AA:
|
||||||
|
zgbfd = serial_init("/dev/ttyUSB0");
|
||||||
if (zgbfd == -1)
|
if (zgbfd == -1)
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(2);
|
||||||
goto AA;
|
goto AA;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
fprintf(stdout, "init zigbee success\n");
|
fprintf(stdout, "init zigbee success\n");
|
||||||
|
|
||||||
//创建共享内存
|
//创建共享内存(ipcs -m ,ipcrm -m shmid ,共享内存查看管理 )
|
||||||
p_shm = create_shm();
|
p_shm = create_shm();
|
||||||
while(1)
|
while (1)
|
||||||
{
|
{
|
||||||
//send_alarm_m0(signo);
|
// send_alarm_m0(signo);
|
||||||
send_cmd_m0(signo);
|
send_cmd_m0(signo);
|
||||||
ret = zigbee_get_dat(zgbfd);
|
ret = zigbee_get_dat(zgbfd);
|
||||||
if(ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
perror("env_recv:");
|
perror("env_recv:");
|
||||||
}
|
}
|
||||||
SqlInsert(pdb,tabname1,data.tem,data.hum,data.light,0);
|
SqlInsert(pdb, tabname1, data.tem, data.hum, data.light, 0);
|
||||||
//sqlSelected(tabname1,pdb);
|
// sqlSelected(tabname1,pdb);
|
||||||
//写入共享内存
|
//写入共享内存
|
||||||
write_shm(p_shm,&env_char_data,sizeof(env_char_data));
|
write_shm(p_shm, &env_char_data, sizeof(env_char_data));
|
||||||
signal(SIGUSR2,read_env);
|
//进程间通信信号
|
||||||
sleep(2);
|
signal(SIGUSR2, read_env);
|
||||||
}
|
sleep(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//建立socket连接
|
//建立socket连接
|
||||||
socketId = socket(AF_INET,SOCK_STREAM,0);
|
socketId = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if(socketId < 0)
|
if (socketId < 0)
|
||||||
{
|
{
|
||||||
perror("create socket error");
|
perror("create socket error");
|
||||||
return -1;
|
return -1;
|
||||||
@ -111,14 +114,14 @@ int service_process()
|
|||||||
|
|
||||||
//设置sockaddr_in结构体的相关参数
|
//设置sockaddr_in结构体的相关参数
|
||||||
server_len = sizeof(struct sockaddr_in);
|
server_len = sizeof(struct sockaddr_in);
|
||||||
memset(&serveraddr,0,server_len);
|
memset(&serveraddr, 0, server_len);
|
||||||
serveraddr.sin_family = AF_INET;
|
serveraddr.sin_family = AF_INET;
|
||||||
serveraddr.sin_port = htons(SPORT);
|
serveraddr.sin_port = htons(SPORT);
|
||||||
serveraddr.sin_addr.s_addr = INADDR_ANY;
|
serveraddr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
//绑定
|
//绑定
|
||||||
ret = bind(socketId,(struct sockaddr *)&serveraddr,server_len);
|
ret = bind(socketId, (struct sockaddr *)&serveraddr, server_len);
|
||||||
if(ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
perror("bind error");
|
perror("bind error");
|
||||||
return -1;
|
return -1;
|
||||||
@ -126,8 +129,8 @@ int service_process()
|
|||||||
printf("bind succeed!\r\n");
|
printf("bind succeed!\r\n");
|
||||||
|
|
||||||
//建立监听队列
|
//建立监听队列
|
||||||
ret = listen(socketId,LISNUM);
|
ret = listen(socketId, LISNUM);
|
||||||
if(ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
perror("listen error");
|
perror("listen error");
|
||||||
return -1;
|
return -1;
|
||||||
@ -136,167 +139,194 @@ int service_process()
|
|||||||
|
|
||||||
//等待客户端的连接(并发服务器)
|
//等待客户端的连接(并发服务器)
|
||||||
client_len = sizeof(clientaddr);
|
client_len = sizeof(clientaddr);
|
||||||
memset(&clientaddr,0,client_len);
|
memset(&clientaddr, 0, client_len);
|
||||||
signal(SIGCHLD,SIG_IGN);
|
signal(SIGCHLD, SIG_IGN);
|
||||||
while(1)
|
while (1)
|
||||||
{
|
{
|
||||||
connfd = accept(socketId,(struct sockaddr *)&clientaddr,&client_len);
|
connfd = accept(socketId, (struct sockaddr *)&clientaddr, &client_len);
|
||||||
if(connfd < 0)
|
if (connfd < 0)
|
||||||
{
|
{
|
||||||
perror("connected error");
|
perror("connected error");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
printf("connected succeed!\r\n");
|
printf("connected succeed!\r\n");
|
||||||
|
|
||||||
|
|
||||||
//通信
|
//通信
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if(pid < 0)
|
if (pid < 0)
|
||||||
{
|
{
|
||||||
perror("create process error");
|
perror("create process error");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(pid == 0)
|
if (pid == 0)
|
||||||
{
|
{
|
||||||
recv_len = recv(connfd,cmd,BUFFER_SIZE,0);
|
recv_len = recv(connfd, cmd, BUFFER_SIZE, 0);
|
||||||
while(recv_len > 0)
|
while (recv_len > 0)
|
||||||
{
|
{
|
||||||
|
printf("套接字数据:%s\n", cmd);
|
||||||
//查询历史数据
|
//查询历史数据
|
||||||
if(0 == strncmp(cmd,"SearchHistoryData",17))
|
if (0 == strncmp(cmd, "SearchHistoryData", 17))
|
||||||
{
|
{
|
||||||
//p_shm = create_shm();
|
// p_shm = create_shm();
|
||||||
//read_env(signo);
|
// read_env(signo);
|
||||||
//send(connfd,env_buf,sizeof(env_buf),0);
|
// 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++)
|
for (i = 0; i < 11; i++)
|
||||||
{
|
{
|
||||||
printf("buf = %d\r\n",env_buf[i]);
|
printf("buf = %d\r\n", env_buf[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//设置安全阈值
|
//设置安全阈值
|
||||||
else if(0 == strncmp(cmd,"SetSecureValue:",15))
|
else if (0 == strncmp(cmd, "SetSecureValue:", 15))
|
||||||
{
|
{
|
||||||
//p_shm = create_shm();
|
// p_shm = create_shm();
|
||||||
buf_ope = strtok(cmd+15,s);
|
buf_ope = strtok(cmd + 15, s);
|
||||||
while(buf_ope != NULL)
|
while (buf_ope != NULL)
|
||||||
{
|
{
|
||||||
value[i] = atof(buf_ope);
|
value[i] = atof(buf_ope);
|
||||||
printf("value = %.2f\r\n",value[i]);
|
printf("value = %.2f\r\n", value[i]);
|
||||||
i++;
|
i++;
|
||||||
buf_ope = strtok(NULL,s);
|
buf_ope = strtok(NULL, s);
|
||||||
}
|
}
|
||||||
/*write_shm(p_shm,value,sizeof(value)/sizeof(value[0]));
|
/*write_shm(p_shm,value,sizeof(value)/sizeof(value[0]));
|
||||||
signal(SIGUSR1,send_alarm_m0);
|
signal(SIGUSR1,send_alarm_m0);
|
||||||
printf("value[1] = %.2f\r\n",value[1]);
|
printf("value[1] = %.2f\r\n",value[1]);
|
||||||
printf("p_shm[1] = %.2f\r\n",p_shm[1]);*/
|
printf("p_shm[1] = %.2f\r\n",p_shm[1]);*/
|
||||||
//将阈值插入到数据库
|
//将阈值插入到数据库
|
||||||
SqlInsert1(pdb,tabname2,*value,*(value+1),*(value+2),*(value+3),*(value+4),*(value+5),*(value+6),*(value+7));
|
SqlInsert1(pdb, tabname2, *value, *(value + 1), *(value + 2), *(value + 3), *(value + 4), *(value + 5), *(value + 6), *(value + 7));
|
||||||
//sqlSelected(tabname2,pdb);
|
// sqlSelected(tabname2,pdb);
|
||||||
}
|
}
|
||||||
|
|
||||||
//解析到是主界面
|
//解析到是主界面
|
||||||
else if(strncmp(cmd,"ThisIsWidget",12))
|
else if (0 == strncmp(cmd, "ThisIsWidget", 12))
|
||||||
{
|
{
|
||||||
p_shm = create_shm();
|
p_shm = create_shm();
|
||||||
read_env(signo);
|
read_env(signo);
|
||||||
send(connfd,env_buf,sizeof(env_buf),0);
|
send(connfd, env_buf, sizeof(env_buf), 0);
|
||||||
printf("cmd = %s\r\n",cmd);
|
printf("主界面刷新\r\n");
|
||||||
for(i = 0; i < 11; i++)
|
// for (i = 0; i < 11; i++)
|
||||||
printf("buf = %d\r\n",env_buf[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));
|
memset(cmd, 0, sizeof(cmd));
|
||||||
strncpy(cmd,"Livingroom:open",15);
|
strncpy(cmd, "Lamp:open", 10);
|
||||||
send(connfd,cmd,sizeof(cmd),0);
|
// printf("cmdlyq: %s\r\n", cmd);
|
||||||
|
// lyq send(connfd, cmd, sizeof(cmd), 0);
|
||||||
led1 = 1;
|
led1 = 1;
|
||||||
write_shm(p_shm+11,&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);
|
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();
|
// p_shm = create_shm();
|
||||||
printf("cmd = %s\r\n",cmd);
|
// printf("cmd = %s\r\n", cmd);
|
||||||
memset(cmd,0,sizeof(cmd));
|
memset(cmd, 0, sizeof(cmd));
|
||||||
strncpy(cmd,"Livingroom:close",16);
|
strncpy(cmd, "Lamp:close\n", 16);
|
||||||
send(connfd,cmd,sizeof(cmd),0);
|
// lyq send(connfd, cmd, sizeof(cmd), 0);
|
||||||
led1 = 2;
|
led1 = 2;
|
||||||
write_shm(p_shm+11,&led1,1);
|
write_shm(p_shm + 11, &led1, 1);
|
||||||
printf("p_shm = %d\r\n",p_shm[11]);
|
printf("p_shm = %d\r\n", p_shm[11]);
|
||||||
signal(SIGUSR1,send_cmd_m0);
|
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();
|
// p_shm = create_shm();
|
||||||
printf("cmd = %s\r\n",cmd);
|
// printf("cmd = %s\r\n", cmd);
|
||||||
memset(cmd,0,sizeof(cmd));
|
memset(cmd, 0, sizeof(cmd));
|
||||||
strncpy(cmd,"Bedroom:open",10);
|
strncpy(cmd, "TemCtl:open", 12);
|
||||||
send(connfd,cmd,sizeof(cmd),0);
|
// lyq send(connfd, cmd, sizeof(cmd), 0);
|
||||||
led2 = 3;
|
led2 = 3;
|
||||||
write_shm(p_shm+11,&led2,1);
|
write_shm(p_shm + 11, &led2, 1);
|
||||||
printf("p_shm = %d\r\n",p_shm[11]);
|
printf("p_shm = %d\r\n", p_shm[11]);
|
||||||
signal(SIGUSR1,send_cmd_m0);
|
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();
|
// p_shm = create_shm();
|
||||||
printf("cmd = %s\r\n",cmd);
|
// printf("cmd = %s\r\n", cmd);
|
||||||
memset(cmd,0,sizeof(cmd));
|
memset(cmd, 0, sizeof(cmd));
|
||||||
strncpy(cmd,"Bedroom:close",13);
|
strncpy(cmd, "Bedroom:close", 13);
|
||||||
send(connfd,cmd,sizeof(cmd),0);
|
// lyq send(connfd, cmd, sizeof(cmd), 0);
|
||||||
led2 = 4;
|
led2 = 4;
|
||||||
write_shm(p_shm+11,&led2,1);
|
write_shm(p_shm + 11, &led2, 1);
|
||||||
printf("p_shm = %d\r\n",p_shm[11]);
|
printf("p_shm = %d\r\n", p_shm[11]);
|
||||||
signal(SIGUSR1,send_cmd_m0);
|
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();
|
// p_shm = create_shm();
|
||||||
printf("cmd = %s\r\n",cmd);
|
// printf("cmd = %s\r\n", cmd);
|
||||||
memset(cmd,0,sizeof(cmd));
|
memset(cmd, 0, sizeof(cmd));
|
||||||
strncpy(cmd,"Aircondition:open",17);
|
strncpy(cmd, "Fan:open", 9);
|
||||||
send(connfd,cmd,sizeof(cmd),0);
|
// lyq send(connfd, cmd, sizeof(cmd), 0);
|
||||||
fan = 5;
|
fan = 5;
|
||||||
write_shm(p_shm+11,&fan,1);
|
write_shm(p_shm + 11, &fan, 1);
|
||||||
printf("p_shm = %d\r\n",p_shm[11]);
|
printf("p_shm = %d\r\n", p_shm[11]);
|
||||||
signal(SIGUSR1,send_cmd_m0);
|
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();
|
// p_shm = create_shm();
|
||||||
printf("cmd = %s\r\n",cmd);
|
// printf("cmd = %s\r\n", cmd);
|
||||||
memset(cmd,0,sizeof(cmd));
|
memset(cmd, 0, sizeof(cmd));
|
||||||
strncpy(cmd,"Aircondition:close",18);
|
strncpy(cmd, "Fan:close", 10);
|
||||||
send(connfd,cmd,sizeof(cmd),0);
|
// lyq send(connfd, cmd, sizeof(cmd), 0);
|
||||||
fan = 6;
|
fan = 6;
|
||||||
write_shm(p_shm+11,&fan,1);
|
write_shm(p_shm + 11, &fan, 1);
|
||||||
printf("p_shm = %d\r\n",p_shm[11]);
|
printf("p_shm = %d\r\n", p_shm[11]);
|
||||||
signal(SIGUSR1,send_cmd_m0);
|
signal(SIGUSR1, send_cmd_m0);
|
||||||
}
|
}
|
||||||
recv_len = recv(connfd,cmd,BUFFER_SIZE,0);
|
//解析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);
|
close(connfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
//关闭socket
|
//关闭socket
|
||||||
close(socketId);
|
close(socketId);
|
||||||
free(pPath);
|
free(pPath);
|
||||||
pPath = NULL;
|
pPath = NULL;
|
||||||
pdb = NULL;
|
pdb = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user