下面的代码是发送普通数值的。(发送普通数值和发送GPS定位的数据有什么区别!?{:soso_e154:} )
#include <stdio.h>
#include <math.h>
int i;
int len;
int times;
int temp;
int t;
char xinlv;
char post[]={//提交数据包
"OST /v1.0/device/12797/sensor/aaaaa/datapoints HTTP/1.1\r\n"
"Host: api.yeelink.net\r\n"
"Accept: */*\r\n"
"U-ApiKey:587407b0532721175514efd429f5dc98\r\n"
"Content-Length: dt\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Connection: close\r\n"
"\r\n"
"{\"value\":xxxxxx}\r\n"
};//xx用来填充数值
char post_reserve[]={//提交数据包
"OST /v1.0/device/12797/sensor/aaaaa/datapoints HTTP/1.1\r\n"
"Host: api.yeelink.net\r\n"
"Accept: */*\r\n"
"U-ApiKey: 587407b0532721175514efd429f5dc98\r\n"
"Content-Length: dt\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Connection: close\r\n"
"\r\n"
"{\"value\":xxxxxx}\r\n"
};//xx用来填充数值
char xinlv_sensor_id[]={"21121"};
/////1
void post_xinlv() //发送温度数据
{ connect();
len=strlen(post);
for(times=0;times<len;times++)
{
if(post[times]=='a'&&post[times+1]=='a'&&post[times+2]=='a')
{
post[times]=xinlv_sensor_id[0];
post[times+1]=xinlv_sensor_id[1];
post[times+2]=xinlv_sensor_id[2];
post[times+3]=xinlv_sensor_id[3];
post[times+4]=xinlv_sensor_id[4];
break;
}
}
temp=0;
len=strlen(post);
temp=Serial.print(xinlv,1);
for(times=0;times<len;times++)
{
if(post[times]=='d'&&post[times+1]=='t')
{
post[times]=(temp+10)/10+0x30;
post[times+1]=(temp+10)%10+0x30;
break;
}
}
for(times=0;times<len;times++)//填改传感器数值,发送报文
{
if(post[times]=='x'&&post[times+1]=='x'&&post[times+2]=='x')
{
Serial1.print(xinlv,1);
times=times+6;
}
Serial1.print(post[times]);
}
Serial1.write(26);//send order
delay(3000);
close();
for(times=0;times<len;times++)
{
post[times]=post_reserve[times];
}
}
////////2
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(115200);
xinlv=0;
}
void loop() {
// put your main code here, to run repeatedly:
// xinlv=xinlv+5;
// if(xinlv==55)
// {xinlv=0; }
post_xinlv();
delay(1000);
}
void connect()//TCP连接
{
int i;
// Serial1.print("ati\r\n");
// delay(1000);
// Serial1.print("ati\r\n");
// delay(1000);
Serial1.print("ati\r\n");
delay(1000);
delay(500);
Serial1.print("AT+CGCLASS=\"B\"\r\n");
delay(500);
Serial1.print("AT+CGDCONT=1,\"IP\",\"CMNET\"\r\n");
delay(500);
Serial1.print("AT+CGATT=1\r\n");
delay(500);
Serial1.print("AT+CIPCSGP=1,\"CMNET\"\r\n"); //uninet
delay(500);
Serial1.print("AT+CLPORT=\"TCP\",\"2000\"\r\n");
delay(500);
Serial1.print("AT+CIPSTART=\"TCP\",\"42.96.164.52\",\"80\"\r\n"); //the ip addr to yeelink
delay(500);
for(i=0;i<=3;i++)
{
delay(1000);
}
Serial1.print("AT+CIPSEND\r\n");
delay(500);
}
void close()//结束TCP连接
{
Serial1.print("AT+CIPCLOSE=1\r\n");
delay(500);
Serial1.print("AT+CIPSHUT\r\n");
delay(500);
}
|