Unable to connect to Openweathermap

Can someone help me fix this code, it cant connect to openweathermap

#include <ESP8266WiFi.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>
#define BLYNK_TEMPLATE_ID "TMPL66Cczl1Kp"
#define BLYNK_TEMPLATE_NAME "ESP 8266"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#include "BlynkEdgent.h"

// Replace with your own OpenWeatherMap API key
const char* apiKey = "10ea467a47af0c94d933380eec746a33";

// Replace with your city and country code
const char* city = "Davao";
const char* countryCode = "PH";

// Use HTTPS port 443 for secure communication
const int httpsPort = 443;

// Set up the client and SSL certificate
BearSSL::WiFiClientSecure client;
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIF1TCCA7WgAwIBAgIQcEypvxJjoGSHsFecTtT+zTANBgkqhkiG9w0BAQsFADCB\n" \
"...\n" \
"-----END CERTIFICATE-----\n";
X509List certList(rootCACertificate);

// Set up the Wi-Fi connection
void setupWifi() {
  Serial.begin(9600);
  delay(1000);
  Serial.println();
  Serial.print("Connecting to Wi-Fi...");
  
  WiFi.mode(WIFI_STA);
  BlynkEdgent.begin();
  
  while (WiFi.status() != WL_CONNECTED) {
    BlynkEdgent.run();
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println();
  Serial.println("Wi-Fi connected!");
}

// Set up the SSL certificate
void setupCertificate() {
  client.setTrustAnchors(&certList);
  client.setInsecure();
}

// Get weather description from OpenWeatherMap API
void getWeatherData() {
  if (!client.connect("api.openweathermap.org", httpsPort)) {
    Serial.println("Connection to OpenWeatherMap API failed!");
    return;
  }
  
  Serial.println("Connected to OpenWeatherMap API!");
  
  client.print(String("GET /data/2.5/weather?q=") + city + "," + countryCode + "&APPID=" + apiKey + "&units=metric\r\n");
  client.print("Host: api.openweathermap.org\r\n");
  client.print("User-Agent: ESP8266/1.0\r\n");
  client.print("Connection: close\r\n\r\n");

  while (client.connected()) {
    String line = client.readStringUntil('\n');
    
    if (line == "\r") {
      break;
    }
  }
  
  String response = client.readStringUntil('\n');
  DynamicJsonDocument jsonBuffer(1024);
  DeserializationError error = deserializeJson(jsonBuffer, response);
  
  if (error) {
    Serial.println("Parsing failed!");
    return;
  }
  
  const char* weatherDescription = jsonBuffer["weather"][0]["description"];
  
  Serial.print("Weather description: ");
  Serial.println(weatherDescription);
  
  // Send weather description to Blynk app
  Blynk.virtualWrite(V0, weatherDescription);
}

// Set up the NodeMCU
void setup() {
  setupWifi();
  setupCertificate();
}

// Main program loop
void loop() {
  BlynkEdgent.run();
  getWeatherData();

  delay(1000);
}

Add this line after the Parsing Failed message:
Serial.println(error.f_str());
That will indicate why the parsing fails, if you get that far.

Your sketch is mostly working for me. I commented out all of the BLYNK stuff and replaced BlynkEdgent.begin();' with WiFi.begin(SSID, PASSWORD);`

I increased the delay() from 1 second to 10 seconds, thinking it might be a throttling problem. It works (says: "Weather description: overcast clouds") most of the time. Sometimes the String line = client.readStringUntil('\n'); will not see the "\r" and gets the JSON instead. Those times the JSON deserialize will get an error of "Parsing failed! EmptyInput"

Post the complete serial output you get!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.