• linux 下上传 datapoint数据到yeelink 修改版本


     
      1 /*client.c*/
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <netinet/in.h>
      6 #include <arpa/inet.h>
      7 #include <unistd.h>
      8 #include <fcntl.h>
      9 #include <sys/stat.h>
     10 #include <sys/types.h>
     11 #include <sys/socket.h>
     12 #define PORT 80
     13 #define REMOTE_IP "42.96.164.52" //api.yeelink.net = 42.96.164.52
     14 
     15 //#define PORT 4321
     16 //#define REMOTE_IP "192.168.1.104"
     17 
     18 
     19 // int GetCpuTemp() {
     20 //char *GetCpuTemp() {
     21 float GetCpuTemp() {
     22 
     23     int fd, size;
     24     int temp = 0;
     25    char buffer[15] = { };
     26    fd = open("/sys/class/thermal/thermal_zone0/temp", O_RDONLY);
     27    size = read(fd,buffer, sizeof(buffer));
     28    close(fd);
     29     int tmp;
     30   // strcpy(tmp,buffer);
     31 
     32    temp = atoi(buffer);
     33    float tf = atof(buffer);
     34    printf("float temp = %2.2f
    ", tf/1000);
     35 
     36   // printf("temp = %d
    ",temp / 1000);
     37 
     38   // sprintf(tmp,"%d",temp / 1000);
     39   // printf(" temp1 =%s
    ",tmp);
     40   // printf(" temp2 = %s
    ",buffer);
     41 
     42   // return (temp/1000) ;
     43      return (tf/1000);
     44 }
     45 
     46 
     47 
     48 int   main(int argc,char *argv[])
     49 {
     50    int s ;
     51    struct sockaddr_in addr ;
     52     char mybuffer[256];
     53         char *str1="POST /v1.0/device/19374/sensor/33945/datapoints HTTP/1.0
    Host: api.yeelink.net
    Accept: */*
    ";
     54         char *str2="U-ApiKey: 108968b03a7e9334b2aca7c45b199dee
    Content-Length: 15
    Content-type: application/json;charset=utf-8
    "; //
    // 此处的Content-Length: 15是决定改善的数据的长度 = strlen("values":33.35) = 15
    55 char *str3=" "; 56 char *str10; 57 printf("GetCpuTemp() = %2.2f ",GetCpuTemp()); 58 59 sprintf(str10,"{"value":%2.2f} ",GetCpuTemp()); 60 // str10 = "{"value":33.0} "; 61 printf("str10 = %s ", str10); 62 // char *str10="{"value":14} "; 63 // printf("str10 = %s ",str10); 64 65 if( (s=socket(AF_INET,SOCK_STREAM,0))<0 ) //IPV4 TCP 66 { 67 perror("socket"); 68 exit(1); 69 } 70 else 71 { 72 printf("socket created . "); 73 printf("socked id: %d ",s); 74 } 75 76 bzero(&addr,sizeof(addr)); 77 addr.sin_family =AF_INET; //IPV4 78 addr.sin_port=htons(PORT); //SERVER PORT 79 addr.sin_addr.s_addr=inet_addr(REMOTE_IP); //SERVER IP 80 81 if(connect(s,(struct sockaddr *)&addr,sizeof(addr))<0) 82 { 83 perror("connect"); 84 exit(1); 85 } 86 else 87 { 88 printf("connected ok! "); 89 printf("remote ip:%s ",REMOTE_IP); 90 printf("remote port:%d ",PORT); 91 } 92 93 bzero(mybuffer,sizeof(mybuffer)); 94 95 //send http request 96 printf("tcp send start!--"); 97 if(send(s,str1,strlen(str1),0)<0) 98 { 99 perror("send"); 100 exit(1); 101 } 102 if(send(s,str2,strlen(str2),0)<0) 103 { 104 perror("send"); 105 exit(1); 106 } 107 if(send(s,str3,strlen(str3),0)<0) 108 { 109 perror("send"); 110 exit(1); 111 } 112 //body 113 if(send(s,str10,strlen(str10),0)<0) 114 { 115 perror("send"); 116 exit(1); 117 } 118 119 printf("tcp send ok!--"); 120 121 recv(s ,mybuffer,sizeof(mybuffer),0); 122 printf("%s ",mybuffer); 123 printf("enter os"); 124 125 while(1 ) 126 { 127 bzero(mybuffer,sizeof(mybuffer)); 128 recv(s ,mybuffer,sizeof(mybuffer),0); 129 // printf("received:%s ",mybuffer); 130 if (strlen(mybuffer) == 0 ){ // if strlen(mybuffer) == 0, exit the loop 131 return 0; 132 } 133 } 134 return 0; 135 }

     修改传递数据的数值参考自:http://home.eeworld.com.cn/my/space-uid-487728-blogid-236541.html

    编译方法

         gcc -o upload_cpu_temp client.c 

        ./upload_cpu_temp 

       运行效果如下图:

       

     上传float数值到yeelink 

        yeelink 上的展示数据如下图:

  • 相关阅读:
    Debug和Release版本区别
    清空模拟器中的app
    在项目中移除CocoaPods
    设置导航栏 self.navigationItem.titleView 居中
    Verify the Developer App certificate for youraccount is trusted on your device
    字典转json
    self.navigationController.navigationBar.translucent = YES航栏的属性默认 YES是透明效果并且主view不会偏移 NO是导航栏不透明 主view会向下偏移64px
    归档-对模型数组对象(存储到本地的plist文件)也数组里存放的是模型
    FMDB存储模型对象(以二进制存储)用NSKeyedArchiver archivedDataWithRootObject序列号,NSKeyedUnarchiver unarchiveObjectWithData反序列化(重点坑是sql语句@"insert into t_newsWithChannel (nwesName,newsType) values (?,?)")一定要用占位符
    根据日期计算发布时间段(NSCalendar)
  • 原文地址:https://www.cnblogs.com/sn-dnv-aps/p/4388433.html
Copyright © 2020-2023  润新知