【问题标题】:Arduino gets response from server problemArduino 从服务器问题得到响应
【发布时间】:2019-08-28 10:39:55
【问题描述】:

我正在使用通过 SoftwareSerial 连接到 Arduino Uno 的 ESP8266 向 API 发出发布请求。 ESP8266 能够连接到 WIFI 连接。当我尝试将一些数据(由模块 DHT22 捕获的温度值)发送到服务器时,我总是被拒绝访问。它总是在执行命令 "esp.println(tcpstart)" 并且我在串行监视器中收到响应 "--> 无法启动 TCP 连接" 时发生,这要归功于我添加的版画。 "tcpstart" 是 AT 命令"AT+CIPSTART"。 Arduino Uno 刷入固件:ai-thinker-0.9.5.2-115200.bin。 这是我的代码:

#include <SoftwareSerial.h>
#include <DHT.h>;
#define RX_PIN 2
#define TX_PIN 3
#define DHTTYPE DHT22
#define DHTPIN  7

SoftwareSerial esp(RX_PIN, TX_PIN); // Rx,  Tx
DHT dht(DHTPIN, DHTTYPE);
float temp;
String ssid = "XXXXX";
String pass = "YYYYY";
//Complete API URL to do the post: https://api.test.com:bbbb/zzz/zzzz/v2/z?zzzzzzz
String server = "https://api.test.com";
String ip = "aa.aaa.aa.aa";
int port = bbbb;
String uri = "/zzz/zzzz/v2/z?zzzzzzz";
bool connection;
String data;
String* response;

void setup(){
  Serial.begin(9600);
  esp.begin(115200);
  esp.setTimeout(5000);
  dht.begin();
}

void loop(){
  reset();
  readSensors();
  while(connection == false)
    connectWifi();
  connection = false;
  httppost();
}

void readSensors(){
  temp = dht.readTemperature();
  data = String(temp);
}

void reset(){
  esp.println("AT+RST");
  delay(300);
  esp.println("AT+CWMODE=1");
  delay(300);
  esp.println("AT+RST");
  delay(300);
  esp.println("AT+CIPMUX=0");
  delay(300);
}

void connectWifi(){
  String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"";
  delay(300);
  esp.println(cmd);
  if(esp.find("OK")){
    Serial.println("Wifi Connected");
    connection = true;
  }else{
    Serial.println("--> Wifi not Connected");
    connection = false;
  }
}

void httppost(){
  String postRequest =
        "POST " + uri + " HTTP/1.1\r\n" +
        "Host: " + server + ":" + port + "\r\n" +
        "Accept: *" "/" "*\r\n" +
        "Content-Length: " + data.length() + "\r\n" +
        "Content-Type: application/x-www-form-urlencoded\r\n" +
        "\r\n" +
        data;
  String tcpStart = "AT+CIPSTART=\"TCP\",\"" + ip + "\"," + port;
  String sendCmd = "AT+CIPSEND=" + postRequest.length();
  esp.println(tcpStart); //start TCP connection
  delay(300);
  if(esp.find("OK")){
    Serial.println("TCP connection OK");
    esp.println(sendCmd); //send the data over TCP connection
    delay(300);
    if(esp.find(">")){
      Serial.println("Sending packet");
      esp.println(postRequest);
      delay(300);
      if(esp.find("SEND OK")){
        Serial.println("Packet sent");
        while(esp.available()){ //number of bytes/char available for reading
          char tmpResp = esp.read(); //read one char at the time
          Serial.print(tmpResp);
          if (tmpResp == '\0') continue; //terminate the while when end of the data
        }
        esp.println("AT+CIPCLOSE"); //close TCP connection
      }else{
        Serial.println("--> An error occured while sending packet");
      }
    }else{
        Serial.println("--> ESP8266 is not listening for incoming data");
    }
  }else{
    Serial.println("--> Cannot initiate TCP connection");
  }
}

你知道可能是什么问题吗?

【问题讨论】:

    标签: c rest tcp arduino esp8266


    【解决方案1】:

    试试这个代码。

    #include <SoftwareSerial.h>
    
    const byte rxPin = 2;
    const byte txPin = 3;
    
    SoftwareSerial ESP8266 (rxPin, txPin);
    
    unsigned long lastTimeMillis = 0;
    
    void setup() {
      Serial.begin(9600);   
      ESP8266.begin(9600);
      delay(2000);
    }
    
    void printResponse() {
      while (ESP8266.available()) {
        Serial.println(ESP8266.readStringUntil('\n')); 
      }
    }
    
    void loop() {
    
      if (millis() - lastTimeMillis > 30000) {
        lastTimeMillis = millis();
    
        ESP8266.println("AT+CIPMUX=1");
        delay(1000);
        printResponse();
    
        ESP8266.println("AT+CIPSTART=4,\"TCP\",\"192.168.1.19\",80");
        delay(1000);
        printResponse();
    
        String cmd = "GET /test.html HTTP/1.1";
        ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
        delay(1000);
    
        ESP8266.println(cmd);
        delay(1000);
        ESP8266.println(""); 
      }
    
      if (ESP8266.available()) {
        Serial.write(ESP8266.read());
      }
    
    }
    

    【讨论】:

    • 谢谢你,伙计!你能解释一下它是做什么的吗?我想将其更改为 POST。
    • 我认为最大的问题是我的 POST 网址是如何构建的
    猜你喜欢
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 2020-11-06
    • 2018-01-21
    • 2021-07-30
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多