智能家居项目(十五)

补充剩余的功能

camrea.c

#include "contrlDevices.h"
 
#define true 1
#define false 0

typedef unsigned int bool;
char buf[1024] = {'\0'};

size_t readData(void *ptr, size_t size, size_t nmemb, void *stream)			//回调函数
{
	strncpy(buf, ptr, 1024);
}

char* getPicBase64FromFile(char* filePath)		//获取图片的base64流
{
	char* bufPic;
	char cmd[256] = {'\0'};
	sprintf(cmd,"base64 %s > tempFile",filePath);
	system(cmd);
	
	int fd = open("./tempFile",O_RDWR);
	int fileLen = lseek(fd,0,SEEK_END);
	lseek(fd,0,SEEK_SET);
	
	bufPic = (char *)malloc(fileLen + 2);
	memset(bufPic, '\0',fileLen+2);
	read(fd,bufPic,fileLen);
	close(fd);
	system("rm -f ./tmpFile");
	
	return bufPic;
}

bool cameraContrl_postUrl(struct Devices *camera)
{
	CURL *curl;
	CURLcode res;
	char *postString;
 
	char *img1;
	char *img2;
 
	system("raspistill -w 700 -h 525 -o ./visitor.jpg");
 
	img1 = getBase64("./visitor.jpg");
	img2 = getBase64("./me1.jpg");
 
	int len = strlen(camera->key)+strlen(camera->secret)+strlen(img1)+strlen(img2)+124;
	postString = (char *)malloc(strlen(camera->key)+strlen(camera->secret)+strlen(img1)+strlen(img2)+124);
	memset(postString,'\0',len);
 
	sprintf(postString,"&img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",  
			img1,img2,camera->key,camera->secret,camera->typeId,camera->format);
 
	system("rm visitor.jpg");
 
	curl = curl_easy_init();
 
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); // 指定cookie文件
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postString);    // ָ指定post内容,用$符号拼接
		curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do");   // ָ指定url
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData1); //将返回的http头输出到fp指向的文件
		res = curl_easy_perform(curl);
		res = curl_easy_perform(curl);
		//printf("ok = %d\n",res);
	
		printf("%s\n",resultBuf);
		if(strstr(resultBuf,"是") != NULL){
				printf("same\n");
		}else{
				printf("different\n");
		}
 
		curl_easy_cleanup(curl);
	}
	return true;
}
 
struct Devices cameraContrl = {
	.deviceName = "camera",
	.key = "11111",
	.secret = "11111",
	.typeId = 21,
	.format = "xml",
	.cameraInit = cameraContrl_postUrl,
	.next = NULL
};
 
struct Devices *addCameraContrlToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &cameraContrl;
	}else{
		cameraContrl.next = phead;
		phead = &cameraContrl;
	}
	return phead;
}

client_wemos.c (树莓派作为客户端 连接wemos D1服务器)

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringSerial.h>
#include <unistd.h>
 
#include <sys/types.h>          
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
#include "inputCommand.h"
 
int clientInit(struct InputCommander *client,char *ipAdress,char *port)
{
        struct sockaddr_in addr;
 
        memset(&addr,0,sizeof(struct sockaddr_in));
 
        //1.socket 创建套接字
        int s_fd=socket(AF_INET, SOCK_STREAM,0);        
        if (s_fd==-1)
        {
                perror("socket");
                exit(-1);
        }
 
        addr.sin_family=AF_INET;
        addr.sin_port=htons(atoi(client->port));
        inet_aton(client->ipAdress,&addr.sin_addr);
 
        //2.connect 连接服务器
        if(connect(s_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1)  //这里是连接wemosD1
        {
                perror("connect");
                exit(-1);
        }
        printf("client wemosD1 connect....\n");
 
		client->sfd = s_fd;
		return s_fd;
 
}
 
struct InputCommander clientContrl = {
	.commandName = "client",       //加入到控制链表的名字
	.comand = {'\0'},              //命令
	.port = "8888",                //wemosD1 端口号
	.ipAdress = "172.20.10.12",    //wemosD1 IP地址
	.Init = clientInit,            //树莓派客户端初始化
	.next = NULL
};
 
struct InputCommander *addclientContrlToInputCommanderLink(struct InputCommander *phead)
{	
	if(phead == NULL){
		return &clientContrl;
	}else{
		clientContrl.next = phead;
		phead = &clientContrl;
	}
	return phead;	
}

剩下的头文件和manPro.c补全即可

上一篇:智能家居项目(十四)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GeniusAng丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值