logo elektroda
logo elektroda
X
logo elektroda

How to read the weather from OpenWeatherMap on an ESP32-S2 Arduino?

klon111 420 4
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 21146489
    klon111
    Level 11  
    Please help me with the weather reading from the server on the browser reads ok
    void odczytSerPog() {  
      WiFiClient client;
        HTTPClient httpPP; 
    String serverName ="http://api.openweathermap.org/data/2.5/weather?lat=52.01...";
    httpPP.begin(client, serverName ); 
          welcome +=  httpPP.getString();
       
         WebSerial.println(welcome);
               httpPP.end(); 
     }
    .
    Browser readout
    {
    "coord": {
    "lon": 23.1,
    "lat": 52.01
    },
    "weather": [
    {
    "id": 802,
    "main": "Clouds",
    "description": "clouds small",
    "icon": "03d"
    }
    ],
    "base": "stations",
    "main": {
    "temp": 20.24,
    "feels_like": 20.64,
    "temp_min": 20.24,
    "temp_max": 20.24,
    "pressure": 1014,
    "humidity": 89,
    "sea_level": 1014,
    "grnd_level": 997
    },
    "visibility": 4885,
    "wind": {
    "speed": 3.85,
    "deg": 333,
    "gust": 7.98
    },
    "clouds": {
    "all": 47
    },
    "dt": 1720375922,
    "sys": {
    "country": "EN",
    "sunrise": 1720318618,
    "sunset": 1720378066
    },
    "timezone": 7200,
    "id": 776175,
    "name": "Biała Podlaska",
    "cod": 200
    }
  • ADVERTISEMENT
  • #2 21146592
    michal.zd
    Level 30  
    Arduino has a json object parser. See example:
    https://arduinojson.org/v6/example/parser/

    Or most simply use the String::find method to find the word "temp", get the first index, then find the comma again but from that index from the first find. You'll have the start and end indexes, the start index immediately switch those six plus one to the forward colon and then the substring method cut the bit you're interested in.
    I'm writing from a tablet, so I won't throw the code.
  • ADVERTISEMENT
  • #3 21147022
    michal.zd
    Level 30  
    
    float getWetherTemperature(std::string json)
    {
        std::string temperature;
        int positionbeg;
        int positionend;
        
        positionbeg = json.find("\"temp\"");
        if(positionbeg == std::string::npos) return -100; // pozycja nie znaleziona
        
        // szukam delimitera kolejnych elementów w json,
        // moze nie być przecinka, jesli to ostatni element w obiekcie, wówczas będzie klamerka
        positionend = json.find(",", positionbeg);
        if(positionend == std::string::npos) positionend = json.find("}", positionbeg);
        if(positionend == std::string::npos) return -100; // błędny json
        if(positionbeg >= positionend) return -100; // błędny json
        
        // wycinam odpowiedni kawałek i usuwam nazwe zmiennej
        temperature = json.substr(positionbeg, (positionend - positionbeg) );
        positionbeg = temperature.find(":");
        if(positionbeg == std::string::npos) return -100; // pozycja nie znaleziona
        temperature = temperature.substr(++positionbeg);
         
        std::cout << "string temperature [" << temperature << "]" << std::endl;
        
        return std::stof(temperature);
    }
    
    .

    this is code for the normal string library, Arduino has its own equivalent, but I think it has such methods.
  • ADVERTISEMENT
  • #5 21366603
    interim
    Level 8  
    You can from an Assessment, but you can also from a weather station located close to where you live. Usually the accuracy is higher. In addition, we can get data on air quality, PM2.5, PM10, etc. A tiny Python script.
ADVERTISEMENT