linux下向yeelink添加数据点

初步简单的实现 并不完整

linux下最简单的实现方式就是curl来实现

请求实例 (运用curl):
curl --request POST --data-binary @datafile.txt --header "U-ApiKey: YOUR_API_KEY_HERE" http://api.yeelink.net/v1.0/device/12/sensor/3/datapoints
需要在HTTP Header中增加API Key来授权写入操作, 具体请参照API Key. 支持一次传送一个数据或者批量上传.



这里的代码是纯c实现的

为了将来方便移植到lwip

目前函数都已经修改

这是最基本的测试


三个文件

client.c  发送http请求给服务器

server.c 用来测试client发送的数据对不对

makefile用来编译上面的两个文件

使用时之需要执行client.c即可 没写退出函数 所以暂时用 ctrl+c结束进程


使用时先make

然后./client



client.c  发送http请求给服务器

/*client.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#define PORT 80
#define REMOTE_IP "202.136.56.203"

//#define PORT 4321
//#define REMOTE_IP "192.168.1.104"

int   main(int argc,char *argv[])
{   
   int s ;
   struct sockaddr_in addr ;
    char mybuffer[256];
	char *str1="POST /v1.0/device/1847/sensor/2326/datapoints HTTP/1.0\r\nHost: api.yeelink.net\r\nAccept: */*\r\n";
	char *str2="U-ApiKey: c3b3d0671f3d962ee2b8aaa1cece81\r\nContent-Length: 12\r\nContent-type: application/json;charset=utf-8\r\n";
	char *str3="\r\n";
	char *str10="{\"value\":14}\r\n";

   if( (s=socket(AF_INET,SOCK_STREAM,0))<0 )  //IPV4 TCP
   {
      perror("socket");
      exit(1);
   }
   else
   {
    	printf("socket created .\n");
    	printf("socked id: %d \n",s);
	}

   bzero(&addr,sizeof(addr));
   addr.sin_family =AF_INET;  //IPV4
   addr.sin_port=htons(PORT); //SERVER PORT
   addr.sin_addr.s_addr=inet_addr(REMOTE_IP);  //SERVER IP

   if(connect(s,(struct sockaddr *)&addr,sizeof(addr))<0)
   {
      perror("connect");
      exit(1);
   }
   else
   {
   	   printf("connected ok!\n");
   	   printf("remote ip:%s\n",REMOTE_IP);
   	   printf("remote port:%d\n",PORT);
    }

	bzero(mybuffer,sizeof(mybuffer));

//send http request
	printf("tcp send start!--");
   if(send(s,str1,strlen(str1),0)<0)
      {
         perror("send");
         exit(1);
      }
   if(send(s,str2,strlen(str2),0)<0)
      {
         perror("send");
         exit(1);
      }
   if(send(s,str3,strlen(str3),0)<0)
      {
         perror("send");
         exit(1);
      }
//body
   if(send(s,str10,strlen(str10),0)<0)
      {
         perror("send");
         exit(1);
      }

   printf("tcp send ok!--");

   recv(s ,mybuffer,sizeof(mybuffer),0);
   printf("%s\n",mybuffer);
   printf("enter os");
   while(1)
   {
         bzero(mybuffer,sizeof(mybuffer));
         recv(s ,mybuffer,sizeof(mybuffer),0);
  //       printf("received:%s\n",mybuffer);
   }
}


server.c 用来测试client发送的数据对不对

/*server.c*/

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>

#define PORT			4321
#define BUFFER_SIZE		1024
#define MAX_QUE_CONN_NM	5

int main()
{
	struct sockaddr_in server_sockaddr, client_sockaddr;
	int sin_size, recvbytes;
	int sockfd, client_fd;
	char buf[BUFFER_SIZE];
	
	/*建立socket连接*/
	if ((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1)
	{
		perror("socket");
		exit(1);
	}
	printf("Socket id = %d\n",sockfd);
	
	/*设置sockaddr_in 结构体中相关参数*/
	server_sockaddr.sin_family = AF_INET;
	server_sockaddr.sin_port = htons(PORT);
	server_sockaddr.sin_addr.s_addr = INADDR_ANY;
	bzero(&(server_sockaddr.sin_zero), 8);
	
	int i = 1;/* 使得重复使用本地地址与套接字进行绑定 */
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));	
	
	/*绑定函数bind*/
	if (bind(sockfd, (struct sockaddr *)&server_sockaddr, sizeof(struct sockaddr))== -1)
	{
		perror("bind");
		exit(1);
	}
	printf("Bind success!\n");
	
	/*调用listen函数*/
	if (listen(sockfd, MAX_QUE_CONN_NM) == -1)
	{
		perror("listen");
		exit(1);
	}
	printf("Listening....\n");
	
	/*调用accept函数,等待客户端的连接*/
	if ((client_fd = accept(sockfd, (struct sockaddr *)&client_sockaddr, &sin_size)) == -1)
	{
		perror("accept");
		exit(1);
	}
	
	/*调用recv函数接收客户端的请求*/
while(1){
	memset(buf , 0, sizeof(buf));
	if ((recvbytes = recv(client_fd, buf, BUFFER_SIZE, 0)) == -1)
	{
		perror("recv");
		exit(1);
	}else{
	printf("Received a message: %s\n", buf);
	}
}

close(sockfd);
exit(0);
}


makefile用来编译上面的两个文件


EXEC1 = server
EXEC2 = client
OBJS1 = server.o
OBJS2 = client.o
HEADERS =

CC = gcc
INC =
CFLAGS = ${INC} -g 

all:${EXEC1} ${EXEC2}
${EXEC1} : ${OBJS1}
	${CC} ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS1} 

${OBJS1} : ${HEADERS}

${EXEC2} : ${OBJS2}
	${CC} ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS2} 

${OBJS2} : ${HEADERS}

.PHONY : clean
clean :
	-rm -f ${OBJS1} ${EXEC1}
	-rm -f ${OBJS2} ${EXEC2}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值