基于树莓派的智能家居项目整理

本文介绍了基于树莓派的智能家居项目,利用语音识别模块、摄像头、继电器和传感器实现语音控制灯光、火灾报警、视频监控及人脸识别开锁等功能。项目采用简单工厂模式设计,易于扩展,使用C语言实现,并通过HTTP和HTTPS协议与外部交互。
摘要由CSDN通过智能技术生成

文章目录


一、功能介绍
二、设计框图
三、实物展示
四、程序

一、功能介绍
基于树莓派的智能家居。智能家居用到的硬件有:树莓派4B、LD3320语音识别模块、pi 摄像头、继电器组、小灯、火焰传感器、蜂鸣器、电磁锁
采用了简单工厂模式的一个设计方式。稳定,拓展性更强,在C语言中,因为没有接口、类这一说法,所以这里采用了结构体来“等效替换”。有四个灯,所以我创建了四个灯控制.c程序。每一个程序文件中,都有一个设备结构体,每个程序文件的函数实现方法不同,当有新设备进入只需要在创建一个.c文件,改变函数实现方法即可。初始化的时候,通过链表将各个模块连接起来(头插法)。在要使用某个模块时,只需要使用链表遍历,找到所需模块去调用功能
具体功能是:
1、可通过ld3320语音模块的口令模式,口令+具体控制,通过串口把控制指令传给树莓派,来控制客厅、餐厅、二楼、浴室的灯
2、也可以通过socket客户端来发指令来控制灯的开关
3、火灾报警,当火焰传感器检测到火焰的时候,蜂鸣器会报警。
4、视频监控采用开源mjpg-Streamer来实现的,程序执行时创建一个视频监控的线程,用system函数调用启动脚本运行,监控画面可在http://172.20.10.8:8080去看到
5、人脸识别开锁,人脸识别功能是使用的翔云平台的人脸识别解决方案,需要安装libcurl 和 openSSl库来支持https协议,通过系统调用wget +http://172.20.10.8:8080/?action=snapshot -O ./huyu1.jpg 指令到树莓派的监控页面"去截取一帧保存到本地,获取图片的base64编码,工程文件夹下也有一张照片,huyu.jpg格式,相当于采集的人脸。也是获取图片的base64编码,通过sprintf函数将访问翔云需要的两张图片的base64编码与Key、secret、typeId、format拼接在一起,通过https协议去访问翔云平台, 识别成功后会将识别结果返回,通过回调函数readData将返回的字符串读到readBuff里,通过strstr去readbuff里找有没有字符’是’,如果识别成功就去控制电磁锁打开。

二、设计框图
在这里插入图片描述
*效果展示:*https://www.bilibili.com/video/BV1uD4y1C7PH?share_source=copy_web&vd_source=5e457ac82f63b0e36b8b9e2708807f31

三、实物展示
请添加图片描述
请添加图片描述
请添加图片描述
四、程序

1、InputCommand.h

#include <wiringPi.h>
#include <stddef.h>

struct InputCommander
{
   
	char commandName[128];
	char deviceName[128];
	char command[32];
	int (*Init)(struct InputCommander *voicer,char *ipAdress,char *port);
	int (*getCommand)(struct InputCommander *voicer);
	char log[1024];
	int fd;
	char port[12];
	char ipAddress[32];
	int sfd;
	struct InputCommander *next;
};

struct InputCommander *addVoiceControlToInputCommandLink(struct InputCommander *phead);

struct InputCommander *addSocketControlToInputCommandLink(struct InputCommander *phead);

2、controlDevices .h

#include <wiringPi.h>
#include <stdio.h>



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 (*changeStatus)(int status);
	

	
	void (*faceRecognition)(); //人脸识别
	char *(*takePictureInit)();//用于摄像头
	char *(*getPicBase64)(); //用于摄像头
	size_t (*readData)(); //用于摄像头

	struct Devices *next;

};

struct Devices *addBathroomLightToDeviceLink(struct Devices *phead);
struct Devices *addUpstairLightToDeviceLink(struct Devices *phead);
struct Devices *addDiningroomLightToDeviceLink(struct Devices *phead);
struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead);
struct Devices *addFireAlarmToDeviceLink(struct Devices *phead);
struct Devices* addBeepToDeviceLink(struct Devices *phead);
struct Devices *addLookToDeviceLink(struct Devices *phead);


struct Devices *addCameraToDeviceLink(struct Devices *phead);

3、mainPro.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>       
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


#include "controlDevices.h"
#include "InputCommand.h"



struct InputCommander *pCommandHead = NULL;
struct Devices 		  *pdeviceHead = NULL;

struct InputCommander *socketHandler = NULL;

int c_fd;
char *name = NULL;

pthread_t cameraThread;



struct Devices *findDeviceByName(char *name,struct Devices *phead)
{
   
	struct Devices *tmp = phead;
	if(phead == NULL)
		{
   
			return NULL;
		}
	else{
   
			while(tmp != NULL){
   
				if(strcmp(tmp->deviceName,name) == 0)
					{
   
						return tmp;
					}
				tmp = tmp->next;
				}
			return NULL;
		}

}

struct InputCommander *findCommandByName(char *name,struct InputCommander *phead)
{
   
	struct InputCommander *tmp = phead;
	if(phead == NULL)
		{
   
			return NULL;

		}
	else
		{
   
			while(tmp != NULL){
   
				if(strcmp(tmp->commandName,name) == 0)
					{
   
						return tmp;
					}
				tmp = tmp->next;
				}
			return NULL;
		}

}

void *camera_thread(void *datas)	//摄像头线程
{
   
	
    struct Devices *cameraTemp;

    cameraTemp = findDeviceByName("camera", pdeviceHead); //设备都要从工厂里面取出来

    if (cameraTemp == NULL)
    {
    	//防止段错误的必需判断,当给指针赋值是,一定要考虑NULL的情况
        printf("find camera error\n");
        pthread_exit(NULL); //在线程中不用return
    	}

    cameraTemp->faceRecognition(); //调用人脸识别函数
	

}
void * voice_thread(void *datas)	//语音线程
{
   
	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,NULL,NULL)<0){
   
			printf("voice init error\n");
			pthread_exit(NULL);
		}else{
   
				printf(
  • 14
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Hestia是一个基于树莓派智能家居项目,它利用了现代技术,如物联网(IoT)、云计算和人工智能(AI)等,为用户提供更加智能化、便利的家居体验。 Hestia的主要特点包括: 1. 轻松部署:Hestia可以在树莓派上轻松部署,使用简单快捷。 2. 模块化设计:Hestia采用模块化设计,可以根据用户需求,自由搭配各种传感器和执行器,实现家居自动化控制。 3. 云端控制:用户可以通过手机或电脑,随时随地远程控制家居设备,方便快捷。 4. 智能化:Hestia利用机器学习和人工智能算法,学习用户的生活习惯和喜好,为用户提供更加便捷的智能家居体验。 Hestia的具体实现方案如下: 1. 硬件方案:使用树莓派作为主控制器,搭配各种传感器和执行器,如温度传感器、湿度传感器、光线传感器、电机、继电器等,实现家居设备的自动化控制。 2. 软件方案:使用Java语言开发控制程序,通过网络连接,将传感器和执行器的数据传输到云端服务器。在云端服务器上,使用Python语言进行数据处理和机器学习算法,根据用户的生活习惯和喜好,自动控制家居设备。 3. 用户界面:提供手机App和网页端,用户可以随时随地远程控制家居设备,查看家居设备的状态和历史数据。用户可以通过App或网页端设置家居设备的自动化控制规则和时间表。 总之,Hestia是一款高效、智能、可扩展的智能家居系统,将为用户带来更加便利、舒适的家居体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunshime.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值