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

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

文章目录


前言

提示:本章主要讲解智能家居串口开发和socket开发


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

一、InputCommand.h

#include <stdlib.h>
#include <wiringPi.h> 

struct InputCommander
{
	char commandName[128];
	char deviceName[128];
	char command[32];
	int (*Init)(struct InputCommander *voicer, char *ipAdress,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* addvoiceToInputCommandLink(struct InputCommander *phead);
  struct InputCommander* addsocketToInputCommandLink(struct InputCommander *p

二、voiceControl.c

#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "InputCommand.h"
/************************语音识别********************/
/************************2021.10.2******  ***********/ 
/***************************************************/ 
/***************************************************/ 
/***************************************************/ 
/***************************************************/ 
/***************************************************/ 


 int voiceInit (struct InputCommander *voicer, char *ipAdress,char *port)
 {
 	int fd;
 	
 	if ((fd = serialOpen(voicer->deviceName,9600)) == -1)
 	{
 			exit(-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 voiceControl = {
 	.commandName = "voice",
 	.deviceName = "/dev/ttyAMAO",
 	.command = {'\0'},
 	.Init = voiceInit,
 	.getCommand = voiceGetCommand,
 	.log = {'\0'},
 	.next = NULL
 	
 };
 

  struct InputCommander* addvoiceToInputCommandLink(struct InputCommander *phead)
 {
 	if(phead == NULL){
 		phead = &voiceControl;
 		return phead;
	 }else{
	 	voiceControl.next = phead;
	 	phead = &voiceControl;
	 	return phead;
	 }
 }

 

三、socketControl.c

#include <wiringPi.h>
#include <wiringSerial.h>
#include <unistd.h>
#include "InputCommand.h"

#include <stdlib.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
/***********************socker net********************/
/************************2021.10.2******  ***********/ 
/***************************************************/ 
/***************************************************/ 
/***************************************************/ 
/***************************************************/ 
/***************************************************/ 


 int soketInit (struct InputCommander *socketMes, char *ipAdress,char *port)
 {
 	int s_fd;
 	
 	struct sockaddr_in s_addr;
 
 	
 	memset(&s_addr,0,sizeof(struct sockaddr_in));
 
 	
 	//1.socket
 	s_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);
	 
	 //2.bind
	 bind(s_fd , (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
	 
	 //3.listen
	 listen(s_fd ,10);   
	 
	socketMes->sfd = s_fd;
	 return s_fd;
 }
 

int soketGetCommand(struct InputCommander *socketMes)
{
	int c_fd;
	int n_read;
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int chen = sizeof(struct sockaddr_in);
	
	c_fd = accept(socketMes->sfd,(struct sockaddr *)&c_addr,&chen);

	n_read = read(c_fd,socketMes->command,sizeof(socketMes->command));
			if(n_read == -1){
				perror("read");
			}else if(n_read>0){
				printf("\nget: %d\n",n_read);
			}else{
				printf("client quit\n");  // = 0 客户端退出 
			}
			
			return n_read ;
}
 
 struct InputCommander socketControl = {
 	.commandName = "soketServer",
 	.port = "8088",
 	.ipAddress = "192.168.31.76",
 	.command = {'\0'},
 	.Init = soketInit,
 	.getCommand = soketGetCommand,
 	.log = {'\0'},
 	.next = NULL
 	
 };
 

  struct InputCommander* addsocketToInputCommandLink(struct InputCommander *phead)
 {
 	if(phead == NULL){
 		phead = &socketControl;
 		return phead;
	 }else{
	 	socketControl.next = phead;
	 	phead = &socketControl;
	 	return phead;
	 }
 }


 

四、编译

gcc main.c bathroomLight.c livegroomLight.c restaurantLight.c upstairLight.c fire.c voiceControl.c socketControl.c  -lwiringPi -o test1

总结

提示:这里对文章进行总结:
今天给代码框架加入socket和语音识别模块代码,仅测试。

上一篇:画叠加的等边三角形


下一篇:蓝牙源码学习笔记