Textdatei von Webseite oder FTP auslesen

Hallo Leute,

ich würde gerne von einer Webseite die Datei (txt) in eine Variable auslesen.
Das ist die Webseite:

https://tgftp.nws.noaa.gov/data/observations/metar/stations/EDDM.TXT

diese gibt es auch als FTP mit freiem Zugang.

Nun noch ein wenig "feintunning". Ich benötige "nur" die zweite Zeile.
Ich bekomme es einfach nich hin.

Kann mir jemand helfen?

Danke im voraus :slight_smile:

Lesen bis zum ersten Zeilenvorschub, Inhalt wegwerfen, dann lesen bis zum nächsten Zeilenvorschub, diesen durch '\0' ersetzen - Verarbeiten.
Also genau so, als würdest Du von der seriellen Schnittstelle einlesen. Dazu gibt es hier im Forum viele Beispiele.

Wenn nur noch Feintuning zu tun ist, dann zeige doch mal, was Du schon hast. Da kann man darauf aufbauen.

Gruß Tommy

Hallo Tommy56,

nein leider ist es nicht nur das "Finetunning" es ist alles eigendlich.
Ich habe "Nun noch ein wenig Finetunning" geschrieben.

Kannst Du mir sagen wie ich das am besten mache.

Gruß
Thomas

Controller?
Mit was arbeitest Du? So kann Dir niemand helfen.
PS: In der ersten Zeile steht bei mir Datum / Uhrzeit...

Hallo,
um welchen Controller geht es denn. Arduino, ESP
Hört sich irgendwie nach Schulprojekt "Hausaufgaben" an :wink:
Heinz

Heißt für mich das Meiste ist fertig. Also Vorspiegelung falscher Tatsachen von Dir.

Gruß Tommy

Das scheint mir eine Flugwettermeldung zu sein.
Vielleicht hilft dieses Beispiel mit dem GET weiter.

Für München.

Gruß Tommy

Sorry wenn das so rüber gekommen ist. Werde versuchen mich besser auszudrücken :wink:

Gruß Thomas

Nein aus diesem alter bin ich schon lange raus :slight_smile:

Es ist ein ESP8266 mit einer 32x8 LED Matrix zum anzeigen.

Danke für den Tipp.
Schau ich mir gerne mal an.

Ich hab's mal mit einem ESP32 ausprobiert.
Ergebnis:

21:15:17.387 -> HTTP Response code: 200
21:15:17.387 -> 2022/01/18 19:50
21:15:17.387 -> EDDM 181950Z 06003KT 9999 FEW026 M01/M03 Q1035 NOSIG
21:15:17.387 -> 

Sketch:

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "SSID";
const char* password = "PASSWORD";

//Your Domain name with URL path or IP address with path
String serverName = "https://tgftp.nws.noaa.gov/data/observations/metar/stations/";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 10 seconds
unsigned long timerDelay = 10000;

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + "EDDM.TXT";
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

Das Auseinanderpflücken des Strings payload bekommst Du selbst hin.

Super danke.
Schau ich mir an.
Denke das bekomme ich hin.

Schönen Abend

Hallo zusammen,
ich leider nochmal. :frowning:

Der beisppielcode funktioniert bei mir nicht. Ich habe ein ESP8266 und da
will " <HTTPClient.h>" nicht kompiliert werden. Jetzt habe ich es mit der "ESP8266HTTPClient.h" versucht.

Leider bekomme ich beim "httpResponseCode" eine "-1".
Mit dem WLAN bin ich verbunden und bekomme auch eine IP.
Hier mein Funktion:

void getMetar(){

      HTTPClient http;

      // Your Domain name with URL path or IP address with path
      http.begin(serverPath);
      Serial.print(serverPath);
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
}

Würde mich über eine weitere Idee von euch freuen.

Auch für den ESP8266 gibt es bei randomnerdtutorials ein Beispiel.
Das läuft aber so noch nicht, weil die Abfrage ein https ist.

Ersetze

#include <WiFiClient.h>

durch

#include <WiFiClientSecure.h>

sowie

WiFiClient client;
HTTPClient http;

durch

      WiFiClientSecure client;
      HTTPClient http;
      client.setInsecure();

EDIT #2: Bitte den Hinweis von Tommy56 beachten - das ist unsicher!
Läuft - getestet mit einem ESP8266-01.

18:46:18.816 -> HTTP Response code: 200
18:46:18.816 -> 2022/01/19 17:20
18:46:18.816 -> EDDM 191720Z 33004KT 9999 BKN030 01/M01 Q1023 NOSIG

Edit: serverName und -path natürlich anpassen wie vorher auch.
Da mir HAJ näher liegt: Hier das Wetter bei EDDV

18:50:08.873 -> 2022/01/19 17:20
18:50:08.873 -> EDDV 191720Z 25013KT 220V280 6000 -DZ BKN008 03/02 Q1016 BECMG 4500

Wenn das Flugwetter zu realen Flügen beitragen sollte, dürfte dieser Unsicherheitsfaktor nicht zulässig sein.
Da sollte man die Identität des Servers checken.

Gruß Tommy

@Tommy56
Das stimmt natürlich, ich bin da für reale Fliegerei bei Dir (ob da ein ESP8266 mitfliegen sollte, ist nochmal eine andere Frage).
Danke für den expliziten Hinweis!

Über den realen Einsatz schweigt sich der TO ja aus.

Gruß Tommy

Es geht hier nicht um die reale Fliegerei. Es geht hier um den MSFS2020. :slight_smile:

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  
  Code compatible with ESP8266 Boards Version 3.0.0 or above 
  (see in Tools > Boards > Boards Manager > ESP8266)
*/

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>


const char* ssid     = "xxxx";     // SSID of local network
const char* password = "xxxx";   // Password on network

//Your Domain name with URL path or IP address with path
String serverName = "https://tgftp.nws.noaa.gov/data/observations/metar/stations/";
String serverPath = serverName + "EDDM.TXT";


// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

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

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  // Send an HTTP POST request depending on timerDelay
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      WiFiClientSecure client;
      HTTPClient http;
      client.setInsecure();

      
      
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

Und das ist die Fehlermeldung die ich bekomme.:

Arduino: 1.8.16 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200"

sketch_jan19i:56:14: error: 'class WiFiClientSecure' has no member named 'setInsecure'

   client.setInsecure();

sketch_jan19i:61:44: error: no matching function for call to 'HTTPClient::begin(WiFiClientSecure&, const char*)'

   http.begin(client, serverPath.c_str());

Hmmm.
Mal ein anderes Board (Generic ESP8266 Module) ausprobiert?
Welche Boardversion hast Du installiert?