Cant connect to Openweathermap

Can someone please help me, i cant connect to Openweathermap

#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);
}

You’ve done a great job of revealing your secret Blynk and OpenWeatherMap credentials on a public forum!

This approach is crazy, and very Blynk un-friendly…

You should use a BlynkTimer to call the getWeatherData function, but why on earth would you want to refresh this data once every second?

I suspect that your setupWiFi function and the use of the WiFiClientSecureBearSSL client are interfering with the Blynk Edgent connection. As there is already a HTTP(S) client included in the Edgent sketch, along with a WiFi connection routine that works perfectly, why don’t you use these?

Pete.

To be honest I’m not quite familiar with the complex code of Blynk because I only know the basics of arduino/c++. Can you please help me with my code, I’m having a hard time coding with this kind of problem. Thank you :sweat_smile:

Do a quick search of the forum and you’ll find some OpenWeatherMap examples, the ones from @Blynk_Coeur are where I’d start.

Pete.

1 Like

Ok thank you

Easy, I use it so far

Webhook widget - #9 by Blynk_Coeur