esp8266 通过edp上传数据到onenet

通讯代码如下,

EdpPacket.h已上传,下载地址如下:

https://download.csdn.net/download/chenchen2360060/12666490

#include 

#include "EdpPacket.h"

#define BAUDRATE 115200
#define DEV_KEY "设备key"
#define DEV_ID  "设备ID"
int led_13=13;
EdpPacketClass edp;  //创建EDP对象


const char* ssid     = "xxx";//要连接的网的名称
const char* password = "xxx";//密码
 const char* serverIP  = "jjfaedp.hedevice.com";
// const char* serverIP  = "192.168.2.165";
int serverPort = 876;
WiFiClient client;
bool bConnected = false;
char buff[512];
int nm = 0;
          float t ;
void setup() {

  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int edp_connect = 0;
void loop() {
t= random(1,50);
  if(WiFi.status() != WL_CONNECTED)//说明没有连接上wifi
  {

    //连接到wifi
      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }

        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        Serial.println(WiFi.localIP());

        delay(1000);

  }


//没有连接到服务器,进行连接
  if (!client.connect(serverIP, serverPort))
  {
    Serial.println("connection failed");
    delay(5000);

    bConnected = true;
    Serial.println("connection ok");

    return;
  }

  delay(1);
  if (bConnected == false)//没有连接到服务器,进行连接
  {
    if (!client.connect(serverIP, serverPort))
    {
      Serial.println("connection failed");
      delay(5000);
      return;
    }
    bConnected = true;
    Serial.println("connection ok");
  }
  else
  {
    if (!edp_connect)//如果没有连接就连接
    {
            //发送
        edp.PacketConnect1(DEV_ID,DEV_KEY); //创建EPD连接请求包

        client.write(edp.GetData(),edp.GetWritepos());//发送EDP连接请求包

        edp_connect = 1;
         delay(1000);
    }
    if (edp_connect)//要是连接上了就发数据
    {
        
// 浮点数转字符串:格式如下: 
// char* dtostrf(double _val,signed char _width, unsigned char prec, char* _s) 
// 参数说明: 
// _val:要转换的float或者double值。

// _width:转换后整数部分长度。

// _prec:转换后小数部分长度。

// _s:保存到该char数组中。

        Serial.println("yes");

       char tempstring[4];
        dtostrf(t,2,1,tempstring); 

        //char s12[] ={',',';','t','e','m','p',',','0','0','.','0','0'};
        char s11[]=",;temp,0000";

        s11[7]=tempstring[0];
        s11[8]=tempstring[1];
        s11[9]=tempstring[2];
        s11[10]=tempstring[3];
        Serial.println();
        Serial.println(s11);

        //edp.PacketSavedataSimpleString(DEV_ID,",;temp,100"); //创建EPD数据上传数据的数据包,数据“,;temp,100”为分割字符串的格式,具体说明见<设备终端接入协议EDP.docx>
        edp.PacketSavedataSimpleString(DEV_ID,s11); 
        client.write(edp.GetData(),edp.GetWritepos()); //发送EDP数据包
        edp_connect = 0;
        delay(20000);
    }
  }
}

 

你可能感兴趣的:(esp32,esp8266)