补充剩余的功能
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补全即可
上一篇:智能家居项目(十四)