本次测试将CPU温度上传至OneNET平台。OneNET是由中国移动打造的PaaS物联网开放平台。
1.1http协议接入OneNET平台可以参考官方手册http://open.iot.10086.cn/doc,在此不再赘述。
1.2记录下你的设备ID和API。
1.3然后复制进代码相应位置
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp"
- #define SIZE 32
- #define APi "xxxAPIXXX="
- #define Id "XXIDXX"
- //IP:183.230.40.33 端口号:80
- #define Ip "183.230.40.33"
- #define Port "80"
- int read_temp(void)
- {
- FILE *fp;
- int temp;
- char buf[SIZE];
-
- fp=fopen(TEMP_PATH,"r");
- if(fp==NULL)
- {
- printf("open errorn");
- return -1;
- }
-
- if(fread(buf,1,SIZE,fp)>=0)
- {
- puts("read successful");
-
- temp=atoi(buf);
- printf("temp:%dn",temp);
- }
- else
- {
- printf("read errorn");
- return -1;
- }
-
-
- if(fclose(fp)==-1)
- {
- printf("close errorn");
- return -1;
- }
- return temp;
-
- }
- uint32_t HTTP_PostPkt(char *pkt, char *key, char *devid, char *dsid, char *val)
- {
- char dataBuf[100] = {0};
- char lenBuf[10] = {0};
- *pkt = 0;
- sprintf(dataBuf, ",;%s,%s", dsid, val); //采用分割字符串格式:type = 5
- sprintf(lenBuf, "%d", strlen(dataBuf));
- strcat(pkt, "POST /devices/");
- strcat(pkt, devid);
- strcat(pkt, "/datapoints?type=5 HTTP/1.1rn");
- strcat(pkt, "api-key:");
- strcat(pkt, key);
- strcat(pkt, "rn");
- strcat(pkt, "Host:api.heclouds.comrn");
- strcat(pkt, "Content-Length:");
- strcat(pkt, lenBuf);
- strcat(pkt, "rnrn");
- strcat(pkt, dataBuf);
- return strlen(pkt);
- }
- int main(void)
- {
- int socke;
- char tempStr[5];
- int temp;
- char HTTP_Buf[400]={0};
- int len;
-
- struct sockaddr_in serv_addr;
-
- socke=socket(PF_INET,SOCK_STREAM,0);
- if(socke == -1)
- {
- printf("socket error!!");
- exit(1);
- }
-
- memset(&serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr=inet_addr(Ip);
- serv_addr.sin_port=htons(atoi(Port));
-
- if(connect(socke,(struct sockaddr*)&serv_addr,sizeof(serv_addr))== -1)
- {
- printf("error connect!n");
- exit(1);
- }
-
- temp=read_temp();
- sprintf(tempStr,"%d",temp);
- len=HTTP_PostPkt(HTTP_Buf,APi,Id,"temp",tempStr);
- write(socke,HTTP_Buf,len); //write to https
- HTTP_Buf[len] = 0;
- printf("Message from server: %s", HTTP_Buf);
- close(socke);
-
- return 0;
-
- }
复制代码
1.4接下来就是编译
gcc ctemp.c -o ctemp
1.5返回onenet平台就可以看到数据了
2接下来是m1 plus 读串口测试。
本次用到的是UART1,对应8 10脚。
pi的TX--RX
pi的RX--TX
0
|
|
|
|