# Retrieving the outdoor temperature from openweathermap.org

**URL:** <https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865>\
**Category:** Programming\
**Created:** [June 2, 2026, 9:04pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865 "2026-06-02T21:04:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikemarcu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikemarcu/32/1126588_2.png) [@mikemarcu](https://forum.arduino.cc/u/mikemarcu)\
**Post date:** [June 2, 2026, 9:04pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/1 "2026-06-02T21:04:41Z")

</div>

I'm trying to get my head around using [openweathermap.org](http://openweathermap.org) info.  
I have an APIkey , as well as my city info, working on a weather station.  
I'm trying no to only retrieve the temperature for the location.  
With the following sketch I get "Error code: -1"  
I added to the sketch a few testing points and it seem that error comes after executing the line "if (httpResponseCode \> 0) {"  
Here is the sketch:

```cpp
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

// Wi-Fi Credentials
const char* ssid = "Pajura1";
const char* password = "sugipula2015!";

// OpenWeatherMap API Details
// Example URL: api.openweathermap.org/data/2.5/weather?q=Brossard,CA&appid=YOUR_API_KEY&units=metric
String apiKey = "7fedc1d7e597836c7793ca30d42826e1";
String city = "5909629"; // Or your specific city
String countryCode = "CA";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected!");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("WIFI connected");
    HTTPClient http;

    // Construct the API request URL
    String serverPath = "http://openweathermap.org" + city + "," + countryCode + "&appid=" + apiKey + "&units=metric";

    http.begin(serverPath.c_str());
    int httpResponseCode = http.GET();
    Serial.println("1");

    if (httpResponseCode > 0) {
      String payload = http.getString();
      Serial.println("2");
      // Parse the JSON response
      DynamicJsonDocument doc(1024);
      deserializeJson(doc, payload);
      Serial.println("3");
      float temp = doc["main"]["temp"];

      Serial.print("Current outdoor temperature in ");
      Serial.print(city);
      Serial.print(": ");
      Serial.print(temp);
      Serial.println(" °C");
    } else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }

  delay(60000); // Wait 1 minute before requesting again

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 2, 2026, 9:44pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/2 "2026-06-02T21:44:19Z")

</div>

Please post the full error message, using code tags when you do

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [June 2, 2026, 10:52pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/3 "2026-06-02T22:52:57Z")

</div>

> [@mikemarcu](#):
>
> ```arduino
> String serverPath = "http://openweathermap.org" + city + "," + countryCode + "&appid=" + apiKey + "&units=metric";
> 
> ```

You probably need a `/` between the hostname and city: e.g. "org/Peoria".

Bigger problem: you might need to use `https`

```http
$ curl -I http://openweathermap.org
HTTP/1.1 301 Moved Permanently
Server: nginx/1.30.1
Date: Tue, 02 Jun 2026 22:51:03 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://openweathermap.org/

```

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [June 2, 2026, 11:00pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/4 "2026-06-02T23:00:09Z")

</div>

I know that country CA is Canada, but I don't recognize a city called 5909629.  
I use the weather api and my code is different than yours.

---

<div class="post-metadata">

**Author:** ![embeddedkiddie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/embeddedkiddie/32/1081520_2.png) [@embeddedkiddie](https://forum.arduino.cc/u/embeddedkiddie)\
**Post date:** [June 2, 2026, 11:02pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/5 "2026-06-02T23:02:09Z")

</div>

The API needs `api.openweathermap.org/data/2.5/weather?`.

> [@mikemarcu](#):
>
> ```arduino
> // OpenWeatherMap API Details
> // Example URL: api.openweathermap.org/data/2.5/weather?q=Brossard,CA&appid=YOUR_API_KEY&units=metric
> 
> ```

But your code is

> [@mikemarcu](#):
>
> ```arduino
> // Construct the API request URL
> String serverPath = "http://openweathermap.org" + city + "," + countryCode + "&appid=" + apiKey + "&units=metric";
> 
> ```

So check the API:

> **[Current weather data](https://openweathermap.org/api/current?collection=current_forecast)**
>
> Obtain real-time weather data for any location globally with OpenWeather's Current Weather Data API. Provides comprehensive weather details in JSON, XML, or HTML formats, including temperature, pressure, humidity, and more. Supports geocoding,...

You can check the response by directly typing the URL into your browser like this:

```arduino
http://api.openweathermap.org/data/2.5/weather?q=Brossard,CA&units=metric&appid=...

```

---

<div class="post-metadata">

**Author:** ![mikemarcu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikemarcu/32/1126588_2.png) [@mikemarcu](https://forum.arduino.cc/u/mikemarcu)\
**Post date:** [June 2, 2026, 11:12pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/6 "2026-06-02T23:12:10Z")

</div>

@UKHeliBob  
Serial monitor error code, Error code: -1

---

<div class="post-metadata">

**Author:** ![mikemarcu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikemarcu/32/1126588_2.png) [@mikemarcu](https://forum.arduino.cc/u/mikemarcu)\
**Post date:** [June 2, 2026, 11:15pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/7 "2026-06-02T23:15:10Z")

</div>

@sonofcy , I also tried to use the city name, "Brossard", the same result, the number it comes from [openweathermap.org](http://openweathermap.org)

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [June 2, 2026, 11:53pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/9 "2026-06-02T23:53:23Z")

</div>

I get invalid api key when I do that.

---

<div class="post-metadata">

**Author:** ![mikemarcu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikemarcu/32/1126588_2.png) [@mikemarcu](https://forum.arduino.cc/u/mikemarcu)\
**Post date:** [June 2, 2026, 11:56pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/10 "2026-06-02T23:56:51Z")

</div>

I'm recompiling using city name,

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [June 2, 2026, 11:57pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/11 "2026-06-02T23:57:11Z")

</div>

It's two different things, Name and ID and they are both used in different places. I will pluck out just those lines of code, the weather station code is many files and tons of code. I did NOT write it.

```arduino
// Update the internet based information and update screen
void updateData() {
  time_t now = time(nullptr);

  gfx.fillBuffer(MINI_BLACK);
  gfx.setFont(ArialRoundedMTBold_14);

  drawProgress(50, "Updating conditions...");
  OpenWeatherMapCurrent *currentWeatherClient = new OpenWeatherMapCurrent();
  currentWeatherClient->setMetric(IS_METRIC);
  currentWeatherClient->setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
  currentWeatherClient->updateCurrentById(&currentWeather, OPEN_WEATHER_MAP_API_KEY, OPEN_WEATHER_MAP_LOCATION_ID);
  delete currentWeatherClient;
  currentWeatherClient = nullptr;

  drawProgress(70, "Updating forecasts...");
  OpenWeatherMapForecast *forecastClient = new OpenWeatherMapForecast();
  forecastClient->setMetric(IS_METRIC);
  forecastClient->setLanguage(OPEN_WEATHER_MAP_LANGUAGE);
  uint8_t allowedHours[] = {12, 0};
  forecastClient->setAllowedHours(allowedHours, sizeof(allowedHours));
  forecastClient->updateForecastsById(forecasts, OPEN_WEATHER_MAP_API_KEY, OPEN_WEATHER_MAP_LOCATION_ID, MAX_FORECASTS);
  delete forecastClient;
  forecastClient = nullptr;

```

```arduino
void loadPropertiesFromSpiffs() {
  if (SPIFFS.begin()) {
    const char *msg = "Using '%s' from SPIFFS\n";
    Serial.println("Attempting to read application.properties file from SPIFFS.");
    File f = SPIFFS.open("/application.properties", "r");
    if (f) {
      Serial.println("File exists. Reading and assigning properties.");
      while (f.available()) {
        String key = f.readStringUntil('=');
        String value = f.readStringUntil('\n');
        if (key == "ssid") {
          WIFI_SSID = value.c_str();
          Serial.printf(msg, "ssid");
        } else if (key == "password") {
          WIFI_PASS = value.c_str();
          Serial.printf(msg, "password");
        } else if (key == "timezone") {
          TIMEZONE = getTzInfo(value.c_str());
          Serial.printf(msg, "timezone");
        } else if (key == "owmApiKey") {
          OPEN_WEATHER_MAP_API_KEY = value.c_str();
          Serial.printf(msg, "owmApiKey");
        } else if (key == "owmLocationId") {
          OPEN_WEATHER_MAP_LOCATION_ID = value.c_str();
          Serial.printf(msg, "owmLocationId");
        } else if (key == "locationName") {
          DISPLAYED_LOCATION_NAME = value.c_str();
          Serial.printf(msg, "locationName");
        } else if (key == "isMetric") {
          IS_METRIC = value == "true" ? true : false;

```

---

<div class="post-metadata">

**Author:** ![embeddedkiddie](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/embeddedkiddie/32/1081520_2.png) [@embeddedkiddie](https://forum.arduino.cc/u/embeddedkiddie)\
**Post date:** [June 2, 2026, 11:59pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/12 "2026-06-02T23:59:10Z")

</div>

Please replace `appid=...` with your correct API KEY.

By the way, if you don't keep your API KEY secret, someone else could use it. Even with the free version, you might hit the daily limit.

---

<div class="post-metadata">

**Author:** ![mikemarcu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikemarcu/32/1126588_2.png) [@mikemarcu](https://forum.arduino.cc/u/mikemarcu)\
**Post date:** [June 2, 2026, 11:59pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/13 "2026-06-02T23:59:49Z")

</div>

I have this code from ThingPulse, I just wanted a simple code to get the temperature.

---

<div class="post-metadata">

**Author:** ![mikemarcu](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mikemarcu/32/1126588_2.png) [@mikemarcu](https://forum.arduino.cc/u/mikemarcu)\
**Post date:** [June 3, 2026, 12:01am UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/14 "2026-06-03T00:01:13Z")

</div>

I did, waiting to compile and upload.

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [June 3, 2026, 12:23am UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/15 "2026-06-03T00:23:44Z")

</div>

I think you need both, look carefully at my code

these are the calls to

> [@sonofcy](#):
>
> `updateCurrentById`

and

> [@sonofcy](#):
>
> `updateForecastsById`

notice the byId at the end of the function name whereas the following is the City NAME

> [@sonofcy](#):
>
> ```arduino
> } else if (key == "locationName") {
> DISPLAYED_LOCATION_NAME = value.c_str();
> Serial.printf(msg, "locationName");
> 
> ```

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [June 3, 2026, 12:24am UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/16 "2026-06-03T00:24:40Z")

</div>

Thanks, I forgot. I deleted the post, I hope that takes care of it.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 3, 2026, 5:42am UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/17 "2026-06-03T05:42:40Z")

</div>

Is that the full error message copied using the "COPY ERROR MESSAGES" button that appears when the error occurs ?

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [June 3, 2026, 7:13am UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/18 "2026-06-03T07:13:09Z")

</div>

I think I can answer that. It is not a compiler error. It is generated from the application in response to an http.Get() :

> [@mikemarcu](#):
>
> ```arduino
> Serial.print("Error code: ");
> Serial.println(httpResponseCode);
> 
> ```

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [June 3, 2026, 9:38am UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/19 "2026-06-03T09:38:06Z")

</div>

This worked for me with my WiFi credentials on an ESP32-C3. Surprisingly with plain http instead of https.  
I changed the format of the query to use latitude and longitude to match the API example.  
It spits out warnings about deprecated JSON usage.

```arduino
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> // ver 7.x

// Wi-Fi Credentials
const char* ssid = "Pajura1";
const char* password = "sugipula2015!";

// OpenWeatherMap API Details
// Example URL: api.openweathermap.org/data/2.5/weather?q=Brossard,CA&appid=YOUR_API_KEY&units=metric
String apiKey = "7fedc1d7e597836c7793ca30d42826e1";
String lat = "45.45"; // Brossard, CA
String lon = "-73.47"; // Brossard, CA

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected!");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("WIFI connected");
    HTTPClient http;

    // Construct the API request URL
    String serverPath = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=" + apiKey + "&units=metric";
    Serial.println(serverPath);

    http.begin(serverPath.c_str());
    int httpResponseCode = http.GET();
    Serial.println("1");

    if (httpResponseCode > 0) {
      String payload = http.getString();
      Serial.println("2");
      // Parse the JSON response
      DynamicJsonDocument doc(1024);
      deserializeJson(doc, payload);
      Serial.println("3");
      float temp = doc["main"]["temp"];
      String city = doc["name"];

      Serial.print("Current outdoor temperature in ");
      Serial.print(city);
      Serial.print(": ");
      Serial.print(temp);
      Serial.println(" °C");
    } else {
      Serial.print("Error code: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }

  delay(60000); // Wait 1 minute before requesting again
}

```

and gives this on the serial console:

```arduino
11:36:53.289 -> WIFI connected
11:36:53.289 -> http://api.openweathermap.org/data/2.5/weather?lat=45.45&lon=-73.47&appid=7fedc1d7e597836c7793ca30d42826e1&units=metric
11:36:53.289 -> 1
11:36:53.289 -> 2
11:36:53.289 -> 3
11:36:53.289 -> Current outdoor temperature in Brossard: 12.42 °C

```

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [June 3, 2026, 12:11pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/20 "2026-06-03T12:11:47Z")

</div>

here I have an example with API 3.0 from 2024:

> **[ESP Weather Monitor with OpenweatherMap](https://werner.rothschopf.net/microcontroller/202409_esp_weather_en.htm)**

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [June 3, 2026, 12:49pm UTC](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865/21 "2026-06-03T12:49:19Z")

</div>

> [@embeddedkiddie](#):
>
> By the way, if you don't keep your API KEY secret, someone else could use it. Even with the free version, you might hit the daily limit.

Mod expunged it.

[Next page](https://forum.arduino.cc/t/retrieving-the-outdoor-temperature-from-openweathermap-org/1446865.md?page=2)
