HTTP GET-запрос с Arduino и ESP8266 - PullRequest
0 голосов
/ 07 марта 2020

Я пытаюсь получить данные с веб-сайта, отправив HTTP-запрос GET vai AT-командам. Я нашел сообщение за несколько лет go с кодом, который якобы делает то, что мне нужно.

Вот как выглядит код:

#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial ESP8266 (rxPin, txPin);

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600);   
  ESP8266.begin(9600);
  delay(2000);
  ESP8266.println("AT+RST");
}

void loop() 
{
  // put your main code here, to run repeatedly:
  ESP8266.println("AT+CIPMUX=1");
  delay(1000);
  printResponse();

  ESP8266.println("AT+CIPSTART=4,\"TCP","ajayvhd.pythonanywhere.com\",80");
  delay(1000);
  printResponse();

  String cmd = "GET /testcon 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());
  }


}

По большей части этот код работает, ESP подключается к ссылке и отправляет запрос и получает ответ. Тем не менее, ответ не тот, который ожидается. Я получаю это:

Recv 25 bytes

SEND OK

+IPD,4,1320:HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 2921
ETag: "5e5fb258-b69"
Server: PythonAnywhere
Age: 21
Date: Fri, 06 Mar 2020 22:16:43 GMT
Connection: keep-alive

<html>
    <head>
        <title>Coming Soon: PythonAnywhere</title>
        <style>
            body {
                font-family: Helvetica, Arial, sans-serif;
                width: 500px;
                margin-left: auto;
                margin-right: auto;
                margin-top: 20px;
            }

            h1 {
                font-family: Trebuchet MS, Helvetica, Arial, sans-serif;
            }

            .for-site-owner {
                font-size: smaller;
                margin-top: 30px;
                color: gray;
            }
        </style>
    </head>

    <body>
        <img src="https://s3.amazonaws.com/pythonanywhere-error-images/logo-234x35.png" />

        <div class="main">
            <h1>Coming Soon!</h1>

            <p>
                This is going to be another great website hosted by
                <a href="https://www.pythonanywhere.com/">PythonAnywhere</a>.
            </p>

            <p>
                PythonAnywhere lets you host, run, and code Python in the cloud.
                Our free plan gives you access to machines with
                everything already
+IPD,4,1320: set up for you. You can develop and host
                your website or any other code directly from your browser
                without having to install software or manage your own server.
            </p>

            <p>
                Need more power? Upgraded plans start at $5/month.
            </p>

            <p>
                <a href="https://www.pythonanywhere.com/">You can find out more about PythonAnywhere here.</a>
            </p>
        </div>

        <div class="for-site-owner">
            <h2>Developer info</h2>

            <p>
                Hi!  If this is your PythonAnywhere-hosted site, then you're
                almost there &mdash; you just need to create
                a web app to handle this domain.
            </p>

            <p>
                Go to the "Web" tab inside
                PythonAnywhere and click "Add a new web app".
                If you already have a web app and you want to use the same code for this domain
                (say because you've just upgraded and want the site
                you built at <code>yourusername.pythonanywhere.com</code> to run on
                <code>www.yourdomain.com</code>) then
                <a href="https://help.pythonanywhere.com/pages/UsingANewDomainForExistingWebApp">this help page should explain

+IPD,4,470:              everything</a>.
            </p>

            <p>
                If you're having problems getting it all working, drop us a line at
                support@pythonanywhere.com, or in
                <a href="https://www.pythonanywhere.com/forums/">the forums</a>,
                or using the "Send
                feedback" link on the site.  We'll
                get back to you as fast as we can!
            </p>

        </div>

    </body>
</html>

Теперь это ответ для веб-сайта, который еще не существует, за исключением того, что веб-сайт существует, и ответ должен быть примерно таким:

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
    Connection Failed, Trying Again
    </body>
</html>

Запрос, похоже, отправляется, однако он отправляется не по адресу, я пытался отладить код, но не могу понять, в чем причина. Мне было интересно, если кто-то еще может попытаться определить, где я иду не так, и предложить улучшение.

Спасибо.

...