Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15287

ESP8266 Async HTTP Request

$
0
0

Hey everyone!

I'm trying to implement a weather display into a 7 segment mechanical clock that I've made, I've been able to implement the API request using the regular WiFiClient built into the ESP8266. and the code DOES work, however when it runs it blocks all other code and the entire thing freezes, but once it receives the information then it continues working as intended.

I learnt about using Async however I'm kind of a noob when it comes to this and I cannot understand how to implement something like that, the library I was looking at is ESPAsyncWebServer.h

This is the current code I have, and it does work fine

#include <ESP8266WiFi.h>

const char* ssid = "SSID";
const char* password = "PASS";
const int httpPort = 443;

const char* host = "aviationweather.gov";

void setup() {
  Serial.begin(115200);

  // Connect to WiFi network
  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");

  // Print the IP address
  Serial.println(WiFi.localIP());

  Serial.println("type enter in the Serial monitor to trigger a request");

}

void loop() {
  if (Serial.read() == '\n') { // type enter in the Serial monitor to trigger a request
    WiFiClientSecure client;
    client.setInsecure();

    if (!client.connect(host, httpPort)) {
      Serial.print("connection to "); Serial.print(host); Serial.println(" failed.");
      return;
    }

    boolean readingAirport = false;
    boolean readingCondition = false;
    boolean readingWDir = false;
    boolean readingWSpeed = false;
    boolean readingWGust = false;
    boolean readingVis = false;
    boolean readingWX = false;
    boolean readingTemp = false;
    boolean readingDewp = false;
    boolean readingAlt = false;
    char c;
    String currentLine = "";
    String currentAirport = "";
    String currentCondition = "";
    String currentWDirString = "";
    String currentWSpeedString = "";
    String currentWGustString = "";
    String currentVisString = "";
    String currentTempString = "";
    String currentDewpString = "";
    String currentAltString = "";
    String currentWX = "";
    String url = "/cgi-bin/data/metar.php?ids=CYYT&format=xml&taf=false";
    Serial.print("Requesting URL: ");  Serial.print("http://"); Serial.print(host); Serial.println(url);

    client.print("GET ");
    client.print(url);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(host);
    client.println("User-Agent: LED Sectional Client");
    client.println("Connection: close\r\n");
    client.println();
    client.flush();

    while (!client.available()) yield(); // active wait for the answer (bad)

    Serial.println("Response:\n----------------------------");
    while (client.available())
    {
      if((c = client.read())>=0);
      {
        yield();
        currentLine += c;
        if(currentLine.endsWith("<station_id>"))
        {
          currentAirport = ""; // Reset everything when the airport changes
          readingAirport = true;
          currentLine = "";
          currentCondition = "";
          currentWDirString = "";
          currentWSpeedString = "";
          currentWGustString = "";
          currentVisString = "";
          currentTempString = "";
          currentDewpString = "";
          currentAltString = "";
          currentWX = "";
        } else if (readingAirport) {
          if(!currentLine.endsWith("<")){
            currentAirport += c;
          } else {
            readingAirport = false;
          }
        } else if (currentLine.endsWith("<temp_c>")){
          readingTemp = true;
        } else if (readingTemp){
          if(!currentLine.endsWith("<")){
            currentTempString += c;
          } else {
            readingTemp = false;
          }
        } else if (currentLine.endsWith("<dewpoint_c>")){
          readingDewp = true;
        } else if (readingDewp){
          if(!currentLine.endsWith("<")){
            currentDewpString += c;
          } else {
            readingDewp = false;
          }
        } else if (currentLine.endsWith("<wind_dir_degrees>")){
          readingWDir = true;
        } else if (readingWDir){
          if(!currentLine.endsWith("<")){
            currentWDirString += c;
          } else {
            readingWDir = false;
          }
        } else if (currentLine.endsWith("<wind_speed_kt>")){
          readingWSpeed = true;
        } else if (readingWSpeed){
          if(!currentLine.endsWith("<")){
            currentWSpeedString += c;
          } else {
            readingWSpeed = false;
          }
        } else if (currentLine.endsWith("<wind_gust_kt>")){
          readingWGust = true;
        } else if (readingWGust){
          if(!currentLine.endsWith("<")){
            currentWGustString += c;
          } else {
            readingWGust = false;
          }
        } else if (currentLine.endsWith("<visibility_statute_mi>")){
          readingVis = true;
        } else if (readingVis){
          if(!currentLine.endsWith("<")){
            currentVisString += c;
          } else {
            readingVis = false;
          }
        } else if (currentLine.endsWith("<altim_in_hg>")){
          readingAlt = true;
        } else if (readingAlt){
          if(!currentLine.endsWith("<")){
            currentAltString += c;
          } else {
            readingAlt = false;
          }
        } else if (currentLine.endsWith("<flight_category>")){
          readingCondition = true;
        } else if (readingCondition){
          if(!currentLine.endsWith("<")){
            currentCondition += c;
          } else {
            readingCondition = false;
          }
        } else if (currentLine.endsWith("<wx_string>")) {
          readingWX = true;
        } else if (readingWX) {
          if (!currentLine.endsWith("<")) {
            currentWX += c;
          } else {
            readingWX = false;
          }
        }
      } 
    }; // dump the answer to the Serial monitor
    int currentWDir = currentWDirString.toInt();
    int currentWSpeed = currentWSpeedString.toInt();
    int currentWGust = currentWGustString.toInt();
    int currentVis = currentVisString.toInt();
    int currentTemp = currentTempString.toInt();
    int currentDewp = currentDewpString.toInt();
    float currentAlt = currentAltString.toFloat();

    Serial.print("Airport: ");
    Serial.println(currentAirport);
    Serial.print("Wind Direction: ");
    Serial.println(currentWDir);
    Serial.print("Wind Speed: ");
    Serial.println(currentWSpeed);
    Serial.print("Wind Gust: ");
    Serial.println(currentWGust);
    Serial.print("Visibility: ");
    Serial.println(currentVis);
    Serial.print("Temp: ");
    Serial.println(currentTemp);
    Serial.print("Dewpoint: ");
    Serial.println(currentDewp);
    Serial.print("Altimeter: ");
    Serial.println(currentAlt);
    Serial.print("Condition: ");
    Serial.println(currentCondition);
    Serial.print("WX: ");
    Serial.println(currentWX);

    Serial.println("\n----------------------------");
    Serial.println("closing connection");
    client.stop();
    Serial.println("type enter in the Serial monitor to trigger a request");
  }
}

I'm probably way in over my head with this but any help would be appreciated, all I'm trying to do is get the request from "aviationweather.gov/cgi-bin/data/metar.php?ids=CYYT&format=xml&taf=false" but have it bring it in a way that isn't blocking

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15287

Trending Articles