技术碎片
技术记录
帮助交流
hot
Arduino ESP8266 串口通信POST+GET示例

#include "DHT.h"

#include <stdlib.h>

#include <Arduino_JSON.h>

#include <SoftwareSerial.h>

#define DHTPIN 7

#define DHTTYPE DHT11   // DHT 11


DHT dht(DHTPIN, DHTTYPE);

#define SSID "******"      //wifi名

#define PASS "********" //wifi密码

#define IP "api.heclouds.com" // 服务器

//

const char OneNetServer[] = "api.heclouds.com";

const char APIKEY[] = "*******************************";    // 使用时请修改为你的API KEY -- Please use your own API KEY

int32_t DeviceId = *************;                             // 使用时请修改为你的设备ID -- Please use your own device ID

const char DS_Temp[] = "TEMP";                        // 数据流 温度TEMP -- Stream "TEMP"

const char DS_Hum[] = "HUMI";                          // 数据流 湿度HUMI -- Stream "HUMI"

const char DS_CK1[] = "JDQ1";

const char DS_CK2[] = "JDQ2";

const int jdq1 = 4;

const int jdq2 = 3;

SoftwareSerial monitor(13, 12); // 定义软串口RX, TX ESP RX接12,TX接13

String _comdata = "";

const int tcpPort = 80;

String ck1 = "0";

String ck2 = "0";

String comdata;

//初始化-----------------------------------------

void setup()

{

  monitor.begin(115200);

  Serial.begin(115200);

  dht.begin();

  pinMode(jdq1,OUTPUT);

  pinMode(jdq2,OUTPUT);

  monitor.println("AT");//指令测试

  delay(1000);

  if (monitor.find("OK"))  //接收指令正常则返回OK

  {

    Serial.println("Wifi module connection is normal");

    connectWiFi();

  }

  else {

    Serial.println("Wifi module connection failed");

  }

}


//主循环-----------------------------------------

void loop()

{


  //Serial.println((float)DHT11.temperature);

  //Serial.println((float)DHT11.humidity);

  float tempH = dht.readHumidity();

  float tempT = dht.readTemperature();

  Serial.print("温度:");

  Serial.print(tempT);

  Serial.print("湿度:");

  Serial.print(tempH);

  Serial.print("\n");


  char buffer[10];

  String temph = dtostrf(tempH, 4, 1, buffer);

  String tempt = dtostrf(tempT, 4, 1, buffer);


  if(temph.toInt() > 60){//湿度大于50%打开继电器

    ck2 = "1";

    digitalWrite(jdq2,LOW);

  }else{  

    ck2 = "0";

    digitalWrite(jdq2,HIGH);

  }

  if( tempt.toInt() > 30){ //温度大于30度打开继电器

    ck1 = "1";

    digitalWrite(jdq1,HIGH);

  }else{

    ck1 = "0";

    digitalWrite(jdq1,LOW);

  }

  updateTemp(temph,tempt,ck1,ck2);

  delay(1000);

  getMonitorData();

  delay(1000);

  getData();

  delay(1000);

  getMonitorData();

  delay(2000);

}



void updateTemp(String temph, String tempt, String ck1, String ck2)

{

  String cmd = "AT+CIPSTART=\"TCP\",\"";

  cmd += IP;

  cmd += "\",80";

  //sendDebug(cmd);                        //发送指令,链接服务器

  monitor.println(cmd);

  delay(2000);

  if (monitor.find("Error"))

  {

    Serial.print("Connection to server failed");

    return;

  }

  //cmd = GET + "&field1=" + temph + "&field2=" + tempt + "\r\n";       //记录T和H的值

  cmd = postData(DeviceId, tempt, temph, ck1, ck2);

  monitor.print("AT+CIPSEND=");

  monitor.println(cmd.length());

  if (monitor.find(">"))

  {

    Serial.print(">");

    monitor.print(cmd);

    Serial.print(cmd);

  }

  else

  {

    Serial.println("Data transmission failure");

  }

  if (monitor.find("OK"))

  {

    Serial.println("RECEIVED: OK");

    monitor.println("AT+CIPCLOSE");

  }

  else

  {

    Serial.println("Data transmission failure");

  }

}


boolean connectWiFi()

{

  //Serial.println("AT+CIPMUX=0");

  monitor.println("AT+CWMODE=1");

  monitor.println("AT+RST");

  delay(2000);

  String cmd = "AT+CWJAP=\"";

  cmd += SSID;

  cmd += "\",\"";

  cmd += PASS;

  cmd += "\"";

  monitor.println(cmd);

  delay(1000);

  if (monitor.find("OK"))

  {

    Serial.println("Wifi connection successful");

    return true;

  } else

  {

    Serial.println("Wifi connection failed");

    return false;

  }

}


String postData(int dId, String val_t, String val_h, String ck1, String ck2) 

{

    // 创建发送请求的URL -- We now create a URI for the request

    String url = "/devices/*****************";//此处修改为你的设备id

    //url += String(dId);

    url += "/datapoints?type=3";           

    String data = "{\"" + String(DS_Temp) + "\":" + val_t + ",\"" + String(DS_Hum) + "\":" + val_h + ",\"" + String(DS_CK1) + "\":" + ck1 + ",\"" + String(DS_CK2) + "\":" + ck2 + "}";

    // 创建发送指令 -- We now combine the request to the server

    String post_data = "POST " + url + " HTTP/1.1\r\n" +

                       "api-key:" + APIKEY + "\r\n" +

                       "Host:" + OneNetServer + "\r\n" +

                       "Content-Length: " + String(data.length()) + "\r\n" +                     //发送数据长度

                       "Connection: close\r\n\r\n" +

                       data;

  

    // 发送指令 -- This will send the request to server

    return post_data;

}


void getData()

{

  String cmd1 = "AT+CIPSTART=\"TCP\",\"";

  cmd1 += "*************";

  cmd1 += "\",80";

  monitor.println(cmd1);

  delay(2000);

  if (monitor.find("Error"))

  {

    Serial.print("Connection to server failed");

    return;

  }

  cmd1 = "GET http://*************/**********\r\n\r\n";

  monitor.print("AT+CIPSEND=");

  monitor.println(cmd1.length());

  if (monitor.find(">"))

  {

    Serial.print(">");

    monitor.print(cmd1);

    Serial.print(cmd1);

  }

  else

  {

    Serial.println("Data transmission failure");

  }

  if (monitor.find("OK"))

  {

    Serial.println("RECEIVED: OK");

    monitor.println("AT+CIPCLOSE");

  }

  else

  {

    Serial.println("Data transmission failure");

  }

}

void getMonitorData(){

   while(monitor.available() > 0){

    _comdata += char(monitor.read());

    delay(2);

   }

   if(_comdata != ""){

    Serial.println(_comdata);

    _comdata = String("");

  }

}

转载请注明来源地址:www.zhyunxuan.com 更新时间 2021-11-01  2042
梦想有多远,路就有多远