Android Smart Access Control Based on Raspberry Pi

Camera+mjpg-streamer configuration

sudo apt-get install libjpeg8-dev #JPEG support library
sudo apt-get install imagemagick
sudo apt-get install libv4l-dev #4l is lowercase "L"
sudo apt-get install cmake #download compilation tools

1. Install GIT

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

3. After entering the download directory, enter the left path

cd mjpg-streamer/mjpg-streamer-experimental

4. Compile

make all 

5. Installation

sudo make install

6. Execute script program

./start.sh

7. Enter http://IP address:8080 in the browser, press Enter to display the following page, click the Stream column on the left side of the page to display the monitoring screen

Insert picture description here

Camera code

Enter the mjpg folder cd mjpg-streamer/mjpg-streamer-experimental and write the code inside (it can also be on the desktop, but the absolute path of the script must be added to the system).
I write the code directly in it and call start.sh in the current directory directly

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

Complete code

Server code:

#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;
}

Guess you like

Origin blog.csdn.net/hhltaishuai/article/details/109291291