树莓派智能家居系统(2)

发布时间:2022-07-04 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了树莓派智能家居系统(2)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

树莓派智能家居系统(2)


文章目录

  • 树莓派智能家居系统(2)
  • 前言
  • 一、Dvices.h
  • 二、基于结构体添加外设
    • 1.引入bathrooMLight
    • 2.main.c
    • 3.编译测试
  • 总结


前言

今天我们进入正式的智能家居系统的架构开发,我们先创建好工程文件,将所需实现的功能一一创建在工程项目内,运用c语言所学链表知识逐一实现。


提示F1a;以下是本篇文章正文内容,下面案例可供参考

一、Dvices.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 status); // 火灾报警器 
	int (*changeStatus)(int status);
	
	struct Devices *next;
	
}


struct Dvices* addBathrommLightToDeviceLink(struct Dvice *phead)

二、基于结构体添加外设

1.引入bathroomLight

代码如下(示例):

#include "contrlDevices.h"
#include <fcntl.h>
#include<stdlib.h>
#include <stdio.h>

int bathroomLightOpen(int pinNum)
{
	
	digitalWrite(pinNum,LOW);
	PRintf("open successn");
} 

int bathroomLightClose(int pinNum)
{
	
	digitalWrite(pinNum,HIGH);
	printf("open failn");
} 

int bathroomLightCloseInit(int pinNum)
{
	pinMode(pinNum,OUTPUT);
	digitalWrite(pinNum,HIGH);
}

int bathroomLightCloseStatus(int status)
{
	
}

struct Devices bathroomLight = {
	.deviceName = "bathroomLight",
	.pinNum = 22,
	.open  = bathroomLightOpen,
	.close = bathroomLightClose,
	.deviceInit = bathroomLightCloseInit,
	.changeStatus = bathroomLightCloseStatus
};

struct Devices* addBathrommLightToDeviceLink(struct Devices *phead)
 {
 	if(phead == NULL){
 		phead = &amp;bathroomLight;
 		return phead;
	 }else{
	 	bathroomLight.next = phead;
	 	phead = &bathroomLight;
	 	return phead;
	 }
 }


2.main.c

代码如下(示例):

#include <stdio.h>
#include <string.h> 
#include "contrlDevices.h"

struct Devices* findDevicesByName(char *name,struct Devices *phead)  //遍历链表 
{
	struct Devices *tmp = phead;
	
	if(phead == NULL){
		printf("NULLn");
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->deviceName,name) == 0){
				return tmp;
			}
			tmp = tmp ->next;
			
		}
		return NULL;	
	}
}

int main ()
{
//	char *name = "bathroomLight" ;       //仅测试用,这里遍历bathroomLight 
	char buf[128] = {''};
	struct Devices *tmp ;
	if(-1==wiringPiSETUP())
	{
		return -1;
	}
	struct Devices *phead = NULL;  //添加phead 用于连接链表 s 
	phead=addBathrommLightToDeviceLink(phead); //添加元素 
	phead=addLivegroomLightToDeviceLink(phead);
	phead=addUpstairLightToDeviceLink(phead);

	while(1)
	{
	
		printf("please input bathroomLight/livegroomLight/upstairLightn");
		scanf("%s",buf);
		tmp = findDevicesByName(buf,phead);
		if (tmp != NULL){
		tmp->deviceInit(tmp->pinNum); //初始化tmp指向device功能 
		tmp->open(tmp->pinNum);	//实现tmp指向地址功能 
		
	}
	 } 


	return 0;
}

3.编译测试

发送到树莓派进行测试代码是否可以实现。 利用unbunto 发送文件或者fileZilla 这里我使用的是fileZilla,百度下载即可,

树莓派智能家居系统(2)

输入树莓派ip地址,用户名及密码

sftp://192.168.31.76

连接成功以后出现右边栏,如图,

树莓派智能家居系统(2)

gcc *.c 

发现使用gcc *.c容易报错

gcc main.c bathroomLight.c -wiringPi -0 test1

添加库编译即可解决

总结

提示:这里对文章进行总结:我们创建了基本的智能家居架构,外设可由自己需要添加。

脚本宝典总结

以上是脚本宝典为你收集整理的树莓派智能家居系统(2)全部内容,希望文章能够帮你解决树莓派智能家居系统(2)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。