前言
设备
设备名称 | 作用 |
---|---|
蜂鸣器 | 提醒烟雾报警 |
MQ-2烟雾报警器 | 检测甲烷、液化气、可燃气体,信号发送给SBC开发板 |
全志H616开发板 | SBC开发板,控制各类外设 |
继电器组 | 控制灯光或其他电器 |
SG90舵机 | 控制开关门 |
SU-03T语音模块 | 发送指令,控制电器 |
实现效果
嵌入式智能家居(学习两个月成果)
代码实现
单独建立一个文件夹用于舵机开关
mkdir sg90
cd sg90
gcc ./SG90.c -lwiringPi
编译方法
gcc *.c -lwiringPi -lpthread -lm
sudo ./a.out
SG90.c
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <wiringPi.h>
#include <unistd.h>
#define SG90Pin 10
int jd;
int count = 0;
static int i = 0;
void signal_handler(int signum)
{
if(i <= jd){
digitalWrite(SG90Pin, HIGH);
}else{
digitalWrite(SG90Pin, LOW);
}
if(i == 40){
i = 0;
count++;
}
i++;
}
int main(int argc,char** argv)
{
int flag = 1;
if(argc != 2)
printf("sudo ./a.out **\n");
struct itimerval itv;
jd = 0;
wiringPiSetup();
pinMode(SG90Pin, OUTPUT);
//设定定时时间
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 500;
//设定开始生效,启动定时器的时间
itv.it_value.tv_sec = 1;
itv.it_value.tv_usec = 0;
//设定定时方式
if( -1 == setitimer(ITIMER_REAL, &itv, NULL)){
perror("error");
exit(-1);
}
//jd = atoi(argv[1]);
//printf("%d",jd);
//信号处理
signal(SIGALRM,signal_handler);
while(flag){
jd = atoi(argv[1]);
if(count >= 100)
flag = 0;
//printf("input jd: 1-0 2-45 3-90 4-135 \n");
//scanf("%d",&jd);
}
return 0;
}
main.c
#include <pthread.h>
#include "ControlDevice.h"
#include "InputCommand.h"
struct InputCommand *pcommandHead = NULL;
struct Devices *pdeviceHead = NULL;
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 InputCommand* findCommandByName(char *name, struct InputCommand *phead)
{
struct InputCommand *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 Command(struct InputCommand* CmdHandler)
{
struct Devices *tmp =NULL;
if(strcmp("CSHALL",CmdHandler->command) == 0){
tmp = findDeviceByName("smokeAlarm",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
tmp = findDeviceByName("buzzer",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
tmp = findDeviceByName("livingroomLight",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
tmp = findDeviceByName("restaurantLight",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
tmp = findDeviceByName("bedroomLight",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
tmp = findDeviceByName("bathroomLight",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
tmp = findDeviceByName("swimpoolLight",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
tmp = findDeviceByName("lock",pdeviceHead);
if(tmp != NULL) tmp->Init(tmp->pinNum);
printf("Initial device has been done.\n");
}
if(strcmp("OL1",CmdHandler->command) == 0){
tmp = findDeviceByName("livingroomLight",pdeviceHead);
if(tmp != NULL){
tmp->open(tmp->pinNum);
printf("Open livingroom Light.\n");
}
}
if(strcmp("CL1",CmdHandler->command) == 0){
tmp = findDeviceByName("livingroomLight",pdeviceHead);
if(tmp != NULL){
tmp->close(tmp->pinNum);
printf("Close livingroom Light.\n");
}
}
if(strcmp("OL2",CmdHandler->command) == 0){
tmp = findDeviceByName("restaurantLight",pdeviceHead);
if(tmp != NULL){
tmp->open(tmp->pinNum);
printf("Open restaurant Light.\n");
}
}
if(strcmp("CL2",CmdHandler->command) == 0){
tmp = findDeviceByName("restaurantLight",pdeviceHead);
if(tmp != NULL){
tmp->close(tmp->pinNum);
printf("Close restaurant Light.\n");
}
}
if(strcmp("OL3",CmdHandler->command) == 0){
tmp = findDeviceByName("bedroomLight",pdeviceHead);
if(tmp != NULL){
tmp->open(tmp->pinNum);
printf("Open bedroom Light.\n");
}
}
if(strcmp("CL3",CmdHandler->command) == 0){
tmp = findDeviceByName("bedroomLight",pdeviceHead);
if(tmp != NULL){
tmp->close(tmp->pinNum);
printf("Close bedroom Light.\n");
}
}
if(strcmp("OL4",CmdHandler->command) == 0){
tmp = findDeviceByName("bathroomLight",pdeviceHead);
if(tmp != NULL){
tmp->open(tmp->pinNum);
printf("Open bathroom Light.\n");
}
}
if(strcmp("CL4",CmdHandler->command) == 0){
tmp = findDeviceByName("bathroomLight",pdeviceHead);
if(tmp != NULL){
tmp->close(tmp->pinNum);
printf("Close bathroom Light.\n");
}
}
if(strcmp("OD",CmdHandler->command) == 0){
tmp = findDeviceByName("lock",pdeviceHead);
if(tmp != NULL){
tmp->open(tmp->pinNum);
printf("Open door.\n");
}
}
if(strcmp("CD",CmdHandler->command) == 0){
tmp = findDeviceByName("lock",pdeviceHead);
if(tmp != NULL){
tmp->close(tmp->pinNum);
printf("Close door.\n");
}
}
if(strcmp("OLALL",CmdHandler->command) == 0){
tmp = findDeviceByName("livingroomLight",pdeviceHead);
if(tmp != NULL) tmp->open(tmp->pinNum);
tmp = findDeviceByName("restaurantLight",pdeviceHead);
if(tmp != NULL) tmp->open(tmp->pinNum);
tmp = findDeviceByName("bedroomLight",pdeviceHead);
if(tmp != NULL) tmp->open(tmp->pinNum);
tmp = findDeviceByName("bathroomLight",pdeviceHead);
if(tmp != NULL) tmp->open(tmp->pinNum);
tmp = findDeviceByName("swimpoolLight",pdeviceHead);
if(tmp != NULL) tmp->open(tmp->pinNum);
printf("Open all Light.\n");
}
if(strcmp("CLALL",CmdHandler->command) == 0){
tmp = findDeviceByName("livingroomLight",pdeviceHead);
if(tmp != NULL) tmp->close(tmp->pinNum);
tmp = findDeviceByName("restaurantLight",pdeviceHead);
if(tmp != NULL) tmp->close(tmp->pinNum);
tmp = findDeviceByName("bedroomLight",pdeviceHead);
if(tmp != NULL) tmp->close(tmp->pinNum);
tmp = findDeviceByName("bathroomLight",pdeviceHead);
if(tmp != NULL) tmp->close(tmp->pinNum);
tmp = findDeviceByName("swimpoolLight",pdeviceHead);
if(tmp != NULL) tmp->close(tmp->pinNum);
printf("Close all Light.\n");
}
}
void *voiceControlThread(void *data)
{
int nread;
struct InputCommand *voiceHandler = NULL;
voiceHandler = findCommandByName("voice", pcommandHead);
if(voiceHandler == NULL){
printf("find voiceHandler error\n");
pthread_exit(NULL);
}else{
if(voiceHandler->Init(voiceHandler) < 0){
printf("voiceControl init error\n");
pthread_exit(NULL);
}else{
printf("voiceControl init success\n");
}
while(1){
memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
nread = voiceHandler->getCommand(voiceHandler);
if(nread == 0){
printf("No voiceCommand received\n");
}else{
printf("Get VoiceCommand -->%s\n",voiceHandler->command);
Command(voiceHandler);
}
}
}
}
void *smokeAlarmThread(void *data)
{
int smokeStatus;
struct Devices *tmp = NULL;
while(1){
tmp = findDeviceByName("smokeAlarm", pdeviceHead);
if(tmp != NULL){
smokeStatus = tmp->readStatus(tmp->pinNum);
delay(100);
tmp = findDeviceByName("buzzer", pdeviceHead);
if(tmp != NULL){
if( smokeStatus == 0 ){
tmp->open(tmp->pinNum);
}else{
tmp->close(tmp->pinNum);
}
}
}
}
}
int main()
{
if (wiringPiSetup () == -1) {
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
pthread_t voiceControl_thread;
pthread_t smokeAlarm_thread;
pcommandHead = addVoiceControlToInputCommandLink(pcommandHead);
pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addSmokeAlarmToDeviceLink(pdeviceHead);
pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
pdeviceHead = addLockToDeviceLink(pdeviceHead);
pthread_create(&voiceControl_thread,NULL,voiceControlThread,NULL);
pthread_create(&smokeAlarm_thread,NULL,smokeAlarmThread,NULL);
pthread_join(voiceControl_thread, NULL);
pthread_join(smokeAlarm_thread, NULL);
return 0;
}
InputCommand.h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <wiringPi.h>
#include <wiringSerial.h>
struct InputCommand
{
char commandName[128];
char deviceName[128];
char command[32];
int fd;
int s_fd;
char port[12];
char ipAdress[32];
char log[1024];
int (*Init)(struct InputCommand *voice);
int (*getCommand)(struct InputCommand *voice);
struct InputCommand *next;
};
struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead);
InputCommand.h
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
extern char ocrRetBuf[1024];
struct Devices
{
char deviceName[128];
int status;
int pinNum;
int (*Init)(int pinNum);
int (*open)(int pinNum);
int (*close)(int pinNum);
int (*readStatus)(int pinNum);
int (*changeStatus)(int status);
void (*justDoOnce)();
struct Devices *next;
};
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead);
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead);
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead);
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead);
struct Devices* addBuzzerToDeviceLink(struct Devices *phead);
struct Devices* addLockToDeviceLink(struct Devices *phead);
bedroom.c
#include "ControlDevice.h"
int bedroomLightInit(int pinNum)
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int bedroomLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int bedroomLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
int bedroomLightStatus(int status)
{
}
struct Devices bedroomLight = {
.deviceName = "bedroomLight",
.pinNum = 7,
.Init = bedroomLightInit,
.open = bedroomLightOpen,
.close = bedroomLightClose,
.changeStatus = bedroomLightStatus
};
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &bedroomLight;
}else{
bedroomLight.next = phead;
phead = &bedroomLight;
return phead;
}
}
livingroom.c
#include "ControlDevice.h"
int livingroomLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int livingroomLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int livingroomLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int livingroomLightStatus(int status)
{
}
struct Devices livingroomLight = { //定义客厅灯(对象)
.deviceName = "livingroomLight", //名字
.pinNum = 8, //香橙派 16号(wPi)引脚
.Init = livingroomLightInit, //指定初始化函数
.open = livingroomLightOpen, //指定“打开灯”函数
.close = livingroomLightClose, //指定“关闭灯”函数
.changeStatus = livingroomLightStatus
};
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead) //客厅灯(对象)加入设备链表函数
{
if(phead == NULL){
return &livingroomLight;
}else{
livingroomLight.next = phead; //以前的头变成.next
phead = &livingroomLight; //更新头
return phead;
}
}
restaurant.c
#include "ControlDevice.h" //自定义设备类的文件
int restaurantLightInit(int pinNum) //C语言必须要传参,JAVA不用,可直接访问变量的值
{
pinMode(pinNum,OUTPUT); //配置引脚为输出模式
digitalWrite(pinNum,HIGH); //引脚置高电平,断开继电器
}
int restaurantLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW); //引脚置低电平,闭合继电器
}
int restaurantLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH); //引脚置高电平,断开继电器
}
int restaurantLightStatus(int status)
{
}
struct Devices restaurantLight = { //定义餐厅灯(对象)
.deviceName = "restaurantLight", //名字
.pinNum = 5, //香橙派 13号(wPi)引脚
.Init = restaurantLightInit, //指定初始化函数
.open = restaurantLightOpen, //指定“打开灯”函数
.close = restaurantLightClose, //指定“关闭灯”函数
.changeStatus = restaurantLightStatus
};
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead) //餐厅灯(对象)加入设备链表函数
{
if(phead == NULL){
return &restaurantLight;
}else{
restaurantLight.next = phead; //以前的头变成.next
phead = &restaurantLight; //更新头
return phead;
}
}
bathroom.c
#include "ControlDevice.h"
int bathroomLightInit(int pinNum) {
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int bathroomLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int bathroomLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int bathroomLightStatus(int status)
{
}
struct Devices bathroomLight = {
.deviceName = "bathroomLight",
.pinNum = 2,
.Init = bathroomLightInit,
.open = bathroomLightOpen,
.close = bathroomLightClose,
.changeStatus = bathroomLightStatus
};
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &bathroomLight;
}else{
bathroomLight.next = phead;
phead = &bathroomLight;
return phead;
}
}
buzzer.c
#include "ControlDevice.h"
int buzzerInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int buzzerOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int buzzerClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
struct Devices buzzer = {
.deviceName = "buzzer",
.pinNum = 11,
.Init = buzzerInit,
.open = buzzerOpen,
.close = buzzerClose,
};
struct Devices* addBuzzerToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &buzzer;
}else{
buzzer.next = phead;
phead = &buzzer;
return phead;
}
}
lock.c
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <wiringPi.h>
#include "ControlDevice.h"
static int count = 0;
int angle;
int lockInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
system("sudo ./sg90/a.out 3");
}
int lockOpen(int pinNum)
{
system("sudo ./sg90/a.out 1");
}
int lockClose(int pinNum)
{
system("sudo ./sg90/a.out 3");
}
int lockStatus(int status)
{
}
struct Devices lock = { //定义客厅灯(对象)
.deviceName = "lock", //名字
.pinNum = 10, //香橙派 16号(wPi)引脚
.Init = lockInit, //指定初始化函数
.open = lockOpen, //指定“打开灯”函数
.close = lockClose, //指定“关闭灯”函数
.changeStatus = lockStatus
};
struct Devices* addLockToDeviceLink(struct Devices *phead) //客厅灯(对象)加入设备链表函数
{
if(phead == NULL){
return &lock;
}else{
lock.next = phead; //以前的头变成.next
phead = &lock; //更新头
return phead;
}
}
firewarn.c
#include "ControlDevice.h"
int smokeAlarmInit(int pinNum)
{
pinMode(pinNum,INPUT);
//digitalWrite(pinNum,HIGH);
}
int smokeAlarmReadStatus(int pinNum)
{
return digitalRead(pinNum);
}
int smokeAlarmStatus(int status)
{
}
struct Devices smokeAlarm = {
.deviceName = "smokeAlarm",
.pinNum = 6,
.Init = smokeAlarmInit,
.readStatus = smokeAlarmReadStatus,
.changeStatus = smokeAlarmStatus
};
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &smokeAlarm;
}else{
smokeAlarm.next = phead;
phead = &smokeAlarm;
return phead;
}
}
注意事项
问题1:三线程同时运行,有时候无法正常进行开关灯操作。
初步排查:烟雾线程占用CPU100%,怀疑是死循环。
解决办法:加入延时函数。
问题2:控制电器之前必须进行初始化,让设备链表相互连接。
方法:语音控制。
SU-03T语音模块可前往智能公元官网设计固件并烧录进去,文中所需命令在main.c中。