求大佬带飞
模块是GA6-Bmini利用该模块和uno板把信息发送到Onenet上。问题是第一步AT都没有返回OK值,下面是我的测试代码,#include<SoftwareSerial.h>
#define Success 1U
#define Failure 0U
char device_id[] = "534897579"; //修改为自己的设备ID
char API_KEY[] = "X3xnk9a1xyAi4OS73Y9VNTNdKQE="; //修改为自己的API_KEY
char sensor_gps[] = "location";
char OneNetServer[] = "183.230.40.33";
char lon_str[] = "11224.4992";
char lat_str[] = "3438.1633";
void setup(){
Serial.begin(115200);
}
void loop(){
sendMengToOnenet(API_KEY,device_id,sensor_gps,lon_str,lat_str);
delay(600);
}
double longitudeToOnenetFormat(char *lon_str_temp)
{
double lon_temp = 0;
long lon_Onenet = 0;
int dd_int = 0;
long mm_int = 0;
double lon_Onenet_double = 0;
lon_temp = atof(lon_str_temp);
lon_Onenet =lon_temp*100000;//转换为整数
dd_int = lon_Onenet/10000000; //取出dd
mm_int = lon_Onenet%10000000;//取出MM部分
lon_Onenet_double = dd_int + (double)mm_int/60/100000;//换算为Onenet格式
return lon_Onenet_double;
}
double latitudeToOnenetFormat(char *lat_str_temp)
{
double lat_temp = 0;
long lat_Onenet = 0;
int dd_int = 0;
long mm_int = 0;
double lat_Onenet_double = 0;
lat_temp = atof(lat_str_temp);
lat_Onenet =lat_temp*100000;//转换为整数
dd_int = lat_Onenet/10000000; //取出dd
mm_int = lat_Onenet%10000000;//取出MM部分
lat_Onenet_double = dd_int + (double)mm_int/60/100000;//换算为Onenet格式
return lat_Onenet_double;
}
void sendMengToOnenet(char* API_VALUE_temp,char* device_id_temp,char* sensor_id_temp,char* lon_temp,char* lat_temp){
char send_buf= {0};
char text = {0};
char tmp = {0};
char lon_str_end = {0};
char lat_str_end = {0};
dtostrf(longitudeToOnenetFormat(lon_temp),3,6, lon_str_end);//转换成字符串输出
dtostrf(latitudeToOnenetFormat(lat_temp),2,6, lat_str_end);
if(sendCommand("AT\r\n","OK",20,10)==Success){
delay(5000);
Serial.write("AT\r\n");
delay(5000);
Serial.write("AT+CGCLASS=\"B\"\r\n");
delay(5000);
Serial.write("AT+CGDCONT=1,\"IP\",\"CMNET\"\r\n");
delay(5000);
Serial.println("AT+CGATT=1");
delay(5000);
Serial.write("AT+CIPCSGP=1,\"CMNET\"\r\n");
delay(5000);
Serial.write("AT+CLPORT=\"TCP\",\"2000\"\r\n");
delay(5000);
Serial.write("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n");
delay(5000);
Serial.write("AT+CIPSEND=1\r\n");
delay(5000);
sprintf(text,"{\"datastreams\":[{\"id\":\"%s\",\"datapoints\":[{\"value\":{\"lon\":%s,\"lat\":%s}}]}]}",sensor_id_temp,lon_str_end,lat_str_end);
send_buf = 0;
strcat(send_buf,"POST /devices/");
strcat(send_buf,device_id_temp);
strcat(send_buf,"/datapoints HTTP/1.1\r\n");//注意后面必须加上\r\n
strcat(send_buf,"api-key:");
strcat(send_buf,API_VALUE_temp);
strcat(send_buf,"\r\n");
strcat(send_buf,"Host:");
strcat(send_buf,OneNetServer);
strcat(send_buf,"\r\n");
sprintf(tmp,"Content-Length:%d\r\n\r\n", strlen(text));//计算JSON串长度
strcat(send_buf,tmp);
strcat(send_buf,text);
Serial.write(send_buf);
delay(5000);
}
}
unsigned char sendCommand(char* message,char* response,int timeout,int begintime){
Serial.write(message);
for(int i=timeout;i>begintime;i--){
if(Serial.find("OK"))
return Success;
else
return Failure;
}
}
页:
[1]