Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

Raspberry Pi project: Weather forecast bot

With this project you will Raspberry Pi 5 be able to send you a daily weather report via Telegram. This weather report contains information such as current weather conditions and temperature for a city you select. This project provides a great opportunity to learn how to... Raspberry Pi can use for practical applications and to get acquainted with Python programming. You will also learn how to use an API. It is also possible to create this project with the Raspberry Pi 4B.

 This manual covers: 

  • Supplies
  • Get Telgram bot and API key
  • Install Python libraries
  • --
  • Run code
  • Beginner 40% 40%
  • 60 -70 minutes 45% 45%
  • Costs €105,81 55% 55%

Step 1: Supplies:

Step 2: Get Telegram bot and API key

a. Create a Telegram bot by contacting the BotFather in Telegram.

b. Make a note of the bot token you get from the BotFather.

c. Register on the OpenWeatherMap website and get a free API key

Image of an AstroPi

Step 3: Install Python libraries

Before you can run the code, make sure you have the necessary Python libraries installed on you Raspberry Pi 5. You can install these libraries using pip, the Python package manager. Open a terminal on you Raspberry Pi 5 and run the following commands to install python and pip:

sudo apt-get update
sudo apt-get upgrade
sudo apt install python3
sudo apt install python3-pip

Now install the Python libraries

pip install requests
pip install python-telegram-bot

Finally, restart the Raspberry Pi to complete the installation:

sudo reboot

Step 4: Code

Copy and paste the code into Thonny. Replace the placeholders in the code (such as 'YOUR_BOT_TOKEN' and 'YOUR_OPENWEATHERMAP_API_KEY') with the actual bot token and API key you obtained.

import requests
import datetime
from telegram import Bot
import time
import asyncio

 

 

# Replace 'YOUR_BOT_TOKEN' with your own Telegram bot token
bot_token = 'YOUR_BOT_TOKEN_HERE'

# Replace 'CITY' with the name of the city you want to retrieve the weather for
city ​​= 'Amersfoort'

# Replace 'YOUR_OPENWEATHERMAP_API_KEY' with your own free OpenWeatherMap API key
api_key = 'YOUR_OPENWEATHERMAP_API_KEY_HERE'

 

# Function to get the weather forecast
async def get_weather():
    base_url = 'http://api.openweathermap.org/data/2.5/weather'
    params = {
        'q': city,
        'appid': api_key,

        'units': 'metric' # Use 'imperial' for Fahrenheit instead of Celsius
     }
    response = requests.get(base_url, params=params)
    weather_data = response.json()

    if response.status_code == 200:
        weather_description = weather_data['weather'][0]['description']
        temperature = weather_data['main']['temp']
        return f'The weather in {city} today: {weather_description}, Temperature: {temperature}°C'
    else:
        return 'The weather forecast could not be retrieved.'

 

 

# Function to send a Telegram message
async def send_telegram_message(message):
    bot = Bot(token=bot_token)
    chat_id = 'YOUR_CHAT_ID_HERE' # Replace this with your own chat ID

    await bot.send_message(chat_id=chat_id, text=message)

    # Run code continuously

 

 

async def main():

    while True:
        now = datetime.datetime.now()
        if now.hour == 14 and now.minute == 5: # Replace this with your desired time
            weather_report = await get_weather()
            await send_telegram_message(weather_report)

            # Please wait a moment to prevent multiple messages from being sent in quick succession
            await asyncio.sleep(60) # Wait 60 seconds (adjust if necessary)
        else:
            # Wait a moment before the time is checked again
            await asyncio.sleep(60) # Wait 60 seconds (adjust if necessary)

 

 

if __name__ == “__main__”:
    asyncio.run(main())

Step 5: Run code

a. Save the Python script with an appropriate name, e.g weer_project.py.

b. Navigate to the folder where you saved the Python script.

c. Run the script with the following command:

python weather_project.py

The script starts running and checks every minute whether it is time to retrieve the weather forecast and send it to your Telegram chat. To test if it works properly you can adjust the time to a time that is 2 minutes away and then run the code.

 And that was it! Did it not work out? Take a look back at the previous steps to see what went wrong. Did it work? Then take a look at our projects!

The rating of www.elektronicavoorjou.nl at WebwinkelKeur Reviews is 9.3/10 based on 5047 reviews.