基于树莓派的智能家居设计
文章目录
- 前言
- 一、整体系统框图
- 二、代码部分
- 二、APP客户端
- 1.MainActivity.java
- 2.NetUtils.java
- 三、实物展示
前言
一个简易的小项目,以下是代码部分和实物效果展示。
一、整体系统框图
二、代码部分
- main.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>#include "contrlDevices.h"
#include "inputCommand.h"pthread_t voiceThread; //注意:定义线程不使用指针以免空指针异常
pthread_t socketThread; //注意:不建议线程传参(链表头)所以定为全局变量
pthread_t fireThread;
pthread_t cameraThread;
pthread_t clientWemosThread;struct InputCommander *pCommandHead = NULL;
struct Devices *pdeviceHead = NULL;
struct InputCommander *socketHandler = NULL;
struct InputCommander *clientHandler = NULL;pthread_mutex_t mutex; //定义互斥量(锁)
//pthread_cond_t cond; //条件 int c_fd; //注意:涉及到多线程不要轻易的去传参//摄像头相关,改变返回值命名,因为C语言中没有这样的返回值
#define true 1
#define false 0
typedef unsigned int bool;
//char buf[1024] = {'\0'};char* faceRes = NULL;struct Devices *findEquipByName(char *name,struct Devices *phead) //查询设备
{if(phead == NULL){return NULL;}while(phead != NULL){if(strstr(phead->deviceName,name) != NULL){return phead;}phead = phead->next;}return NULL;
}struct InputCommander *findCommandByName(char *name,struct InputCommander *phead) //查询控制
{if(phead == NULL){return NULL;}while(phead != NULL){if(strcmp(phead->commandName,name) == 0){return phead;}phead = phead->next;}return NULL;
}//=======================================================摄像头======================================================================
void *camera_thread(void *datas) //摄像头线程
{const char* openCamera = "/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh";system(openCamera); //调用start.sh可执行文件
}
//=======================================================/end/======================================================================
//=======================================================人脸识别开锁=================================================================size_t handle(void *ptr, size_t size, size_t nmemb, void *stream){//拷贝返回来的结果字段int ssize = strlen(ptr) + 1;faceRes = (char*)malloc(ssize);memset(faceRes, '\0', ssize);strncpy(faceRes, ptr, ssize);
}
char* getBase64(char* photoPath){// 获取照片(jpg格式)的Base64的值char cmd[256] = {'\0'};sprintf(cmd, "base64 %s > photoBase64File", photoPath);system(cmd);// 通过执行"base64"这个指令即可得到照片的Base64值,在这里将得到的Base64值存放在photoBase64File文件中int fd,size;fd = open("./photoBase64File", O_RDWR);size = lseek(fd, 0, SEEK_END) + 1;lseek(fd, 0, SEEK_SET);char* res = (char*)malloc(size);memset(res, '\0', size);read(fd, res, size);// 从photoBase64File文件中读取照片的Base64值close(fd);system("rm photoBase64File");return res;
}
bool cameraContrlPostUrl()
{CURL *curl;CURLcode res;struct Devices *linkHandler = NULL;char* message = NULL;// 调用getBase64()自定的函数获取存放在当前文件夹下的两个进行识别的图片的Base64值char* img1 = getBase64("./host.jpg");/*************************添加的代码**************************************///openCamera为start.sh可执行文件的绝对地址,请根据自己的路径改//const char* openCamera = "/home/pi/mjpg-streamer/mjpg-streamer-experimental/start.sh";//system(openCamera); //调用start.sh可执行文件//获取访问者的照片system("wget http://192.168.137.104:8080/?action=snapshot -O ./visitor.jpg");/************************************************************************/char* img2 = getBase64("./visitor.jpg");// key值和secret值是在翔云官网->个人中心的OCR Key和OCR secret两个的值char* key = "QYRbM22W52hMrHaUWkdKEN";char* secret = "30e80f34e0b04a2f863ff4ff615c2661";int typeId = 21;char* format = "xml";int size = strlen(img1)+strlen(img2)+strlen(key)+strlen(secret)+strlen(format)+3;message = (char*)malloc(size);memset(message, '\0', size);sprintf(message, "&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1, img2, key, secret, typeId, format);curl = curl_easy_init(); // 初始化if (curl){curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");// 指定urlcurl_easy_setopt(curl, CURLOPT_POSTFIELDS, message);// 指定post内容curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle);// 当拿到结果后,指定调用handle()该自定的函数进行处理res = curl_easy_perform(curl);// 进行访问官网、进行人脸识别的操作curl_easy_cleanup(curl);// 执行完后,对curl_easy_init()进行清理printf("%s\n",faceRes);// 打印出人脸识别后返回来的结果字段printf("shibiechenggong\n");if(strstr(faceRes, "是") != NULL){// 如果返回来的结果字段中有“是”这个字眼,代表是同一个人printf("tongyigeren\n");linkHandler = findEquipByName("lock",pdeviceHead);linkHandler->open(linkHandler->pinNum);sleep(3);linkHandler->close(linkHandler->pinNum);}else{printf("bushitongyigeren\n");}printf("shibiejieshu\n");}free(faceRes);free(img1);free(img2);}//=======================================================/end人脸识别/================================================================//=======================================================语音模块=====================================================================void *voice_thread(void *arg) //语音线程
{int i = 0;int nread;struct InputCommander *voiceHandler = NULL;struct Devices *linkHandler = NULL;voiceHandler = findCommandByName("voice",pCommandHead); //在控制工厂找到语音模块if(voiceHandler == NULL){printf("find voiceHandler error\n");pthread_exit(NULL); }else{if(voiceHandler->Init(voiceHandler) < 0){ //语音模块初始化printf("voice init error\n");pthread_exit(NULL); //退出线程}else{printf("%s init success\n",voiceHandler->commandName);} //语音初始化完成//pthread_mutex_lock(&mutex); //加锁【有待研究】 //为什么加这个锁呢,我的想法是在语音读取一级指令的时候,为了避免其它线程对于 紧接着读取二级指令的干扰while(1){ memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));nread = voiceHandler->getCommand(voiceHandler); //读取来自语音模块的串口数据if(nread == 0){printf("noData from voice,please say again\n");}else{ //获取到指令printf("Get voice command:%s\n",voiceHandler->command);//以下为根据不用指令执行相应操作//语音模块串口传出来的后面带\r\n,不加对比不出来if(strcmp("kycd\r\n",voiceHandler->command) == 0){//开泳池灯linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->open(linkHandler->pinNum);printf("open bathroomlight\n");}if(strcmp("gycd\r\n",voiceHandler->command) == 0){//关泳池灯linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->close(linkHandler->pinNum);printf("已关闭浴室灯\n");}if(strcmp("kwsd\r\n",voiceHandler->command) == 0){//开卧室灯linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gwsd\r\n",voiceHandler->command) == 0){//关卧室灯linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("kktd\r\n",voiceHandler->command) == 0){//开客厅灯linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gktd\r\n",voiceHandler->command) == 0){//关客厅灯linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("kfs\r\n",voiceHandler->command) == 0){//开风扇linkHandler = findEquipByName("fan",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gfs\r\n",voiceHandler->command) == 0){//关风扇linkHandler = findEquipByName("fan",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("km\r\n",voiceHandler->command) == 0){//开门cameraContrlPostUrl(); //调用人脸识别//linkHandler = findEquipByName("lock",pdeviceHead);//linkHandler->open(linkHandler->pinNum);}if(strcmp("gm\r\n",voiceHandler->command) == 0){//关门linkHandler = findEquipByName("lock",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("kqbd\r\n",voiceHandler->command) == 0){//打开全部灯linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->open(linkHandler->pinNum);linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->open(linkHandler->pinNum);linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("face",voiceHandler->command) == 0){ //进行人脸识别//deviceTmp = findDeviceByName("face",pdeviceHead);//deviceTmp->cameraInit(deviceTmp);cameraContrlPostUrl(); //调用人脸识别}
/*//以下指令发送到wemos服务端if(strcmp("dkds\r\n",voiceHandler->command) == 0){//打开电视memset(clientHandler,'\0',sizeof(clientHandler));strcpy(clientHandler->command,"1");write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); }if(strcmp("gbds\r\n",voiceHandler->command) == 0){//关闭电视memset(clientHandler,'\0',sizeof(clientHandler));strcpy(clientHandler->command,"2");write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); }if(strcmp("dkkt\r\n",voiceHandler->command) == 0){//打开空调memset(clientHandler,'\0',sizeof(clientHandler));strcpy(clientHandler->command,"3");write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); }if(strcmp("gbkt\r\n",voiceHandler->command) == 0){//关闭空调memset(clientHandler,'\0',sizeof(clientHandler));strcpy(clientHandler->command,"4");write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); }*/}}//pthread_mutex_unlock(&mutex); //解锁}
}//========================================================/end语音/====================================================================//========================================================火焰警报=====================================================================
void *fire_thread(void *datas) //火灾线程
{int status;struct Devices *fireDeviceTmp = NULL;struct Devices *buzzerDeviceTmp = NULL;fireDeviceTmp = findEquipByName("fire",pdeviceHead); //在设备工厂找到火灾模块buzzerDeviceTmp = findEquipByName("buzzser",pdeviceHead);fireDeviceTmp->deviceInit(fireDeviceTmp->pinNum); //火灾模块初始化buzzerDeviceTmp->deviceInit(buzzerDeviceTmp->pinNum);printf("fire_thread init\n");while(1){delay(2000);status = fireDeviceTmp->readStatus(fireDeviceTmp->pinNum); //读取火灾模块实时数据//printf("fire get data = %d\n",status);if(status == 0){buzzerDeviceTmp->open(buzzerDeviceTmp->pinNum);}else{buzzerDeviceTmp->close(buzzerDeviceTmp->pinNum);}}
}//========================================================/end火焰/====================================================================//========================================================socket网络控制===============================================================
void *read_thread(void *datas) //通过socket读取客户端发来的数据
{int n_read;struct Devices *linkHandler = NULL;while(1){memset(socketHandler->command,'\0',sizeof(socketHandler->command));n_read = read(c_fd,socketHandler->command,sizeof(socketHandler->command)); //读取客户端发来的数据if(n_read == -1){perror("read_thread");}else if(n_read > 0){printf("getCommand:%s\n",socketHandler->command);//处理客户端读到的命令if(strcmp("kycd",socketHandler->command) == 0){linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gycd",socketHandler->command) == 0){linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("kwsd",socketHandler->command) == 0){linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gwsd",socketHandler->command) == 0){linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("kktd",socketHandler->command) == 0){linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gktd",socketHandler->command) == 0){linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("kfs",socketHandler->command) == 0){linkHandler = findEquipByName("fan",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gfs",socketHandler->command) == 0){linkHandler = findEquipByName("fan",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("km",socketHandler->command) == 0){linkHandler = findEquipByName("lock",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gm",socketHandler->command) == 0){linkHandler = findEquipByName("lock",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("kqbd",socketHandler->command) == 0){linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->open(linkHandler->pinNum);linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->open(linkHandler->pinNum);linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->open(linkHandler->pinNum);}if(strcmp("gqbd",socketHandler->command) == 0){linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->close(linkHandler->pinNum);linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->close(linkHandler->pinNum);linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->close(linkHandler->pinNum);}if(strcmp("gqbdq",socketHandler->command) == 0){linkHandler = findEquipByName("bedroomlight",pdeviceHead);linkHandler->close(linkHandler->pinNum);linkHandler = findEquipByName("yongchilight",pdeviceHead);linkHandler->close(linkHandler->pinNum);linkHandler = findEquipByName("livingroomLight",pdeviceHead);linkHandler->close(linkHandler->pinNum);linkHandler = findEquipByName("fan",pdeviceHead);linkHandler->close(linkHandler->pinNum);linkHandler = findEquipByName("lock",pdeviceHead);linkHandler->close(linkHandler->pinNum);}//if(strcmp("face",socketHandler->command) == 0){ //进行人脸识别//deviceTmp = findDeviceByName("face",pdeviceHead);//deviceTmp->cameraInit(deviceTmp);//cameraContrlPostUrl(); //调用人脸识别//// }}else{printf("client quit\n");exit(-1); //客户端退出,服务器程序退出//pthread_exit(NULL); //退出线程}}}void *socket_thread(void *datas) //开启socket服务端,并将socket服务端初始化
{int n_read = 0;pthread_t readPthread;struct sockaddr_in c_addr;memset(&c_addr,0,sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);socketHandler = findCommandByName("socketContrl",pCommandHead); //在控制工厂找到socketif(socketHandler == NULL){printf("find socketHandler error\n");pthread_exit(NULL); }else{printf("%s init success\n",socketHandler->commandName);}if(socketHandler->Init(socketHandler) < 0){ //“网络控制”功能初始化printf("socketControl init error\n");pthread_exit(NULL);} //socketHandler->Init(socketHandler,NULL,NULL); //初始化socketwhile(1){c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr, &clen);pthread_create(&readPthread,NULL,read_thread,NULL);}}//========================================================/end网络/====================================================================
//=========================================================/连接wemos服务端/============================================================
/*
void *clientWemos_Thread(void *datas) //连接wemos线程
{char *p;//struct InputCommander *clientHandler = NULL; //放到全局,因为我要在socket那里接收用户客户端client的数据,然后发给wemos//做客户端连接wemosD1服务器clientHandler = findCommandByName("client",pCommandHead); //在控制工厂找到客户连接端if(clientHandler == NULL){printf("find clientHandler error\n");exit(-1);}else{clientHandler->Init(clientHandler); //client初始化}while(1){ //获取服务端输入的命令数据,发送到wemos执行,这里只是调试作用,实际上我们需要接收的是客户端发来的数据。memset(clientHandler,'\0',sizeof(clientHandler));printf("input your contrl wemosD1 command::\n");fgets(clientHandler->command,sizeof(clientHandler->command),stdin); //不用gets,有警告if((p = strchr(clientHandler->command,'\n')) != NULL)*p = '\0'; //手动将\n位置处的值变为0write(clientHandler->sfd,clientHandler->command,strlen(clientHandler->command)); //向wemosD1发送数据}
}*///=========================================================/end/=======================================================================//========================================================主函数=======================================================================
int main()
{char name[32] = {'\0'};//树莓派库初始化if(wiringPiSetup() == -1){ printf("硬件接口初始化失败\n");return -1;}pthread_mutex_init(&mutex,NULL); //初始化互斥量(锁)//pthread_cond_init(&cond,NULL); //条件的创建(动态初始化)//1、指令工厂初始化pCommandHead = addVoiceContrlToInputCommanderLink(pCommandHead);pCommandHead = addsocketContrlToInputCommanderLink(pCommandHead);//pCommandHead = addclientContrlToInputCommanderLink(pCommandHead);//2、设备控制工程初始化pdeviceHead = addyongchilightToDeviceLink(pdeviceHead);pdeviceHead = addfanToDeviceLink(pdeviceHead);pdeviceHead = addlivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addbedroomlightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);pdeviceHead = addlockToDeviceLink(pdeviceHead);//pdeviceHead = addcameraContrlToDeviceLink(pdeviceHead);struct Devices *tmpequiphead = pdeviceHead;while(tmpequiphead != NULL){ //设备工厂所有设备初始化tmpequiphead->deviceInit(tmpequiphead->pinNum);tmpequiphead = tmpequiphead->next;}//int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);//3、线程池的建立//3.1、语音线程pthread_create(&voiceThread,NULL,voice_thread,NULL); //参数2:线程属性,一般设置为NULL,参数3:线程干活的函数,参数4:往voice_thread线程里面传送数据。//3.2、socket服务器线程pthread_create(&socketThread,NULL,socket_thread,NULL); //3.3、火灾线程pthread_create(&fireThread,NULL,fire_thread,NULL); //3.4、摄像头线程pthread_create(&cameraThread,NULL,camera_thread,NULL); //3.5、作为客户端连接wemosD1服务器//pthread_create(&clientWemosThread,NULL,clientWemos_Thread,NULL); //等待线程退出pthread_join(voiceThread,NULL); pthread_join(socketThread,NULL); pthread_join(fireThread,NULL); pthread_join(cameraThread,NULL); //pthread_join(clientWemosThread,NULL);pthread_mutex_destroy(&mutex); //销毁互斥量//pthread_cond_destroy(&cond); //条件的销毁return 0;//========================================================/end主函数/==================================================================}
- socketContrl.c
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>#include "inputCommand.h"int socketgetCommand(struct InputCommander *socketMes)
{int c_fd;int n_read;struct sockaddr_in c_addr;memset(&c_addr,0,sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);//4.accept c_fd = accept(socketMes->sfd,(struct sockaddr *)&c_addr, &clen);n_read = read(c_fd,socketMes->command,sizeof(socketMes->command));if(n_read == -1){perror("read");}else if(n_read > 0){printf("\nget:%d\n",n_read);}else{printf("client quit\n");}return n_read;
} int socketInit(struct InputCommander *socketMes)
{int s_fd;struct sockaddr_in s_addr;memset(&s_addr,0,sizeof(struct sockaddr_in)); //1.socket s_fd = socket(AF_INET,SOCK_STREAM,0);if(s_fd == -1){perror("socked");exit(-1);}s_addr.sin_family = AF_INET;s_addr.sin_port = htons(atoi(socketMes->port));inet_aton(socketMes->ipAdress,&s_addr.sin_addr);//2.bind bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));//3.listen listen(s_fd,10);printf("socket Server listening......\n");socketMes->sfd = s_fd;}struct InputCommander socketContrl = {.commandName = "socketContrl",.command = {'\0'},.port = "8085", .ipAdress = "192.168.137.104",.Init = socketInit,.getCommand = socketgetCommand,.log = {'\0'},.next = NULL
};struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead)
{ if(phead == NULL){return &socketContrl;}else{socketContrl.next = phead;phead = &socketContrl;}return phead;
}
- voiceContrl.c
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>
#include <string.h>#include "inputCommand.h"int getCommand(struct InputCommander *voicer)
{int nread = 0;memset(voicer->command,'\0',sizeof(voicer->command));nread = read(voicer->fd,voicer->command,sizeof(voicer->command));return nread;}int voiceInit(struct InputCommander *voicer)
{/*形参虽然定多了,用不上,咱不管*/int fd;if((fd = serialOpen(voicer->deviceName,9600)) == -1){exit(-1);}voicer->fd = fd;return fd;
}struct InputCommander voiceContrl = {.commandName = "voice",.deviceName = "/dev/ttyAMA0",//.boTelv = "9600";.command= {'\0'},.Init = voiceInit,.getCommand = getCommand,.log = {'\0'},.next = NULL
};struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead)
{ if(phead == NULL){return &voiceContrl;}else{voiceContrl.next = phead;phead = &voiceContrl;}return phead;
}
- bedroomlight.c
#include "contrlDevices.h"int bedroomlightOpen(int pinNum)
{digitalWrite (pinNum,LOW);
}int bedroomlightClose(int pinNum)
{digitalWrite (pinNum,HIGH);
} int bedroomlightCloseInit(int pinNum)
{pinMode (pinNum, OUTPUT);digitalWrite (pinNum,HIGH);
}int bedroomlightCloseStatus(int status)
{}struct Devices bedroomlight = {.deviceName = "bedroomlight",//.deviceName = "chu",.pinNum = 27,.open = bedroomlightOpen,.close = bedroomlightClose,.deviceInit = bedroomlightCloseInit,.changStatus = bedroomlightCloseStatus,.next = NULL
};struct Devices *addbedroomlightToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &bedroomlight;}else{bedroomlight.next = phead;phead = &bedroomlight;}return phead;
}
- buzzer.c
#include "contrlDevices.h"int buzzerOpen(int pinNum)
{digitalWrite (pinNum,LOW);
}int buzzerClose(int pinNum)
{digitalWrite (pinNum,HIGH);
}int buzzerInit(int pinNum)
{pinMode (pinNum, OUTPUT);digitalWrite (pinNum,HIGH);
}struct Devices buzzer = {.deviceName = "buzzser",.pinNum = 7,.open = buzzerOpen,.close = buzzerClose,.deviceInit = buzzerInit,.next = NULL
};struct Devices *addBuzzerToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &buzzer;}else{buzzer.next = phead;phead = &buzzer;}return phead;
}
- fan.c
#include "contrlDevices.h"int fanOpen(int pinNum)
{digitalWrite (pinNum,LOW);
}int fanClose(int pinNum)
{digitalWrite (pinNum,HIGH);
} int fanCloseInit(int pinNum)
{pinMode (pinNum, OUTPUT);digitalWrite (pinNum,HIGH);
}int fanCloseStatus(int status)
{}struct Devices fan = {.deviceName = "fan",//.deviceName = "chu",.pinNum = 28,.open = fanOpen,.close = fanClose,.deviceInit = fanCloseInit,.changStatus = fanCloseStatus,.next = NULL
};struct Devices *addfanToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &fan;}else{fan.next = phead;phead = &fan;}return phead;
}
- fire.c
#include "contrlDevices.h"int fireInit(int pinNum)
{pinMode (pinNum,INPUT);digitalWrite (pinNum,HIGH);
}int readFireStatus(int pinNum)
{return digitalRead(pinNum);
}struct Devices fire = {.deviceName = "fire",.pinNum = 25,.deviceInit = fireInit,.readStatus = readFireStatus,.next = NULL
};struct Devices *addFireToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &fire;}else{fire.next = phead;phead = &fire;}return phead;
}
- livingroomLight.c
#include "contrlDevices.h"int livingroomLightOpen(int pinNum)
{digitalWrite (pinNum,LOW);
}int livingroomLightClose(int pinNum)
{digitalWrite (pinNum,HIGH);
} int livingroomLightCloseInit(int pinNum)
{pinMode (pinNum, OUTPUT);digitalWrite (pinNum,HIGH);
}int livingroomLightCloseStatus(int status)
{}struct Devices livingroomLight2 = {.deviceName = "livingroomLight",//.deviceName = "shui",.pinNum = 26,.open = livingroomLightOpen,.close = livingroomLightClose,.deviceInit = livingroomLightCloseInit,.changStatus = livingroomLightCloseStatus,.next = NULL};struct Devices *addlivingroomLightToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &livingroomLight2;}else{livingroomLight2.next = phead;phead = &livingroomLight2;}return phead;
}
- lock.c
#include "contrlDevices.h"int lockOpen(int pinNum)
{digitalWrite (pinNum,HIGH);
}int lockClose(int pinNum)
{digitalWrite (pinNum,LOW);
} int lockCloseInit(int pinNum)
{pinMode (pinNum, OUTPUT);digitalWrite (pinNum,LOW);
}int lockCloseStatus(int status)
{}struct Devices lock = {.deviceName = "lock",.pinNum = 5,.open = lockOpen,.close = lockClose,.deviceInit = lockCloseInit,.changStatus = lockCloseStatus,.next = NULL
};struct Devices *addlockToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &lock;}else{lock.next = phead;phead = &lock;}return phead;
}
- yongchilight.c
#include "contrlDevices.h"int yongchilightOpen(int pinNum)
{digitalWrite (pinNum,LOW);
}int yongchilightClose(int pinNum)
{digitalWrite (pinNum,HIGH);
} int yongchilightCloseInit(int pinNum)
{pinMode (pinNum, OUTPUT);digitalWrite (pinNum,HIGH);
}int yongchilightCloseStatus(int status)
{}struct Devices yongchilight = {.deviceName = "yongchilight",//.deviceName = "yu",.pinNum = 29,.open = yongchilightOpen,.close = yongchilightClose,.deviceInit = yongchilightCloseInit,.changStatus = yongchilightCloseStatus,.next = NULL
};struct Devices *addyongchilightToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &yongchilight;}else{yongchilight.next = phead;phead = &yongchilight;}return phead;
}
- contrlDevices.h
#include <wiringPi.h>
#include <stdio.h>
#include <curl/curl.h>typedef unsigned int bool;struct Devices
{char deviceName[128]; //设备名int status; //读取到的数据int pinNum; //引脚号int (*open)(int pinNum); //打开设备函数指针int (*close)(int pinNum); //关闭设备函数指针int (*deviceInit)(int pinNum); //设备初始化函数指针int (*readStatus)(int pinNum); //读取数据函数指针int (*changStatus)(int status); //改变数据函数指针//摄像头相关的CURL *curl;char *key;char *secret;int typeId;char *format;bool (*cameraInit)(struct Devices *camera);int yesNum;int noNum;struct Devices *next;
};//每个设备加到链表函数的声明
struct Devices *addyongchilightToDeviceLink(struct Devices *phead);
struct Devices *addfanToDeviceLink(struct Devices *phead);
struct Devices *addlivingroomLightToDeviceLink(struct Devices * phead);
struct Devices *addlockToDeviceLink(struct Devices *phead);
struct Devices *addbedroomlightToDeviceLink(struct Devices *phead);
struct Devices *addFireToDeviceLink(struct Devices *phead);
struct Devices *addBuzzerToDeviceLink(struct Devices *phead);
struct Devices *addcameraContrlToDeviceLink(struct Devices *phead);
- inputCommand.h
#include <wiringPi.h>
#include <stdio.h>struct InputCommander
{char commandName[128]; //socket名char deviceName[128]; //串口名char command[32]; //控制命令int (*Init)(struct InputCommander *voicer); //socket初始化int (*getCommand)(struct InputCommander *voicer); //读取数据char log[1024];int fd;char port[12]; //端口号char ipAdress[32]; //IP地址int sfd;int cfd; struct InputCommander *next;
};//每个控制添加到控制链表的函数声明
struct InputCommander *addVoiceContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addsocketContrlToInputCommanderLink(struct InputCommander *phead);
struct InputCommander *addclientContrlToInputCommanderLink(struct InputCommander *phead);
二、APP客户端
参考博文:https://blog.csdn.net/bhbhhyg/article/details/115694774?spm=1001.2014.3001.5501
1.MainActivity.java
package com.example.smarthome;
import java.io.OutputStream;
import java.net.Socket;import android.os.Bundle;
import android.app.Activity;import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {Button kycd;Button gycd;Button kws;Button gws;Button kkt;Button gkt;Button kfs;Button gfs;Button km;Button gm;Button kqbd;Button gqbd;Button ljms;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);WebView wb = (WebView) findViewById(R.id.web);wb.setWebViewClient(new WebViewClient());String s="http://192.168.137.104:8080/?action=stream";wb.loadUrl(s);kycd=(Button)findViewById(R.id.kycd);gycd=(Button)findViewById(R.id.gycd);kws=(Button)findViewById(R.id.kws);gws=(Button)findViewById(R.id.gws);kkt=(Button)findViewById(R.id.kkt);gkt=(Button)findViewById(R.id.gkt);kfs=(Button)findViewById(R.id.kfs);gfs=(Button)findViewById(R.id.gfs);km=(Button)findViewById(R.id.km);gm=(Button)findViewById(R.id.gm);kqbd = (Button)findViewById(R.id.kqbd);gqbd = (Button)findViewById(R.id.gqbd); ljms=(Button)findViewById(R.id.ljms);kycd.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("kycd").sendMessage();}});gycd.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("gycd").sendMessage();}});kws.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("kwsd").sendMessage();}});gws.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("gwsd").sendMessage();}});kkt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("kktd").sendMessage();}});gkt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("gktd").sendMessage();}});kfs.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("kfs").sendMessage();}});gfs.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("gfs").sendMessage();}});km.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("km").sendMessage();}});gm.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("gm").sendMessage();}});kqbd.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("kqbd").sendMessage();}});gqbd.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("gqbd").sendMessage();}});ljms.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new NetUtils("gqbdq").sendMessage();}});}}
2.NetUtils.java
package com.example.smarthome;import java.io.OutputStream;
import java.net.Socket;import android.os.Handler;public class NetUtils {public String message;public String reTurnMes;public Handler handler;public NetUtils(String message) {this.message = message;}public NetUtils(String message, Handler handler) {this.message = message;this.handler = handler;}public void sendMessage() {new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {Socket client = new Socket("192.168.137.104",8085);//("192.168.137.166",8085);//Socket client = new Socket(StartActivity.IP, Integer.parseInt(StartActivity.Port));OutputStream out = client.getOutputStream();out.write(message.getBytes()); //out.close();//client.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}
}
三、实物展示
基于树莓派的智能家居设计相关推荐
- 基于树莓派的智能家居控制系统设计论文参考
完整论文咨询可WX联系:gyf1842965496 智能家居控制系统功能实现详细介绍:基于树莓派的智能家居控制系统设计https://blog.csdn.net/G1842965496/article ...
- python智能家居论文_毕业设计(论文)-基于树莓派的智能家居精选.docx
毕业设计(论文)-基于树莓派的智能家居精选 摘要随着物联网技术的发展,智能家居产业迅速崛起.在此背景下,我们研究了智能家居管理系统的设计与实现.本文所设计的智能家居管理系统采用分层架构设计,分别为感知 ...
- 基于树莓派的智能家居项目整理
文章目录 一.功能介绍 二.设计框图 三.实物展示 四.程序 一.功能介绍 基于树莓派的智能家居.智能家居用到的硬件有:树莓派4B.LD3320语音识别模块.pi 摄像头.继电器组.小灯.火焰传感器. ...
- python语音控制智能家电_基于树莓派的智能家居语音控制系统
2018-10 基于树莓派的智能家居语音控制系统 Intelligent home voice control system based on raspberry Pi 刘 华 , 田占生 , 冯宇飞 ...
- 【毕业设计之树莓派系列】基于树莓派的智能小车设计
基于树莓派的智能小车设计 摘要 随着人们对智能化生活的需求不断增长,智能小车的发展逐渐受到关注.然而,现有的智能小车硬件和软件设计有一定的局限性,需要进一步改进和完善.本文旨在基于树莓派PICO开发板 ...
- 基于树莓派的智能家居控制系统设计
基于树莓派的智能家居控制系统设计 完整文件下载 基于树莓派的智能家居控制系统设计 https://download.csdn.net/download/G1842965496/85802405 一.功 ...
- 【毕业设计】基于树莓派的智能小车设计 - 物联网 单片机 嵌入式 stm32
文章目录 1 简介 2 实现功能 主控开发板:树莓派 电机和控制器 避障功能 3 实现效果 5 部分实现代码 6 最后 1 简介 Hi,大家好,这里是丹成学长,今天向大家介绍一个单片机项目 ** 基于 ...
- 基于树莓派的智能家居
一点点摸索总结资料出来,所以希望记录下来,也能给其他的小伙伴有一点点参考.由于是自己摸索,所以可能说的不是很清楚或者显得很业余.后来还把小车和智能家居联合起来了,小车可以控制智能家居,智能家居也可以控 ...
- linux项目—基于树莓派的智能家居系统
1.项目简介 项目采用工厂设计模式.所有控制以及外设的设备都做成一个个对象(java思想),分别将命令控制的连成一个控制链表,外设设备做成一个外设设备的链表,这样做是为了方便以后功能模块的添加.其中为 ...
最新文章
- Ubuntu 下配置 SSH服务全过程及问题解决
- 畅谈程序人生暨孙鑫老师与读者交流会
- 最短路径 - 迪杰斯特拉(Dijkstra)算法
- 探索Julia(part3)--数据类型
- cocos2dx 2.0升级为3.0一些常见变化纪录
- HDOJ2027统计元音
- 【Anylogic智能体状态转移】
- 最新SSD固态硬盘颗粒QLC、SLC、MLC、TLC详解
- Excel 实现 平均数±标准差
- Dram学习笔记(1) Dram相关基础知识
- iOS视频通话问题总结及心路历程。。。
- 学python如何找工作
- 1348. 推文计数
- 华为eNSP模拟器的搭建
- 【LabVIEW懒人系列教程-小白入门】1.16LabVIEW程序结构之小试身手
- Oracle 11g安装报错You do not have sufficient permissions to access the inventory
- Arduino ESP8266 创建OneNet设备(二)
- 硬币找钱问题,求所有可能解决方案数目,最少的钱币数目,每种钱币用多少张
- 如何使用OpenCV进行图像的边缘检测和边缘增强?
- 私募股权公司TorQuest Partners收购Bartek Ingredients Inc.