write me a arduino script that uses the open-meteo.com api to get the weather in berlin
Sure! Below is a simple Arduino script that uses the `Open-Meteo.com` API to get the current weather in Berlin, Germany. Before you start, make sure you have an internet connection on your Arduino. If you are using an ESP8266 or ESP32, they have built-in Wi-Fi capabilities.
### Required Libraries
You'll need the following libraries:
1. **ESP8266WiFi.h** or **WiFi.h** for ESP32
2. **HTTPClient.h** for making HTTP requests
3. Depending on the target board, you may also need to install the corresponding board package (for ESP8266 or ESP32).
### Arduino Code
Here's the code:
```cpp
#include <ESP8266WiFi.h> // Use <WiFi.h> for ESP32
#include <HTTPClient.h>
const char* ssid = "your_SSID"; // Replace with your WiFi SSID
const char* password = "your_PASSWORD"; // Replace with your WiFi password
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
// Make a request to Open-Meteo API for weather in Berlin
if (WiFi.status() == WL_CONNECTED) { // Check WiFi connection
HTTPClient http;
// OpenWeatherMap API URL for Berlin
String api_url = "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.405¤t_weather=true";
http.begin(api_url); // Specify the URL
int httpResponseCode = http.GET(); // Make the request
if (httpResponseCode > 0) {
String payload = http.getString(); // Get the response payload
Serial.println(httpResponseCode); // Print the response code
Serial.println(payload); // Print the payload
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
}
}
void loop() {
// Nothing here for now
}
```
### How to Use the Code
1. Replace `your_SSID` and `your_PASSWORD` with your actual Wi-Fi credentials.
2. Upload the code to your ESP8266 or ESP32 board.
3. Open the Serial Monitor (make sure the baud rate is set to 115200).
4. Once connected to Wi-Fi, it will fetch the weather data from Open-Meteo for Berlin and print it to the Serial Monitor.
### Note
- Make sure that you have a stable internet connection.
- The response from the API will be in JSON format. You can parse it using a JSON library like `ArduinoJson` if you want to extract specific information (temperature, humidity, etc.).
### Dependencies
To use the `ESP8266WiFi` or `WiFi` and `HTTPClient` libraries, ensure you've selected the appropriate board in the Arduino IDE and installed the necessary libraries through the Library Manager. If you need to parse JSON, consider also installing the `ArduinoJson` library.