How to get weather from an API

I try to get the weather from an api. I have my key and it works fine but the code give me a 404 error. I started with an example with WIFININA and the example works fine, but if i substitute the weatherstack information the server return an error 404. (connected to server
HTTP/1.0 404 Not Found)

/*
Web client
This sketch connects to a website (http://www.google.com)
using the WiFi module.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.

Circuit:

  • Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)
    created 13 July 2010
    by dlf (Metodo2 srl)
    modified 31 May 2012
    by Tom Igoe
    */

#include <SPI.h>
#include <WiFiNINA.h>

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):

WiFiClient client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:

status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);

}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// API api.weatherstack.com/current?access_key=bb0badd5e733943310f3632acc2d4130&query=Blainville

client.println("GET /current?access_key=bb0badd5e733943310f3632acc2d4130&query=Blainville");
client.println("Host: www.api.weatherstack.com");
client.println("Connection: close");
client.println();
// // Make a HTTP request:    
// client.println("GET /search?q=arduino HTTP/1.1");
// client.println("Host: www.google.com");
// client.println("Connection: close");
// client.println();

}
}

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();

// do nothing forevermore:
while (true);

}
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}

Please can you paste all of your code in code tabs, and also post your serial monitor output in code tabs.

You've got commas in your IP address, shouldn't they be periods/full stops?

Commas are correct:

The problem is solved, i used the code of Lorenzoromagnoli with succes. My board is a MKR1010 so i used the WIFININA library rather than WiFi101 for the web connexion.

get_weather_databy lorenzoromagnoli for
Arduino MKR1000:
https://app.arduino.cc/sketches/cb4a907b-d26a-42f9-864f-ba91098be43a?view-mode=preview

#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

// char ssid[] = SECRET_SSID; //  your network SSID (name)
// char pass[] = SECRET_PSW;//  your network PASSWORD ()

//open weather map api key
String apiKey= SECRET_APIKEY;

//the city you want the weather for
//String location= "torino,IT";
String location= "blainville,ca";
int status = WL_IDLE_STATUS;
char server[] = "api.openweathermap.org";    

WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    
    status = WiFi.begin(ssid);
    //use the line below if your network is protected by wpa password 
    status = WiFi.begin(ssid, pass);
    
    // wait 10 seconds for connection:
    delay(1000);
  }
  Serial.println("Connected to wifi");
}

void loop() {
  getWeather();
  delay(10000);
}


void getWeather() {

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.print("GET /data/2.5/forecast?");
    client.print("q="+location);
    client.print("&appid="+apiKey);
    client.print("&cnt=3");
    client.println("&units=metric");
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");
    client.println();
  } else {
    Serial.println("unable to connect");
  }

  delay(1000);
  String line = "";

  while (client.connected()) {
    line = client.readStringUntil('\n');
    Serial.println(line);
  }
}
**Serial monitor:**

Starting connection to server...
connected to server
{"cod":"200","message":0,"cnt":3,"list":[{"dt":1736110800,"main":{"temp":-11.64,"feels_like":-18.64,"temp_min":-11.64,"temp_max":-10.6,"pressure":1015,"sea_level":1015,"grnd_level":1010,"humidity":61,"temp_kf":-1.04},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":83},"wind":{"speed":4.43,"deg":299,"gust":7.1},"visibility":10000,"pop":0,"sys":{"pod":"d"},"dt_txt":"2025-01-05 21:00:00"},{"dt":1736121600,"main":{"temp":-11.52,"feels_like":-17.41,"temp_min":-11.52,"temp_max":-11.2,"pressure":1017,"sea_level":1017,"grnd_level":1011,"humidity":67,"temp_kf":-0.32},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":70},"wind":{"speed":3.06,"deg":296,"gust":7.33},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2025-01-06 00:00:00"},{"dt":1736132400,"main":{"temp":-11.3,"feels_like":-17.4,"temp_min":-11.3,"temp_max":-11.3,"pressure":1018,"sea_level":1018,"grnd_level":1012,"humidity":75,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":9},"wind":{"speed":3.27,"deg":272,"gust":6.77},"visibility":10000,"pop":0,"sys":{"pod":"n"},"dt_txt":"2025-01-06 03:00:00"}],"city":{"id":5903510,"name":"Blainville","coord":{"lat":45.6668,"lon":-73.8825},"country":"CA","population":46493,"timezone":-18000,"sunrise":1736080554,"sunset":1736112349}}

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