• 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 上的展示数据如下图:

  • 相关阅读:
    4. 单向循环链表
    3. 单向链表
    2. 队列的实现
    1. 时间复杂度(大O表示法)以及使用python实现栈
    Ubuntu16.04LTS安装集成开发工具IDE: CodeBlocks 和Eclipse-cdt
    Ubuntu16.04搭建各种开发环境的IDE: QT5 , CodeBlocks ,eclipse-cdt, PyCharm
    python2和python3 分别连接MySQL的代码
    面向对象基础
    文件的处理
    离散表(哈希表),深浅拷贝
  • 原文地址:https://www.cnblogs.com/sn-dnv-aps/p/4388433.html
Copyright © 2020-2023  润新知