基于树莓派的安卓智能门禁控制

摄像头+mjpg-streamer的配置

sudo apt-get install libjpeg8-dev #JPEG支持库
sudo apt-get install imagemagick
sudo apt-get install libv4l-dev #4l是小写"L"
sudo apt-get install cmake #下载编译工具

1.安装GIT

sudo apt-get install git
git clone https://github.com/jacksonliam/mjpg-streamer.git

3.进入下载目录后进入左侧路径

cd mjpg-streamer/mjpg-streamer-experimental

4.编译

make all 

5.安装

sudo make install

6.执行脚本程序

./start.sh

7.在浏览器输入 http://IP地址:8080,回车 显示如下页面,点击页面左侧,Stream栏,显示监视画面

在这里插入图片描述

摄像头代码

进入mjpg文件夹 cd mjpg-streamer/mjpg-streamer-experimental 在里面写代码(也可以在桌面,但是system要加脚本的绝对路径)。
我是直接在里面写代码然后直接调用当前目录下的start.sh

void cameraCrl()
{
	system("./start.sh");
}

完整代码

服务端代码:

#include <sys/types.h> 
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <wiringPi.h>
#include <stdlib.h>
#include <string.h>
#include <wiringSerial.h>
#include <pthread.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>


int s_fd;
struct sockaddr_in s_addr;

void WiringPi_Init()  //wiringPi库初始化
{
	
	if(wiringPiSetup()==-1)
	{
		printf("setup fail\n");
		exit(-1);
	}

	pinMode(7,OUTPUT);
	digitalWrite (7, HIGH);
	pinMode(1,OUTPUT);
	digitalWrite (1, HIGH);

}

int ftpServer_Init(char **argv)  //socket服务端初始化
{

	memset(&s_addr,0,sizeof(struct sockaddr_in));

	s_fd = socket(AF_INET,SOCK_STREAM,0);
	if(s_fd == -1)
	{
		perror("socket\n");
		exit(-1);
	}

	s_addr.sin_family = AF_INET;
	s_addr.sin_port = htons(atoi(argv[2]));
	inet_aton(argv[1],&s_addr.sin_addr);

	bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));

	listen(s_fd,10);

	return s_fd;

}


void ftpServer_Crl()    //服务端线程
{

	int reg;
	char cmd[64];
	int c_fd;
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int clen = sizeof(struct sockaddr_in);

	while(1)
	{
		c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen);
		if(c_fd == -1)
		{
			perror("accept\n");

		}
		printf("get connect: %s\n",inet_ntoa(c_addr.sin_addr));
		if(fork() == 0)
		{
			while(1)
			{
				memset(&cmd,'\0',sizeof(cmd));
				read(c_fd,cmd,sizeof(cmd));
				if(strcmp(cmd,"k") == 0)
				{
					digitalWrite (7, LOW);
					printf("开门");

				}else if(strcmp(cmd,"g") == 0)
				{
					digitalWrite (7, HIGH);
					printf("关门");
				}
				else if(strcmp(cmd,"a") == 0)
				{
					digitalWrite (1, LOW);
					printf("开灯");
				}
				else if(strcmp(cmd,"b") == 0)
				{
					digitalWrite (1, HIGH);
					printf("关灯");
				}
			}
		}

	}
}

void cameraCrl()  //摄像头线程
{
	system("./start.sh");
}

int main(int argc,char **argv)
{

	pthread_t th1;
	pthread_t th2; 

	WiringPi_Init();

	if(argc != 3)
	{
		printf("param no three\n");
		exit(-1);
	}
	ftpServer_Init(argv);

	pthread_create(&th2,NULL,(void *)ftpServer_Crl,NULL);
	pthread_create(&th1,NULL,(void *)cameraCrl,NULL);
	
	pthread_join(th1,NULL);
	pthread_join(th2,NULL);

	return 0;
}
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页