当前位置: 首页 > news >正文

智能家居项目(三)之框架设计及框架代码文件工程建立

目录

一、智能家居项目框架设计草图

二、框架代码文件工程建立

三、添加声音识别模块的串口读取功能


一、智能家居项目框架设计草图

 代码思路讲解:

1、一个指令工厂,一个控制工厂,实际上就是通过链表链起来的数据。具体怎么链接起来,就是基于简单工厂模式的类与对象的概念,上一篇文章有学习记录。
2、主函数语音指令程序和tcp指令程序各起一个线程,然后通过指令名字找到对应的控制程序,实现对应的模块的功能。

二、框架代码文件工程建立

1、在桌面新建一个项目文件夹smartHose,然后在文件夹中创建如下文件:

2、把上述的所有文件,都加载到Source lnsight工具中,如下图代表加载完毕

3、创建inputCommand.h头文件

//面向指令工厂的头文件
#include <wiringPi.h>
#include <stdlib.h>struct InputCommander{char commandName[128];             //名字char command[32];                  //指令int (*Init)(char *name,char *ipAdress,char *port);  //操作函数 int (*getCommand)(char *cmd);      //获取数据函数char log[1024];                    //log日志获取struct InputCommander *next;
};

4、创建contrlDevices.h头文件

//设备头文件
#include <wiringPi.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 (*changeStatus)(int status);struct Devices *next;
};struct Devices* addBathroomLightToDeviceLink(struct Devices *phead);
struct Devices* addUpstairLightToDeviceLink(struct Devices *phead);
struct Devices* addLivingRoomLightToDeviceLink(struct Devices *phead);
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);
struct Devices* addFireToDeviceLink(struct Devices *phead);

5、创建bathroomLight.c文件

//浴室的灯
#include "contrlDevices.h"
#include<stdlib.h>int bathroomLightOpen(int pinNum){digitalWrite(pinNum,LOW);
}
int bathroomLightClose(int pinNum){digitalWrite(pinNum,HIGH);
}
int bathroomLightCloseInit(int pinNum){pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);
}
int bathroomLightCloseStatus(int status){}
struct Devices bathroomLight={.deviceName = "bathroomLight",.pinNum = 26,.open = bathroomLightOpen,.close = bathroomLightClose,.deviceInit = bathroomLightCloseInit,.changeStatus = bathroomLightCloseStatus};
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead){if(phead == NULL){	return &bathroomLight;}else{bathroomLight.next = phead;phead = &bathroomLight;}
};

6、创建livingroomLight.c文件

#include "contrlDevices.h"
#include<stdlib.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 livingroomLight={.deviceName = "livingroomLight",.pinNum = 27,.open = livingroomLightOpen,.close = livingroomLightClose,.deviceInit = livingroomLightCloseInit,.changeStatus = livingroomLightCloseStatus};
struct Devices* addLivingRoomLightToDeviceLink(struct Devices *phead){if(phead == NULL){	return &livingroomLight;}else{livingroomLight.next = phead;phead = &livingroomLight;}
};

7、创建restaurantLight.c文件

#include "contrlDevices.h"
#include<stdlib.h>int restaurantLightOpen(int pinNum){digitalWrite(pinNum,LOW);
}
int restaurantLightClose(int pinNum){digitalWrite(pinNum,HIGH);
}
int restaurantLightCloseInit(int pinNum){pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);
}
int restaurantLightCloseStatus(int status){}
struct Devices restaurantLight={.deviceName = "restaurantLight",.pinNum = 28,.open = restaurantLightOpen,.close = restaurantLightClose,.deviceInit = restaurantLightCloseInit,.changeStatus = restaurantLightCloseStatus
};
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead){if(phead == NULL){	return &restaurantLight;}else{restaurantLight.next = phead;phead = &restaurantLight;}
};

8、创建upstairLight.c文件

//二楼灯
#include "contrlDevices.h"
#include<stdlib.h>int upstairLightOpen(int pinNum){digitalWrite(pinNum,LOW);
}
int upstairLightClose(int pinNum){digitalWrite(pinNum,HIGH);
}
int upstairLightCloseInit(int pinNum){pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);
}
int upstairLightCloseStatus(int status){}
struct Devices upstairLight={.deviceName = "upstairLight",.pinNum = 29,.open = upstairLightOpen,.close = upstairLightClose,.deviceInit = upstairLightCloseInit,.changeStatus = upstairLightCloseStatus
};
struct Devices* addUpstairLightToDeviceLink(struct Devices *phead){if(phead == NULL){	return &upstairLight;}else{upstairLight.next = phead;phead = &upstairLight;}
};

9、创建fire.c文件

//火灾报警
#include "contrlDevices.h"
#include<stdlib.h>int fireIfOrNotInit(int pinNum){pinMode(pinNum,INPUT);digitalWrite(pinNum,HIGH);
}
int fireStatusRead(int pinNum){return digitalRead(pinNum);
}
struct Devices fireIfOrNot={.deviceName = "fireIfOrNot",.pinNum = 25,.deviceInit = fireIfOrNotInit,.readStatus = fireStatusRead};
struct Devices* addFireToDeviceLink(struct Devices *phead){if(phead == NULL){	return &fireIfOrNot;}else{fireIfOrNot.next = phead;phead = &fireIfOrNot;}
};

10、创建mainPro.c主函数文件

#include <stdio.h>
#include "contrlDevices.h"
#include <string.h>
#include "inputCommand.h"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;}
};int main(){char name [128];struct Devices *tmp = NULL;if(-1 == wiringPiSetup()){return -1;}struct Devices *pdeviceHead = NULL;pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingRoomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);while(1){printf("Input:\n");scanf("%s",name);tmp = findDeviceByName(name,pdeviceHead);if(tmp != NULL){tmp->deviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}}//1、指令工厂初始化//2、设备控制工厂初始化//3、线程池建立//3.1、语音线程//3.2、socket线程//3.3、摄像头线程//3.4、火灾线程
}

把上述的代码传到树莓派的终端,用FileZilla传即可,然后执行结果:

gcc mainPro.c upstairLight.c bathroomLight.c livingroomLight.c restaurantLight.c -lwiringPi -o test1

效果演示:(虽然没有装到实际的智能家居里,但是小灯亮了,说明程序是可以正常用的🤭)

三、添加声音识别模块的串口读取功能

这里主要通过主控芯片树莓派的串口跟语音模块连接。
树莓派的T接语音模块的R
树莓派的R接语音模块的T
然后就是供电

我们先把语音模块的代码整合到指令链表当中去:
1.语音控制设备voiceContrl.c

#include "inputCommand.h"
#include <stdlib.h>
#include <wiringPi.h>
#include <stdio.h>
#include <wiringSerial.h>
#include <unistd.h>//串口int voiceInit(struct InputCommander *voicer,char *ipAdress,char *port){ //声音初始化int fd;if((fd = serialOpen(voicer->deviceName,9600)) == -1){  //初始化串口,波特率9600exit(-1);}voicer->fd = fd;return fd;
} 
int voiceGetCommand(struct InputCommander *voicer){int nread = 0;nread = (voicer->fd,voicer->command,sizeof(voicer->command));if(nread == 0){printf("usart for voice read over time\n");}else{return nread;}
}
struct InputCommander voiceContrl = {.commandName = "voice", .deviceName = "dev/ttyAMA0",.command = {'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL,};
struct InputCommander* addvoiceContrlToInputCommandLink(struct InputCommander *phead){if(phead == NULL){return &voiceContrl;}else{voiceContrl.next = phead;phead = &voiceContrl;}
};

2.控制设备的头文件inputCommand.h

//面向指令工厂的头文件
#include <wiringPi.h>
#include <stdlib.h>struct InputCommander{char commandName[128];  //声音的名字char command[32];       char deviceName[128];   //串口的名字int (*Init)(struct InputCommander *voicer,char *ipAdress,char *port); int (*getCommand)(struct InputCommander *voicer);char log[1024];int fd;struct InputCommander *next;};
struct InputCommander* addvoiceContrlToInputCommandLink(struct InputCommander *phead);

3.在mainPro.c主函数中添加语音模块的函数

#include <stdio.h>
#include "contrlDevices.h"
#include <string.h>
#include "inputCommand.h"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;}
};
int main(){char name [128];struct Devices *tmp = NULL;if(-1 == wiringPiSetup()){return -1;}struct Devices        *pdeviceHead = NULL;   //设备工厂struct InputCommander *pCommandHead = NULL;  //指令工厂pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingRoomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);  //火灾pCommandHead = addvoiceContrlToInputCommandLink(pCommandHead); //串口while(1){printf("Input:\n");scanf("%s",name);tmp = findDeviceByName(name,pdeviceHead);if(tmp != NULL){tmp->deviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}}
}

4.把上述的代码传到树莓派的终端,用FileZilla传即可,然后执行结果:

gcc mainPro.c upstairLight.c bathroomLight.c fire.c livingroomLight.c restaurantLight.c voiceContrl.c -lwiringPi -o test1

相关文章:

智能家居项目(三)之框架设计及框架代码文件工程建立

目录 一、智能家居项目框架设计草图 二、框架代码文件工程建立 三、添加声音识别模块的串口读取功能 一、智能家居项目框架设计草图 代码思路讲解&#xff1a; 1、一个指令工厂&#xff0c;一个控制工厂&#xff0c;实际上就是通过链表链起来的数据。具体怎么链接起来&…...

全网最全的Ansible中常用模块讲解

目录 前言 一、ansible实现管理的方式 二、Ad-Hoc执行方式中如何获得帮助 三、ansible命令运行方式及常用参数 四、ansible的基本颜色代表信 五、ansible中的常用模块 1、command 2、shell 3、script 4、copy 5、fetch 6、file 7、 unarchive 8、archive 9、h…...

linux程序分析工具

嵌入式调试工具1. nm2. addr2line3. readelf3.1 ELF 文件分类3.2 ELF文件组成3.3使用1. nm nm源于name&#xff0c;是linux下一个文本分析工具&#xff0c;可以罗列指定文件中的符号(函数名、变量&#xff0c;以及符号类型)。 nm命令参数如下&#xff1a; 用法&#xff1a;nm …...

Python3,2分钟掌握Doscoart库,你也能成为艺术家。

2行代码绘制水彩画1、引言2、 代码实战2.1 模块介绍2.2 模块安装2.3 代码示例2.3.1 创建默认图片2.3.2 设置参数创建图片2.3.3 查看设置参数2.3.4 查看配置2.3.5 保存配置2.3.6 加载配置2.3.7 导出配置文件2.3.7 生成Python代码2.3.8 调用文档3、总结1、引言 小屌丝&#xff1…...

1225057-68-0,Alkyne PEG4 TAMRA-5,四甲基罗丹明-四聚乙二醇-炔基TAMRA红色荧光染料连接剂

中英文别名&#xff1a;CAS号&#xff1a;1225057-68-0 | 英文名&#xff1a;5-TAMRA-PEG4-Alkyne |中文名&#xff1a;5-四甲基罗丹明-四聚乙二醇-炔基物理参数&#xff1a;CASNumber&#xff1a;1225057-68-0Molecular formula&#xff1a;C36H41N3O8Molecular weight&#x…...

Ae:解释素材

所谓解释素材 Interpret Footage&#xff0c;就是通过修改素材的某些属性&#xff08;像素长宽比、帧速率、颜色配置文件及 Alpha 通道类型等&#xff09;&#xff0c;让它能更好地参与到合成中去。Ae菜单&#xff1a;文件/解释素材快捷键&#xff1a;Ctrl Alt G在项目面板里…...

无文件攻击

无文件攻击是一种高级持续性威胁&#xff08;APT&#xff09;的攻击方式&#xff0c;它不会在目标系统的磁盘上留下可执行文件&#xff0c;而是利用系统内置的工具或脚本执行恶意代码&#xff0c;从而绕过传统的安全防护措施。无文件攻击的最大特点就是恶意代码直接在内存中运行…...

JS高级——数据类型

数据类型 基本类型 String: 任意字符串Number: 任意的数字boolean: true/falseundefined: undefinednull: null 对象类型 Object: 任意对象Function 一种特别的对象&#xff08;可以执行&#xff09;Array: 一种特别的对象 判断 typeof //不能区分数组与对象、null与obje…...

场景案例│数字员工在银行业的典型应用场景,效率及准确率“双高”

伴随数字经济的高速发展&#xff0c;企业数字化转型步伐不断加快&#xff0c;银行内部信息系统越趋复杂&#xff0c;业务处理的自动化及智能化需求日益旺盛。调查显示&#xff0c;数字员工为60~75%的银行流程带来约30~40%的效能提升&#xff0c;能够全面帮助银行在各场景流程中…...

2023美国大学生数学建模竞赛选题建议

总的来说&#xff0c;这次算是美赛环境题元年&#xff0c;以往没有这么多环境题目&#xff0c;大部分题目都是开放度相当高的题目。C君认为的难度&#xff1a;D>C>AE>BF&#xff0c;开放度&#xff1a;DF>ABE>C。A题 遭受旱灾的植物群落这次A题为环境类题目&…...

整合K8s+SpringBoot+gRpc

本文使用K8s当做服务注册与发现、配置管理&#xff0c;使用gRpc用做服务间的远程通讯一、先准备K8s我在本地有个K8s单机二、准备service-providerpom<?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.…...

ROS 教程:使用 Moveit C++ 接口进行拾取和放置任务

文章目录 简介Moveit C++ 接口Gazebo 取放世界初始化界面拾取流程1.移动到原位2.将TCP放在蓝框上方3.打开夹具4. 将 TCP 移近物体5.关闭夹具6. 将 TCP 移至板上方7./8. 降低 TCP 并打开夹具使用 Moveit 避免碰撞将碰撞对象添加到 Moveit 规划组结论参考简介 本教程展示了如何使…...

seo细分和切入点

seo细分和切入点本文重点介绍做SEO网站细分和切入点的方法&#xff1a;当我们的行业和关键词竞争性比较大的时候&#xff0c;我们可以考虑对行业或者产品做细分&#xff0c;从而找到切入点。可以按照以下三个方面进行细分。1、按城市细分例如&#xff1a;A&#xff1a;餐饮培训…...

PyQt5数据库开发1 4.3 QSqlTableModel 之 Qt项目的创建

目录 一、新建Qt项目 1. 编辑资源文件 2. 添加前缀 3. 新建放资源文件的目录 4. 添加图标文件 二、Action 1. 新建打开数据库Action 2. 添加其他Action 三、工具栏 1. 添加工具栏 2. 拖动actOpenDB到工具栏 3. 设置工具栏属性 4. 添加分隔符 5. 添加其他工具 6.…...

【大数据】第三章:详解HDFS(送尚硅谷笔记和源码)

什么是HDFS HDFS是&#xff08;Hadoop Distributed File System&#xff09;的缩写&#xff0c;也即Hadoop分布式文件系统。它通过目录树定位在分布式场景下 在不同服务器主机上的文件。它适用于一次写入&#xff0c;多次读出的场景。 HDFS的优缺点 优点 1&#xff0c;高容…...

27岁想转行IT,还来得及吗?

来不来得及不还是看你自身的意愿和条件&#xff0c;这个问题要问你自己吧&#xff01; 每个人的能力、看法都不同。面对类似的问题&#xff0c;很多人会把侧重点放在IT上&#xff0c;或者27岁上面。那么我们试着换一个方式来问呢&#xff1a;什么时候适合转行&#xff0c;有哪些…...

Web前端CSS清除浮动的5种方法

在移动端清除浮动布局&#xff0c;常用的5种方法&#xff1a; 使用清除浮动的类&#xff1b;使用overflow属性&#xff1b;使用 flex 布局&#xff1b;使用grid 布局&#xff1b;使用 table 布局。 根据实际情况选择适合的方法&#xff0c;需要注意兼容性和语义性问题。在移动…...

Java:博客系统,实现加盐加密,分页,草稿箱,定时发布

文章目录1. 项目概述2. 准备工作2.1 数据库表格代码2.2 前端代码2.3 配置文件3. 准备项目结构3.1 拷贝前端模板3.2 定义实体类3.3 定义mapper接口和 xml 文件3.4 创建其他包4. 统一数据返回4.1 Result 类4.2 统一数据格式5. 注册5.1 逻辑5.2 验证数据规范性5.3 实现注册5.4 前端…...

JuiceFS 在火山引擎边缘计算的应用实践

火山引擎边缘云是以云计算基础技术和边缘异构算力结合网络为基础&#xff0c;构建在边缘大规模基础设施之上的云计算服务&#xff0c;形成以边缘位置的计算、网络、存储、安全、智能为核心能力的新一代分布式云计算解决方案。边缘存储主要面向适配边缘计算的典型业务场景&#…...

实验06 二叉树遍历及应用2022

A. 【程序填空】二叉树三种遍历题目描述给定一颗二叉树的特定先序遍历结果&#xff0c;空树用字符‘0’表示&#xff0c;例如AB0C00D00表示如下图请完成以下程序填空&#xff0c;建立该二叉树的二叉链式存储结构&#xff0c;并输出该二叉树的先序遍历、中序遍历和后序遍历结果输…...

导师严选!盘点2026年抢手爆款的AI论文写作工具

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具&#xff0c;覆盖选题构思、文献整理、内容生成、降重润色四大核心场景&#xff0c;帮你高效搞定论文&#xff0c;轻松应对学术挑战。 一、全流程王者&#xff1a;一站式搞定论文全链路…...

戴尔Precision Pro商务笔记本回归,新一代产品聚焦便携性

看起来2026年是戴尔按下重启键的一年——在消费者和商用产品线中&#xff0c;戏剧性的品牌重塑都在被回归。有时候&#xff0c;老品牌确实是好品牌&#xff1a;熟悉的XPS名称和设计今年回归是有原因的&#xff0c;绝对不是因为戴尔Premium有正面的品牌认知度。在戴尔商务产品方…...

Fira Code技术揭秘:编程字体连字引擎的深度优化与实战应用

Fira Code技术揭秘&#xff1a;编程字体连字引擎的深度优化与实战应用 【免费下载链接】FiraCode Free monospaced font with programming ligatures 项目地址: https://gitcode.com/GitHub_Trending/fi/FiraCode 在当今的代码编辑环境中&#xff0c;开发者每天需要处理…...

合肥高中英语一对一辅导2026指南,突破听说读写全面提升路径

合肥高中英语一对一辅导2026指南&#xff0c;突破听说读写全面提升路径据《2026年中国基础教育课外辅导行业白皮书》数据显示&#xff0c;2026年高中阶段英语学科辅导需求同比增长23%&#xff0c;其中超过65%的学生家长明确表示&#xff0c;传统大班教学已无法满足孩子个性化提…...

项目分享|LLM驱动的多市场股票智能分析器

项目分享|LLM驱动的多市场股票智能分析器 引言 在股票投资分析中&#xff0c;实时行情跟踪、多维度数据解析和科学决策判断是核心需求&#xff0c;而个人投资者往往面临数据分散、分析耗时、缺乏专业工具的问题。由ZhuLinsen开源的daily_stock_analysis项目完美解决了这些痛点…...

SDMatte效果对比图谱:SDMatte/RemBG/BackgroundMattingV2在玻璃场景PK

SDMatte效果对比图谱&#xff1a;SDMatte/RemBG/BackgroundMattingV2在玻璃场景PK 1. 引言&#xff1a;玻璃抠图的特殊挑战 玻璃材质因其透明和反光特性&#xff0c;一直是图像抠图领域最具挑战性的对象之一。传统抠图工具在处理玻璃制品时&#xff0c;往往会出现边缘断裂、透…...

AI训练神器!免配置YOLO可视化工具,标注+训练+推理全流程集成,支持YOLOv8~v12+50系显卡,开源可二开

AI训练神器&#xff01;免配置YOLO可视化工具&#xff0c;标注训练推理全流程集成&#xff0c;支持YOLOv8~v1250系显卡&#xff0c;开源可二开 yolo可视化训练工具&#xff0c;免配置环境&#xff0c;打开界面即可训练yolo模型&#xff0c;提供源代码及完整打包项目&#xff0c…...

从零到一:在本地CentOS环境完整部署yshop-drink扫码点餐系统的实战指南

1. 环境准备&#xff1a;从零搭建CentOS基础系统 第一次在本地部署yshop-drink扫码点餐系统时&#xff0c;我选择了CentOS 7.9作为基础环境。这个版本既稳定又兼容大多数现代软件包&#xff0c;特别适合作为生产环境使用。建议直接使用阿里云镜像站下载Minimal版本ISO文件&…...

Matlab粒子动画:3D爱心生成与动态模拟

1. 从零开始理解Matlab粒子动画 第一次看到3D爱心在屏幕上跳动时&#xff0c;那种震撼感至今难忘。作为理工科学生&#xff0c;我们常常用Matlab处理枯燥的数据&#xff0c;但很少有人知道它还能创作出如此浪漫的视觉效果。这就像用计算器弹钢琴——工具本身没有情感&#xff0…...

OV5640摄像头SCCB配置详解:告别照抄寄存器表,教你读懂数据手册进行个性化设置

OV5640摄像头SCCB高级配置实战&#xff1a;从寄存器表解读到图像优化全解析 1. 深入理解OV5640寄存器架构 OV5640作为OmniVision推出的500万像素图像传感器&#xff0c;其强大功能背后是超过200个可配置寄存器。许多开发者习惯直接套用现成的寄存器配置表&#xff0c;但当遇到图…...