摘自:智能家居
作者:LEO-max
发布时间: 2021-03-05 00:38:07
网址:https://blog.csdn.net/zouchengzhi1021/article/details/114375529?spm=1001.2014.3001.5502

目录

  • 主流程设计框架及某一功能框架编写
    • InputCommand.h框架
    • contrlDevices.h框架
    • bathroomLight.c框架
  • 四盏灯、火焰传感器及主程序代码(继电器输入控制)
    • bathroomLight.c
    • upstairLight.c
    • livingroomLight.c
    • restaurantLight.c
    • fire.c
    • mainPro.c
    • contrlDevices.h
  • 添加声音识别模块的串口读取功能
    • voiceContrl.c
    • InputCommand.h
    • mainPro.c
  • 添加socket服务器功能
    • socketContrl.c
    • InputCommand.h
    • mainPro.c
  • 主程序代码编写,实现语音和网络线程
    • socketContrl.c
    • voiceContrl.c
    • mainPro.c

主流程设计框架及某一功能框架编写

头文件:contrlDevices.hInputCommand.h
源文件:mainPro.cbathroomLight.cCamera.cfire.clivingroomLight.clock.crestaurantLight.csocketContrl.cupstairLight.cusartContrl.cvoiceContrl.c
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

InputCommand.h框架

struct InputCommander
{char commandName[128]; //名字char command[32];       //指令int (*Init)(char* name, char* ipAddress, char* port);   //操作函数int (*getCommand)(char* cmd); //获得数据char log[1024];struct InputCommander* next;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

contrlDevices.h框架

struct Devices
{char devicesName[128];int status;              //状态int (*open)();          //打开int (*close)();         //关闭int (*devicesInit)();   //初始化int (*readStatus)();   //读状态int(*changeStatus)(int status);        //改变状态struct Devices* next;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

bathroomLight.c框架

#include "contrlDevices.h"int bathroomLightOpen()
{}int bathroomLightClose()
{}int bathroomLightCloseInit()
{}int bathroomLightCloseStatus(int status)
{}struct Devices bathroomLight
{.name = "bathLight",.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}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

四盏灯、火焰传感器及主程序代码(继电器输入控制)

bathroomLight.c

#include "contrlDevices.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 =
{.devicesName = "bathLight",.pinNum = 22,.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;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

upstairLight.c

#include "contrlDevices.h"int upstairsLightOpen(int pinNum)    //打开
{digitalWrite(pinNum, LOW); //低电平开启
}int upstairsLightClose(int pinNum) //关闭
{digitalWrite(pinNum, HIGH);    //高电平关闭
}int upstairsLightCloseInit(int pinNum) //初始化
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);
}int upstairsLightCloseStatus(int status)   //保存
{}struct Devices upstairsLight =
{.devicesName = "upstairsLight",.pinNum = 21,.open = upstairsLightOpen,.close = upstairsLightClose,.deviceInit = upstairsLightCloseInit,.changeStatus = upstairsLightCloseStatus
};struct Devices* addUpstairsLightToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &upstairsLight;}else {upstairsLight.next = phead;    //  头插法phead = &upstairsLight;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

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 livingroomLight =
{.devicesName = "livingroomLight",.pinNum = 23,.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;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

restaurantLight.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 livingroomLight =
{.devicesName = "livingroomLight",.pinNum = 23,.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;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

fire.c

#include "contrlDevices.h"int fireStatusRead(int pinNum)
{return digitalWrite(pinNum, HIGH);
}int fireInit(int pinNum)   //初始化
{pinMode(pinNum, INPUT);digitalWrite(pinNum, HIGH);
}struct Devices fire =
{.devicesName = "fire",.pinNum = 25,.deviceInit = fireInit,.readStatus = fireStatusRead
};struct Devices* addFireToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &fire;}else {fire.next = phead;  //  头插法phead = &fire;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.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->devicesName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;if (wiringPiSetup() == -1) {return -1;}struct Devices* pdeviceHead = NULL;pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead)//struct Devices* tmp = findDeviceByName(name, pdeviceHead);while(1){printf("Inupt:\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 火灾线程return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

contrlDevices.h

#include <wiringPi.h>
#include <stdlib.h>struct Devices
{char devicesName[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* addRestaurantLightToDeviceLink(struct Devices* phead);
struct Devices* addLivingroomLightToDeviceLink(struct Devices* phead);
struct Devices* addBathroomLightToDeviceLink(struct Devices* phead);
struct Devices* addUpstairsLightToDeviceLink(struct Devices* phead);
struct Devices* addFireToDeviceLink(struct Devices* phead)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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

voiceContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>int voiceInit(struct InputCommander *voicer, char* ipAddress, 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 = read(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/ttyAMAO",.command = {'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addVoiceContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &voiceContrl;}else {voiceContrl.next = phead;    //  头插法phead = &voiceContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

InputCommand.h

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>int voiceInit(struct InputCommander *voicer, char* ipAddress, 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 = read(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/ttyAMAO",.command = {'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addVoiceContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &voiceContrl;}else {voiceContrl.next = phead;    //  头插法phead = &voiceContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.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->devicesName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;if (wiringPiSetup() == -1) {return -1;}struct Devices* pdeviceHead = NULL;struct InputCommander* pcommandHead = NULL;pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);pcommandHead = addVoiceContrlToCommandLink(pcommandHead);//struct Devices* tmp = findDeviceByName(name, pdeviceHead);while(1){printf("Inupt:\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 火灾线程return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

添加socket服务器功能

socketContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>int socketInit(struct InputCommander* socketMes, char* ipAddress, char* port)
{int s_fd;int c_fd;struct sockaddr_in s_addr;/*struct sockaddr_in c_addr;*///做初始化memset(&s_addr, 0, sizeof(struct sockaddr_in));/*memset(&c_addr, 0, sizeof(struct sockaddr_in));*/// sockets_fd = socket(AF_INET, SOCK_STREAM, 0);if (s_fd == -1) {perror("socket");exit(-1);}s_addr.sin_family = AF_INET;s_addr.sin_port = htons(atoi(socketMes->port));inet_aton(socketMes->ipAddress, &s_addr.sin_addr);// bindbind(s_fd, (struct sockaddr*) & s_addr, sizeof(struct sockaddr_in));//listenlisten(s_fd, 10);socketMes->sfd = s_fd;return s_fd;
}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);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("\get:%d\n", n_read);}else {printf("client quit\n");}return n_read;
}struct InputCommander socketContrl =
{.commandName = "socketServer",.command = {'\0'},.port = "8088",.ipAddress = "192.168.4.126",.Init = socketInit,.getCommand = socketGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addSocketContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &socketContrl;}else {socketContrl.next = phead;  //  头插法phead = &socketContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

InputCommand.h

#include <wiringPi.h>
#include <stdlib.h>struct InputCommander
{char commandName[128]; //名字char deviceName[128];char command[32];      //指令int (*Init)(struct InputCommander* voicer, char* ipAddress, 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* addVoiceContrlToCommandLink(struct InputCommander* phead);
struct InputCommander* addSocketContrlToCommandLink(struct InputCommander* phead);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.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->devicesName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;if (wiringPiSetup() == -1) {return -1;}struct Devices* pdeviceHead = NULL;struct InputCommander* pcommandHead = NULL;pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);pcommandHead = addVoiceContrlToCommandLink(pcommandHead);pcommandHead = addSocketContrlToCommandLink(pcommandHead);//struct Devices* tmp = findDeviceByName(name, pdeviceHead);while(1){printf("Inupt:\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 火灾线程return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

主程序代码编写,实现语音和网络线程

socketContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>int socketInit(struct InputCommander* socketMes, char* ipAddress, char* port)
{int s_fd;int c_fd;struct sockaddr_in s_addr;/*struct sockaddr_in c_addr;*///做初始化memset(&s_addr, 0, sizeof(struct sockaddr_in));/*memset(&c_addr, 0, sizeof(struct sockaddr_in));*/// sockets_fd = socket(AF_INET, SOCK_STREAM, 0);if (s_fd == -1) {perror("socket");exit(-1);}s_addr.sin_family = AF_INET;s_addr.sin_port = htons(atoi(socketMes->port));inet_aton(socketMes->ipAddress, &s_addr.sin_addr);// bindbind(s_fd, (struct sockaddr*) & s_addr, sizeof(struct sockaddr_in));//listenlisten(s_fd, 10);printf("socket Server listening\n");socketMes->sfd = s_fd;return s_fd;
}int socketGetCommand(struct InputCommander* socketMes)
{int c_fd;int n_read=0;struct sockaddr_in c_addr;memset(&c_addr, 0, sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);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("\get:%d\n", n_read);}else {printf("client quit\n");}return n_read;
}struct InputCommander socketContrl =
{.commandName = "socketServer",.command = {'\0'},.port = "8088",.ipAddress = "192.168.4.126",.Init = socketInit,.getCommand = socketGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addSocketContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &socketContrl;}else {socketContrl.next = phead;  //  头插法phead = &socketContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

voiceContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>int voiceInit(struct InputCommander* voicer, char* ipAddress, 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;memset(voicer->command, '\0',sizeof(voicer->command));nread = read(voicer->fd, voicer->command, sizeof(voicer->command));if (nread == 0) {printf("voice no datas\n");}else {return nread;}}struct InputCommander voiceContrl =
{.commandName = "voice",.deviceName = "/dev/ttyAMAO",.command = {'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addVoiceContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &voiceContrl;}else {voiceContrl.next = phead;    //  头插法phead = &voiceContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.h"
#include "InputCommand.h"
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>//全局变量
struct InputCommander* pcommandHead = NULL;
struct Devices* pdeviceHead = NULL;
struct InputCommander* socketHandler = NULL;
int c_fd;struct Devices* findDeviceByName(char* name, struct Devices* phead)
{struct Devices* tmp = phead;if (phead == NULL) {return NULL;}else {while (tmp != NULL) {if (strcmp(tmp->devicesName, 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* voice_thread(void* datas)
{struct InputCommander* voiceHandler;int nread;voiceHandler = findCommandByName("voice", pCommandHead);if (voiceHandler == NULL) {printf("find voiceHandler error");pthread_exit(NULL);//return NULL;}else {if (voiceHandler->Init(voiceHandler,NULL,NULL) < 0) {printf("voice init error\n");pthread_exit(NULL);//return NULL;}else {printf("%s init success\n", voiceHandler->commandName);}while (1) {nread = voiceHandler->getCommand(voiceHandler);if (nread == 0) {printf("nodata from voice\n");}else {printf("do divece contrl:%s\n", voiceHandler->command);}}}
}void* read_thread(void* datas)
{int n_read;memset(socketHandler->command,'\0', sizeof(socketHandler->command));n_read = read(c_fd, socketHandler->command, sizeof(socketHandler->command));if (n_read == -1) {perror("read");}else if (n_read > 0) {printf("\nget:%d,%s\n", n_read, socketHandler->command);}else {printf("client quit\n");}return n_read;
}void* socket_thread(void* datas)
{int n_read=0;pthread_t* readThread;struct sockaddr_in c_addr;memset(&c_addr, 0, sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);//int nread;socketHandler = findCommandByName("socketServer", pdeviceHead);if (socketHandler == NULL) {printf("find socketHandler error");pthread_exit(NULL);//return NULL;}else {printf("%s init success\n", socketHandler->commandName);}socketHandler->Init(socketHandler,NULL,NULL);while (1) {c_fd = accept(socketHandler->sfd, (struct sockaddr*) & c_addr, &clen);pthread_create(&readThread, NULL, read_thread, NULL);}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;pthread_t* voiceThread;pthread_t* socketThread;if (wiringPiSetup() == -1) {return -1;}// 1.指令工厂初始化pcommandHead = addVoiceContrlToCommandLink(pcommandHead);pcommandHead = addSocketContrlToCommandLink(pcommandHead);// 2.设备控制工厂初始化pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);// 3.线程池建立// 3.1 语音线程pthread_create(&voiceThread,NULL, voice_thread,NULL);// 3.2 socket线程pthread_create(&socketThread, NULL, socket_thread, NULL);// 3.3 摄像头线程// 3.4 火灾线程//struct Devices* tmp = findDeviceByName(name, pdeviceHead);/*while (1) {printf("Inupt:\n");scanf("%s", name);tmp = findDeviceByName(name, pdeviceHead);if (tmp != NULL) {tmp->deviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}}*///不退出,等待线程pthread_join(voiceThread,NULL);pthread_join(socketThread, NULL);return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191

智能家居(工厂模式)相关推荐

  1. 智能家居 (3) ——智能家居工厂模式介绍实现继电器控制灯

    目录 智能家居工厂模式整体设计框架 继电器控制灯代码 contrlEquipments.h 文件(设备类) mainPro.c 文件(主函数) bathroomLight.c 文件(浴室灯) seco ...

  2. 智能家居工厂模式整体设计框架控制设备测试

    通俗理解的步骤就是链表通用模板定义(在头文件里定义).链表的创建(头插尾插,在.C 文件里).链表的初始化(init配置管脚初始电平等).链表内容的读取(指令工厂TCP服务端读取客户端发来的指令.串口 ...

  3. 智能云工厂模式普惠中小企业,千鸟互联或成纸包装产业链“带头大哥”

    从废纸回收与线上交易平台,到纸包装闭环供应链交易平台,再到聚合中小企业闲置产能,成为具有实质生产能力的纸包装"大工厂".自2017年创立至今,短短5年时间里,千鸟互联完成跨越式的三 ...

  4. 全球及中国智能家居市场十四五竞争形势及营销模式咨询报告2021-2027年

    全球及中国智能家居市场十四五竞争形势及营销模式咨询报告2021-2027年 HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS--HS-- [修订日期 ...

  5. 智能家居市场需求矛盾深层原因分析

    智能家居设备主要包括中央控制系统.家庭安防系统.家居照明系统.家居布线系统.家居网络系统.家庭环境控制系统和影院与多媒体系统以及背景音乐系统等多个智能家居系统子系统的设备. 从目前我国智能设备制造企业 ...

  6. 第18届中国智能家居主题沙龙在北京成功举办

    面对小米难题,智能家居的三种解法 --第18届中国智能家居主题沙龙举办 79元智能手环.59元智能插座.智能路由器.智能家居样板间以及智能家居生态系统······小米进军智能家居早已不是秘密,但以低价 ...

  7. 智能家居行业发展趋势洞察

    近年来,在智能化.自动化高新技术的驱动下,智能家居行业进入了飞速发展时期.一方面,AI.loT.边缘计算全面赋能智能家居:另一方面,中国的房地产行业正在从上半场的"增量开发",切换 ...

  8. html控制智能家居,一种通过web控制的智能家居系统的制作方法

    一种通过web控制的智能家居系统的制作方法 [专利摘要]本发明提供了一种通过web控制的智能家居系统,包括移动终端,以MIPS芯片为核心的WiFi转串口模块,STM32控制模块,DALI控制模块,SI ...

  9. 物联网智能家居实训系统

    物联网智能家居实训系统 产品型号和技术规格 系统概述 物联网智能家居实训系统,以智能家居行业为背景,对智能家居中多个子系统进行安装调试操作,培养学生的工程技能,使其能够搭建相对复杂的物联网应用系统,并 ...

  10. 金博科技-智能家居APP开发四大分类详解

    随着物联网行业的不断开展,智能家居体系渠道及大数据服务渠道建立,下游设备厂商完善,消费者关于智能设备的承受程度将越来越大,且年纪区别将不再显着.越来越多的企业开始布局开展智能家居APP开发和应用,那么 ...

最新文章

  1. ios打不开html文件,在DocumentsWeb目录中打开UIWebview时,ios-css和js在html文件中无法链接...
  2. 深入浅出妙用 Javascript 中 apply、call、bind
  3. 在C++程序中使用系统热键(附代码)
  4. 备份计算机软件,如何备份电脑里面的软件
  5. 拆解玩具电池充电器:充久了可能会爆,廉价电路方案让人震惊!
  6. channel java_Java Channel
  7. mc服务器语音,浸入式语音(Immersive Voice)|我的世界1.12.2版本
  8. 哈佛结构冯·诺依曼结构
  9. python3 将科学计数法(用E表示的)转化为书写形式的科学计数法的数字
  10. 如何重置计算机服务到默认状态,电脑慢到让人抓狂?教你如何将Windows10还原到出厂设置...
  11. 解决MacBook外置鼠标不灵敏,移动速度慢
  12. 地图定位之3D 地图
  13. AKAZE/KAZE局部特征
  14. WEMOS D1 R1/R2 [ESP8266] + PCA9685 驱动舵机
  15. 多卡聚合智能融合通信设备在智慧房车上的应用
  16. Notion-PC最好用的笔记软件
  17. 从零到一编写一个 spark 程序并提交到集群中运行
  18. 关于linux中cron.daily、weekly、monthly执行时间的问题?
  19. NOSuchKeys: com.aliyun.oss.OSSException: The specified key does not exist.
  20. 红旗linux9支持软件,红旗linux系统下载|红旗Linux操作系统9.0正式版下载(c语言编写) 最新版_数码资源网...

热门文章

  1. 程序包com.sun.istack.internal不存在
  2. 终于把joomla 的 protostar 模版的菜单,从垂直改到水平了
  3. [转]Eclipse下的JavaScript编辑器中文无法保存问题解决
  4. linux rpm 校验软件包中的文件
  5. 浅析透雾监控镜头的四大透雾技术
  6. alsa声音编程介绍
  7. 定制一个网络文件系统
  8. 用OPENCV视觉解数独
  9. 最新的一些开源face alignment及评价
  10. 微型计算机中使用的光盘应属于什么媒体,计算机应用基础练习题