• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

智能家居项目三:框架设计和框架代码文件工程建立

武飞扬头像
Love小羽
帮助1

目录

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

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

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


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

学新通

 代码思路讲解:

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

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

学新通

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

学新通

3、创建inputCommand.h头文件

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

4、创建contrlDevices.h头文件

  1.  
    //设备头文件
  2.  
    #include <wiringPi.h>
  3.  
     
  4.  
    struct Devices{
  5.  
     
  6.  
    char deviceName[128]; //名字
  7.  
    int status; //状态
  8.  
    int pinNum; //引脚
  9.  
     
  10.  
    int (*open)(int pinNum); //打开
  11.  
    int (*close)(int pinNum); //关闭
  12.  
    int (*deviceInit)(int pinNum); //设备初始化
  13.  
     
  14.  
    int (*readStatus)(); //火灾
  15.  
    int (*changeStatus)(int status);
  16.  
     
  17.  
    struct Devices *next;
  18.  
    };
  19.  
     
  20.  
    struct Devices* addBathroomLightToDeviceLink(struct Devices *phead);
  21.  
    struct Devices* addUpstairLightToDeviceLink(struct Devices *phead);
  22.  
    struct Devices* addLivingRoomLightToDeviceLink(struct Devices *phead);
  23.  
    struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);
  24.  
    struct Devices* addFireToDeviceLink(struct Devices *phead);
学新通

5、创建bathroomLight.c文件

  1.  
    //浴室的灯
  2.  
    #include "contrlDevices.h"
  3.  
    #include<stdlib.h>
  4.  
     
  5.  
    int bathroomLightOpen(int pinNum){
  6.  
     
  7.  
    digitalWrite(pinNum,LOW);
  8.  
    }
  9.  
    int bathroomLightClose(int pinNum){
  10.  
     
  11.  
    digitalWrite(pinNum,HIGH);
  12.  
    }
  13.  
    int bathroomLightCloseInit(int pinNum){
  14.  
     
  15.  
    pinMode(pinNum,OUTPUT);
  16.  
    digitalWrite(pinNum,HIGH);
  17.  
    }
  18.  
    int bathroomLightCloseStatus(int status){
  19.  
     
  20.  
    }
  21.  
    struct Devices bathroomLight={
  22.  
     
  23.  
    .deviceName = "bathroomLight",
  24.  
    .pinNum = 26,
  25.  
    .open = bathroomLightOpen,
  26.  
    .close = bathroomLightClose,
  27.  
    .deviceInit = bathroomLightCloseInit,
  28.  
    .changeStatus = bathroomLightCloseStatus
  29.  
     
  30.  
    };
  31.  
    struct Devices* addBathroomLightToDeviceLink(struct Devices *phead){
  32.  
     
  33.  
    if(phead == NULL){
  34.  
    return &bathroomLight;
  35.  
    }
  36.  
    else{
  37.  
    bathroomLight.next = phead;
  38.  
    phead = &bathroomLight;
  39.  
    }
  40.  
    };
学新通

6、创建livingroomLight.c文件

  1.  
    #include "contrlDevices.h"
  2.  
    #include<stdlib.h>
  3.  
     
  4.  
    int livingroomLightOpen(int pinNum){
  5.  
     
  6.  
    digitalWrite(pinNum,LOW);
  7.  
    }
  8.  
    int livingroomLightClose(int pinNum){
  9.  
     
  10.  
    digitalWrite(pinNum,HIGH);
  11.  
    }
  12.  
    int livingroomLightCloseInit(int pinNum){
  13.  
     
  14.  
    pinMode(pinNum,OUTPUT);
  15.  
    digitalWrite(pinNum,HIGH);
  16.  
    }
  17.  
    int livingroomLightCloseStatus(int status){
  18.  
     
  19.  
    }
  20.  
    struct Devices livingroomLight={
  21.  
     
  22.  
    .deviceName = "livingroomLight",
  23.  
    .pinNum = 27,
  24.  
    .open = livingroomLightOpen,
  25.  
    .close = livingroomLightClose,
  26.  
    .deviceInit = livingroomLightCloseInit,
  27.  
    .changeStatus = livingroomLightCloseStatus
  28.  
     
  29.  
    };
  30.  
    struct Devices* addLivingRoomLightToDeviceLink(struct Devices *phead){
  31.  
    if(phead == NULL){
  32.  
    return &livingroomLight;
  33.  
    }
  34.  
    else{
  35.  
    livingroomLight.next = phead;
  36.  
    phead = &livingroomLight;
  37.  
    }
  38.  
    };
学新通

7、创建restaurantLight.c文件

  1.  
    #include "contrlDevices.h"
  2.  
    #include<stdlib.h>
  3.  
     
  4.  
    int restaurantLightOpen(int pinNum){
  5.  
     
  6.  
    digitalWrite(pinNum,LOW);
  7.  
    }
  8.  
    int restaurantLightClose(int pinNum){
  9.  
     
  10.  
    digitalWrite(pinNum,HIGH);
  11.  
    }
  12.  
    int restaurantLightCloseInit(int pinNum){
  13.  
     
  14.  
    pinMode(pinNum,OUTPUT);
  15.  
    digitalWrite(pinNum,HIGH);
  16.  
    }
  17.  
    int restaurantLightCloseStatus(int status){
  18.  
     
  19.  
    }
  20.  
    struct Devices restaurantLight={
  21.  
     
  22.  
    .deviceName = "restaurantLight",
  23.  
    .pinNum = 28,
  24.  
    .open = restaurantLightOpen,
  25.  
    .close = restaurantLightClose,
  26.  
    .deviceInit = restaurantLightCloseInit,
  27.  
    .changeStatus = restaurantLightCloseStatus
  28.  
    };
  29.  
    struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead){
  30.  
     
  31.  
    if(phead == NULL){
  32.  
    return &restaurantLight;
  33.  
    }
  34.  
    else{
  35.  
    restaurantLight.next = phead;
  36.  
    phead = &restaurantLight;
  37.  
    }
  38.  
    };
学新通

8、创建upstairLight.c文件

  1.  
    //二楼灯
  2.  
    #include "contrlDevices.h"
  3.  
    #include<stdlib.h>
  4.  
     
  5.  
    int upstairLightOpen(int pinNum){
  6.  
     
  7.  
    digitalWrite(pinNum,LOW);
  8.  
    }
  9.  
    int upstairLightClose(int pinNum){
  10.  
     
  11.  
    digitalWrite(pinNum,HIGH);
  12.  
    }
  13.  
    int upstairLightCloseInit(int pinNum){
  14.  
     
  15.  
    pinMode(pinNum,OUTPUT);
  16.  
    digitalWrite(pinNum,HIGH);
  17.  
    }
  18.  
    int upstairLightCloseStatus(int status){
  19.  
     
  20.  
    }
  21.  
    struct Devices upstairLight={
  22.  
     
  23.  
    .deviceName = "upstairLight",
  24.  
    .pinNum = 29,
  25.  
    .open = upstairLightOpen,
  26.  
    .close = upstairLightClose,
  27.  
    .deviceInit = upstairLightCloseInit,
  28.  
    .changeStatus = upstairLightCloseStatus
  29.  
    };
  30.  
    struct Devices* addUpstairLightToDeviceLink(struct Devices *phead){
  31.  
     
  32.  
    if(phead == NULL){
  33.  
    return &upstairLight;
  34.  
    }
  35.  
    else{
  36.  
    upstairLight.next = phead;
  37.  
    phead = &upstairLight;
  38.  
    }
  39.  
    };
学新通

9、创建fire.c文件

  1.  
    //火灾报警
  2.  
    #include "contrlDevices.h"
  3.  
    #include<stdlib.h>
  4.  
     
  5.  
    int fireIfOrNotInit(int pinNum){
  6.  
     
  7.  
    pinMode(pinNum,INPUT);
  8.  
    digitalWrite(pinNum,HIGH);
  9.  
    }
  10.  
    int fireStatusRead(int pinNum){
  11.  
     
  12.  
    return digitalRead(pinNum);
  13.  
    }
  14.  
    struct Devices fireIfOrNot={
  15.  
     
  16.  
    .deviceName = "fireIfOrNot",
  17.  
    .pinNum = 25,
  18.  
    .deviceInit = fireIfOrNotInit,
  19.  
    .readStatus = fireStatusRead
  20.  
     
  21.  
    };
  22.  
    struct Devices* addFireToDeviceLink(struct Devices *phead){
  23.  
     
  24.  
    if(phead == NULL){
  25.  
    return &fireIfOrNot;
  26.  
    }
  27.  
    else{
  28.  
    fireIfOrNot.next = phead;
  29.  
    phead = &fireIfOrNot;
  30.  
    }
  31.  
    };
学新通

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

  1.  
    #include <stdio.h>
  2.  
    #include "contrlDevices.h"
  3.  
    #include <string.h>
  4.  
    #include "inputCommand.h"
  5.  
     
  6.  
    struct Devices* findDeviceByName(char* name,struct Devices* phead){
  7.  
     
  8.  
    struct Devices *tmp = phead;
  9.  
    if(phead == NULL){
  10.  
    return NULL;
  11.  
    }
  12.  
    else{
  13.  
    while(tmp != NULL){
  14.  
    if(strcmp(tmp->deviceName,name) == 0){
  15.  
    return tmp;
  16.  
    }
  17.  
    tmp = tmp->next;
  18.  
    }
  19.  
    return NULL;
  20.  
    }
  21.  
    };
  22.  
     
  23.  
    int main(){
  24.  
     
  25.  
    char name [128];
  26.  
    struct Devices *tmp = NULL;
  27.  
    if(-1 == wiringPiSetup()){
  28.  
    return -1;
  29.  
    }
  30.  
    struct Devices *pdeviceHead = NULL;
  31.  
    pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
  32.  
    pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
  33.  
    pdeviceHead = addLivingRoomLightToDeviceLink(pdeviceHead);
  34.  
    pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
  35.  
    pdeviceHead = addFireToDeviceLink(pdeviceHead);
  36.  
     
  37.  
    while(1){
  38.  
     
  39.  
    printf("Input:\n");
  40.  
    scanf("%s",name);
  41.  
    tmp = findDeviceByName(name,pdeviceHead);
  42.  
     
  43.  
    if(tmp != NULL){
  44.  
    tmp->deviceInit(tmp->pinNum);
  45.  
    tmp->open(tmp->pinNum);
  46.  
    }
  47.  
    }
  48.  
    //1、指令工厂初始化
  49.  
    //2、设备控制工厂初始化
  50.  
    //3、线程池建立
  51.  
    //3.1、语音线程
  52.  
    //3.2、socket线程
  53.  
    //3.3、摄像头线程
  54.  
    //3.4、火灾线程
  55.  
    }
学新通

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

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

学新通

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

学新通

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

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

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

  1.  
    #include "inputCommand.h"
  2.  
    #include <stdlib.h>
  3.  
    #include <wiringPi.h>
  4.  
    #include <stdio.h>
  5.  
    #include <wiringSerial.h>
  6.  
    #include <unistd.h>
  7.  
     
  8.  
    //串口
  9.  
     
  10.  
    int voiceInit(struct InputCommander *voicer,char *ipAdress,char *port){ //声音初始化
  11.  
     
  12.  
    int fd;
  13.  
    if((fd = serialOpen(voicer->deviceName,9600)) == -1){ //初始化串口,波特率9600
  14.  
    exit(-1);
  15.  
    }
  16.  
    voicer->fd = fd;
  17.  
    return fd;
  18.  
    }
  19.  
    int voiceGetCommand(struct InputCommander *voicer){
  20.  
     
  21.  
    int nread = 0;
  22.  
    nread = (voicer->fd,voicer->command,sizeof(voicer->command));
  23.  
    if(nread == 0){
  24.  
    printf("usart for voice read over time\n");
  25.  
    }else{
  26.  
    return nread;
  27.  
    }
  28.  
    }
  29.  
    struct InputCommander voiceContrl = {
  30.  
     
  31.  
    .commandName = "voice",
  32.  
    .deviceName = "dev/ttyAMA0",
  33.  
    .command = {'\0'},
  34.  
    .Init = voiceInit,
  35.  
    .getCommand = voiceGetCommand,
  36.  
    .log = {'\0'},
  37.  
    .next = NULL,
  38.  
     
  39.  
    };
  40.  
    struct InputCommander* addvoiceContrlToInputCommandLink(struct InputCommander *phead){
  41.  
     
  42.  
    if(phead == NULL){
  43.  
    return &voiceContrl;
  44.  
    }
  45.  
    else{
  46.  
    voiceContrl.next = phead;
  47.  
    phead = &voiceContrl;
  48.  
    }
  49.  
    };
学新通

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

  1.  
    //面向指令工厂的头文件
  2.  
    #include <wiringPi.h>
  3.  
    #include <stdlib.h>
  4.  
     
  5.  
    struct InputCommander{
  6.  
     
  7.  
    char commandName[128]; //声音的名字
  8.  
    char command[32];
  9.  
    char deviceName[128]; //串口的名字
  10.  
    int (*Init)(struct InputCommander *voicer,char *ipAdress,char *port);
  11.  
    int (*getCommand)(struct InputCommander *voicer);
  12.  
     
  13.  
    char log[1024];
  14.  
    int fd;
  15.  
    struct InputCommander *next;
  16.  
     
  17.  
    };
  18.  
    struct InputCommander* addvoiceContrlToInputCommandLink(struct InputCommander *phead);
学新通

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

  1.  
    #include <stdio.h>
  2.  
    #include "contrlDevices.h"
  3.  
    #include <string.h>
  4.  
    #include "inputCommand.h"
  5.  
     
  6.  
    struct Devices* findDeviceByName(char* name,struct Devices* phead){
  7.  
     
  8.  
    struct Devices *tmp = phead;
  9.  
    if(phead == NULL){
  10.  
    return NULL;
  11.  
    }
  12.  
    else{
  13.  
    while(tmp != NULL){
  14.  
    if(strcmp(tmp->deviceName,name) == 0){
  15.  
    return tmp;
  16.  
    }
  17.  
    tmp = tmp->next;
  18.  
    }
  19.  
    return NULL;
  20.  
    }
  21.  
    };
  22.  
    int main(){
  23.  
     
  24.  
    char name [128];
  25.  
    struct Devices *tmp = NULL;
  26.  
    if(-1 == wiringPiSetup()){
  27.  
    return -1;
  28.  
    }
  29.  
    struct Devices *pdeviceHead = NULL; //设备工厂
  30.  
    struct InputCommander *pCommandHead = NULL; //指令工厂
  31.  
     
  32.  
    pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
  33.  
    pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
  34.  
    pdeviceHead = addLivingRoomLightToDeviceLink(pdeviceHead);
  35.  
    pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
  36.  
    pdeviceHead = addFireToDeviceLink(pdeviceHead); //火灾
  37.  
     
  38.  
    pCommandHead = addvoiceContrlToInputCommandLink(pCommandHead); //串口
  39.  
    while(1){
  40.  
     
  41.  
    printf("Input:\n");
  42.  
    scanf("%s",name);
  43.  
    tmp = findDeviceByName(name,pdeviceHead);
  44.  
     
  45.  
    if(tmp != NULL){
  46.  
    tmp->deviceInit(tmp->pinNum);
  47.  
    tmp->open(tmp->pinNum);
  48.  
    }
  49.  
    }
  50.  
    }
学新通

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

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

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhacbbg
系列文章
更多 icon
同类精品
更多 icon
继续加载