Arduino Uno + ESP8266读取服务器的响应 [英] Arduino Uno + ESP8266 reading server's response

查看:464
本文介绍了Arduino Uno + ESP8266读取服务器的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ESP8266从Arduino Uno发送GET请求.请求已发送,但我无法打印收到的响应.

I'm sending a GET request from Arduino Uno using ESP8266. The request is sent, but I'm unable to print the received response.

我使用的代码来自我已经更改了连接到服务器的代码,并且可以在服务器的日志中看到GET请求.

I have changed the code for connecting to my server and I can see the GET request is received on my server's log.

我尝试放

 while (ser.available())
 {
     Serial.write(ser.read());
 }

Serial.println("AT+CIPCLOSE");语句之后.

但是"AT+CIPCLOSE"

这是我的完整代码:

Here's my entire code:

// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // TX, RX

// this runs once
void setup()
{

    // enable debug serial
    Serial.begin(9600);
    // enable software serial
    ser.begin(9600);

    // reset ESP8266
    ser.println("AT+RST");
}
// the loop
void loop()
{

    // TCP connection
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += "192.168.0.25"; 
    cmd += "\",3000";
    ser.println(cmd);

    if(ser.find("Error"))
    {
        Serial.println("AT+CIPSTART error");
        return;
    }

    // prepare GET string
    String getStr = "GET /api/myservice";

    getStr += "\r\n\r\n";

    // send data length
    cmd = "AT+CIPSEND=";
    cmd += String(getStr.length());
    ser.println(cmd);

    if(ser.find(">")){
    ser.print(getStr);
}
else
{ 
    ser.println("AT+CIPCLOSE");
    // alert user
    Serial.println("AT+CIPCLOSE");

    // CODE I FOUND FOR READING THE REPLY FROM SERVER:
    while (ser.available())
    {
         // char c = ser.read();
         Serial.write(ser.read());
         // if (c == '\r') Serial.print('\n');
    }
}

delay(1000);
}

ESP详细信息:

ESP-01

AT版本:0.40.0.0

AT version: 0.40.0.0

推荐答案

如果您只是在努力阅读答案,那么答案很简单;

If you are only struggling to read a response then the answer is simple;

在尝试阅读之前,您正在关闭TCP连接:

You are closing the TCP connection before you try to read:

ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");

// CODE I FOUND FOR READING THE REPLY FROM SERVER:
while (ser.available())
{

将读数移到ser.print(getStr);下面的上方块中,但同时在两者之间添加延迟.

Move the reading while into the above block just beneath ser.print(getStr); but add a delay between the two as well.

这篇关于Arduino Uno + ESP8266读取服务器的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆