A website demo code for your temperature sensors, including a weather forecast picked up from openweathermap.org

Let us start with grabbing some external weather data from openweathermap.org The openweathermap.org service has free and paid plans for their weather APIs to obtain weather data in realtime for any city on earth. They

Let us start with grabbing some external weather data from openweathermap.org

The openweathermap.org service has free and paid plans for their weather APIs to obtain weather data in realtime for any city on earth. They also have weather forecast data for the upcoming 5 days in a 3 hour interval. These values are already available in their free plan and this is what I use here to soup up my own weather site with my own temperature displays.

Start by creating an account at https://home.openweathermap.org/

After you are registered, you will already have free access to the 2.5 version of the openweathermap API which already makes the above described weather data available. Head over to their menu “API keys” and grab your own API key here, which we will use later in our code for the weather.php site:
https://home.openweathermap.org/api_keys

Once you have copied your own personal API key, we can create a script that gathers all the information and stores it into our sensor_data database that we created for the climate control system – module one.

Head over to your web server document directory and open a file weather.sh

nano weather.sh
#!/bin/bash # OpenWeatherMap API-Key api_key=”yourkeyhere” # Stadt und Land (z.B., “Munich,DE”) city=”Munich,DE” # Datenbankverbindung db_host=”192.168.x.y” db_user=”yourdbuserhere” db_password=”yourdbpasshere” db_name=”sensor_data” # SQL-Abfrage zur Erstellung der Tabelle (falls noch nicht vorhanden) create_table_sql=”CREATE TABLE IF NOT EXISTS openweathermap ( id INT AUTO_INCREMENT PRIMARY KEY, city VARCHAR(255), temperature FLOAT, min_temperature FLOAT, max_temperature FLOAT, feels_like FLOAT, main_weather VARCHAR(255), description VARCHAR(255), visibility INT, clouds INT, sunrise TIME, sunset TIME, wind_speed FLOAT, wind_deg INT, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP );” # Führe die SQL-Abfrage zur Erstellung der Tabelle aus mysql -h “$db_host” -u “$db_user” -p”$db_password” -e “$create_table_sql” “$db_name” # API-Anfrage an OpenWeatherMap api_url=”http://api.openweathermap.org/data/2.5/weather?q=$city&appid=$api_key” api_response=$(curl -s “$api_url”) # Daten aus der API-Antwort extrahieren (angepasst an JSON-Format) temperature=$(echo “$api_response” | jq -r ‘.main.temp – 273.15’) min_temperature=$(echo “$api_response” | jq -r ‘.main.temp_min – 273.15’) max_temperature=$(echo “$api_response” | jq -r ‘.main.temp_max – 273.15’) feels_like=$(echo “$api_response” | jq -r ‘.main.feels_like – 273.15’) main_weather=$(echo “$api_response” | jq -r ‘.weather[0].main’) description=$(echo “$api_response” | jq -r ‘.weather[0].description’) visibility=$(echo “$api_response” | jq -r ‘.visibility’) clouds=$(echo “$api_response” | jq -r ‘.clouds.all’) sunrise=$(echo “$api_response” | jq -r ‘.sys.sunrise | strftime(“%H:%M:%S”)’) sunset=$(echo “$api_response” | jq -r ‘.sys.sunset | strftime(“%H:%M:%S”)’) wind_speed=$(echo “$api_response” | jq -r ‘.wind.speed’) wind_deg=$(echo “$api_response” | jq -r ‘.wind.deg’) # SQL-Abfrage zur Einfügung der Daten in die Datenbanktabelle insert_sql=”INSERT INTO openweathermap (city, temperature, min_temperature, max_temperature, feels_like, main_weather, description, visibility, clouds, sunrise, sunset, wind_speed, wind_deg) VALUES (‘$city’, $temperature, $min_temperat> # Führe die SQL-Abfrage zur Einfügung der Daten aus mysql -h “$db_host” -u “$db_user” -p”$db_password” -e “$insert_sql” “$db_name”

Confirgure your own City and state, MySQL database settings and your own API key here. Save and exit the file.

Create another one for the forecast data:

nano forecast.sh
#!/bin/bash # OpenWeatherMap API-Key api_key=”yourapikeyhere” # Stadt und Land (z.B., “Munich,DE”) city=”Munich,DE” # Datenbankverbindung db_host=”192.168.x.y” db_user=”yourdbuser” db_password=”yourdbpass” db_name=”sensor_data” # SQL-Abfrage zur Erstellung der Tabelle (falls noch nicht vorhanden) create_table_sql=”CREATE TABLE IF NOT EXISTS forecast ( id INT AUTO_INCREMENT PRIMARY KEY, city VARCHAR(255), forecast_data JSON, timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP );” # Führe die SQL-Abfrage zur Erstellung der Tabelle aus mysql -h “$db_host” -u “$db_user” -p”$db_password” -e “$create_table_sql” “$db_name” # API-Anfrage an OpenWeatherMap Forecast-API api_url=”http://api.openweathermap.org/data/2.5/forecast?q=$city&appid=$api_key” api_response=$(curl -s “$api_url”) # SQL-Abfrage zur Einfügung der API-Daten in die Datenbanktabelle insert_sql=”INSERT INTO forecast (city, forecast_data) VALUES (‘$city’, ‘$api_response’);” # Führe die SQL-Abfrage zur Einfügung der Daten aus mysql -h “$db_host” -u “$db_user” -p”$db_password” -e “$insert_sql” “$db_name”

Confirgure your own City and state, MySQL database settings and your own API key here. Save and exit the file.

And a third script for the “whole day” accumulation.

nano wholeday.sh

#!/bin/bash # API-Schlüssel und Anfrageparameter api_key=”yourapikey” stadt=”Munich” land=”DE” # Datenbank-Parameter db_host=”192.168.x.y” db_name=”sensor_data” db_user=”yourdbuser” db_password=”yourdbpass” # Aktuelles Datum und Zeitstempel timestamp=$(date +’%Y-%m-%d %H:%M:%S’) # API-Anfrage für die tägliche Vorhersage und JSON-Antwort in der Datei “forecast.json” speichern curl -s “https://api.openweathermap.org/data/2.5/forecast?q=${stadt},${land}&appid=${api_key}” > forecast.json # Überprüfen, ob die Anfrage erfolgreich war if [ $? -eq 0 ]; then # Temperatur von Kelvin in Celsius umrechnen kelvin_temp=$(jq -r ‘.list[0].main.temp’ forecast.json) celsius_temp=$(echo “$kelvin_temp – 273.15” | bc) # Wetterbeschreibung extrahieren wetter=$(jq -r ‘.list[0].weather[0].description’ forecast.json) # Zusammenfassung in die Datenbank einfügen und Datum/Zeitstempel hinzufügen mysql -h “$db_host” -u “$db_user” -p”$db_password” “$db_name” <Again, configure your own city, state, API key and database credentials here and save the script.

Now we use a cronjob for all three scripts and gather the data from openweathermap.org every 15 minutes.

crontab -e

And add the following three lines to it:

*/15 * * * * /home/axel/scripts/weather.sh > /dev/null
*/15 * * * * /home/axel/scripts/forecast.sh > /dev/null
*/15 * * * * /home/axel/scripts/wholeday.sh > /dev/null

Now we need a php page to display

Start by opening a file weather.php

nano weather.php

and copy/paste the following code into it:

Munich weather data

Local Weather Sensors and Model Forecast

connect_error) { die(“Verbindung zur Datenbank fehlgeschlagen: ” . $conn->connect_error); } // SQL-Abfrage, um lokale Daten aus der sensor_data-Tabelle abzurufen $local_data_sql = “SELECT Innentemperatur, Aussentemperatur, AirQuality, AirQualityScale, Luftdruck, Feuchtigkeit, valvestate, date, time FROM sensor_data ORDER BY date DESC, time DESC LIMIT 1”; $local_data_result = $conn->query($local_data_sql); if ($local_data_result->num_rows > 0) { $row = $local_data_result->fetch_assoc(); $temperatur = $row[“Innentemperatur”]; $outtemperatur = $row[“Aussentemperatur”]; $luftdruck = $row[“Luftdruck”]; $luftfeuchte = $row[“Feuchtigkeit”]; $rdate = $row[“date”]; $rtime = $row[“time”]; $airqualityscale = $row[“AirQualityScale”]; $valve1 = $row[“valvestate”]; } else { echo “Keine lokalen Daten in der Datenbank gefunden.”; } // SQL-Abfrage, um lokale Daten aus der sensor_data2-Tabelle abzurufen $local_data2_sql = “SELECT Innentemperatur2, Aussentemperatur2, AirQuality2, AirQualityScale2, Luftdruck2, Feuchtigkeit2, valvestate2, date2, time2 FROM sensor_data2 ORDER BY date2 DESC, time2 DESC LIMIT 1”; $local_data2_result = $conn->query($local_data2_sql); if ($local_data2_result->num_rows > 0) { $row = $local_data2_result->fetch_assoc(); $temperatur2 = $row[“Innentemperatur2”]; $outtemperatur2 = $row[“Aussentemperatur2”]; $luftdruck2 = $row[“Luftdruck2”]; $luftfeuchte2 = $row[“Feuchtigkeit2”]; $rdate2 = $row[“date2”]; $rtime2 = $row[“time2”]; $airqualityscale2 = $row[“AirQualityScale2”]; $valve2 = $row[“valvestate2”]; } else { echo “Keine lokalen Daten in der Datenbank gefunden.”; } // SQL-Abfrage, um lokale Daten aus der sensor_data2-Tabelle abzurufen $local_data3_sql = “SELECT Innentemperatur3, Aussentemperatur3, AirQuality3, AirQualityScale3, Luftdruck3, Feuchtigkeit3, valvestate3, date3, time3 FROM sensor_data3 ORDER BY date3 DESC, time3 DESC LIMIT 1”; $local_data3_result = $conn->query($local_data3_sql); if ($local_data3_result->num_rows > 0) { $row = $local_data3_result->fetch_assoc(); $temperatur3 = $row[“Innentemperatur3”]; $outtemperatur3 = $row[“Aussentemperatur3”]; $luftdruck3 = $row[“Luftdruck3”]; $luftfeuchte3 = $row[“Feuchtigkeit3”]; $rdate3 = $row[“date3”]; $rtime3 = $row[“time3”]; $airqualityscale3 = $row[“AirQualityScale3”]; $valve3 = $row[“valvestate3”]; } else { echo “Keine lokalen Daten in der Datenbank gefunden.”; } // SQL-Abfrage, um lokale Daten aus der sensor_data2-Tabelle abzurufen $local_data4_sql = “SELECT Innentemperatur4, Aussentemperatur4, AirQuality4, AirQualityScale4, Luftdruck4, Feuchtigkeit4, valvestate4, date4, time4 FROM sensor_data4 ORDER BY date4 DESC, time4 DESC LIMIT 1”; $local_data4_result = $conn->query($local_data4_sql); if ($local_data4_result->num_rows > 0) { $row = $local_data4_result->fetch_assoc(); $temperatur4 = $row[“Innentemperatur4”]; $outtemperatur4 = $row[“Aussentemperatur4”]; $luftdruck4 = $row[“Luftdruck4”]; $luftfeuchte4 = $row[“Feuchtigkeit4”]; $rdate4 = $row[“date4”]; $rtime4 = $row[“time4”]; $airqualityscale4 = $row[“AirQualityScale4”]; $valve4 = $row[“valvestate4”]; } else { echo “Keine lokalen Daten in der Datenbank gefunden.”; } // SQL-Abfrage, um Daten aus der openweathermap-Tabelle abzurufen $openweathermap_sql = “SELECT temperature, feels_like, main_weather, description, visibility, clouds, sunrise, sunset, wind_speed, wind_deg FROM openweathermap ORDER BY timestamp DESC LIMIT 1”; $openweathermap_result = $conn->query($openweathermap_sql); if ($openweathermap_result->num_rows > 0) { $row = $openweathermap_result->fetch_assoc(); $temperature_celsius = $row[“temperature”]; $feels_like = $row[“feels_like”]; $main_weather = $row[“main_weather”]; $description = $row[“description”]; $visibility = $row[“visibility”]; $clouds = $row[“clouds”]; $sunrise = $row[“sunrise”]; $sunset = $row[“sunset”]; $wind_speed = $row[“wind_speed”]; $wind_deg = $row[“wind_deg”]; } else { echo “Keine OpenWeatherMap-Daten in der Datenbank gefunden.”; } // SQL-Abfrage, um Daten aus der forecast-Tabelle abzurufen $forecast_sql = “SELECT forecast_data FROM forecast ORDER BY timestamp DESC LIMIT 1”; $forecast_result = $conn->query($forecast_sql); if ($forecast_result->num_rows > 0) { $row = $forecast_result->fetch_assoc(); $forecast_data = json_decode($row[“forecast_data”]); } else { echo “Keine Wettervorhersage-Daten in der Datenbank gefunden.”; } // Verbindung zur Datenbank schließen $conn->close(); ?>

Current Weather in Munich, DE

= $colorRange2[0] && $temperature_celsius < $colorRange2[1]) { return $colorRange2[2]; } } return '#000'; // Standardfarbe (Schwarz) für Werte außerhalb des Schemas } // Definiere ein Assoziatives Array, das die Wetter-Icons den Wetterbedingungen zuordnet $weathernow_icons = [ "clear sky" => “day_clear.png”, “few clouds” => “day_partial_cloud.png”, “scattered clouds” => “day_partial_cloud.png”, “broken clouds” => “broken_cloud.png”, “overcast clouds” => “angry_clouds.png”, “light rain” => “day_rain.png”, “moderate rain” => “rain.png”, “heavy intensity rain” => “moderate_rain.png”, “very heavy rain” => “heavy_rain.png”, “extreme rain” => “very_heavy_rain.png”, “freezing rain” => “sleet.png”, “light snow” => “snow.png”, “snow” => “snow.png”, “heavy snow” => “snow.png”, “sleet” => “sleet.png”, “shower sleet” => “sleet.png”, “light rain and snow” => “day_snow.png”, “rain and snow” => “day_snow.png”, “light shower snow” => “snow.png”, “shower snow” => “snow.png”, “heavy shower snow” => “snow.png”, “thunderstorm” => “thunder.png”, “mist” => “mist.png”, “fog” => “fog.png”, “light fog” => “fog.png”, “smoke” => “fog.png”, “haze” => “fog.png”, “sand/dust whirls” => “fog.png”, “volcanic ash” => “fog.png”, “squalls” => “fog.png”, “tornado” => “tornado.png”, “light rain and snow” => “day_snow.png”, “light shower sleet” => “sleet.png”, “light drizzle” => “day_rain.png”, “light intensity drizzle” => “day_rain.png”, “light intensity drizzle rain” => “day_rain.png”, “light shower rain” => “rain.png”, “light intensity shower rain” => “rain.png”, “heavy intensity shower rain” => “rain.png”, “ragged shower rain” => “rain.png”, “light intensity shower rain” => “rain.png”, “shower rain” => “rain.png”, “thunderstorm with light rain” => “day_rain_thunder.png”, “thunderstorm with rain” => “rain_thunder.png”, “thunderstorm with heavy rain” => “rain_thunder.png”, “thunderstorm with light drizzle” => “rain_thunder.png”, “thunderstorm with drizzle” => “rain_thunder.png”, “thunderstorm with heavy drizzle” => “rain_thunder.png”, “default” => “default.png” // Standard-Icon, wenn keine Übereinstimmung gefunden wird ]; $icon_filename = isset($weathernow_icons[$description]) ? $weathernow_icons[$description] : ‘default.png’; echo “$description“; // Temperaturwert in Celsius echo “

Temperature: $temperature_celsius °C

“; ?>

Feels like: °C

Weather: ()

Visibility: m

Clouds: %

Sunrise:

Sunset:

Wind speed: m/s

Direction: °

Upcoming weather next 3 hours

connect_error) { die(“Verbindung zur Datenbank fehlgeschlagen: ” . $conn->connect_error); } // Ersetzen Sie das Abfragedatum “CURDATE()” durch das gewünschte Datum, wenn die Daten nicht korrekt abgerufen werden. $desired_date = date(“Y-m-d”); // Heutiges Datum // $today_forecast_sql = “SELECT temperature, weather, date FROM wholeday WHERE date = ‘$desired_date'”; // $today_forecast_sql = “SELECT temperature, weather, date FROM wholeday ORDER BY date DESC LIMIT 1”; $today_forecast_sql = “SELECT temperature, weather, date FROM wholeday ORDER BY date DESC LIMIT 1”; $today_forecast_result = $conn->query($today_forecast_sql); if ($today_forecast_result->num_rows > 0) { $row = $today_forecast_result->fetch_assoc(); $temperature_today = $row[“temperature”]; $weather_today = $row[“weather”]; $date_today = date_format(date_create($row[“date”]), “Y-m-d”); // Definieren Sie die Icons entsprechend den Wetterbedingungen $weather_icons = [ “clear sky” => “day_clear.png”, “few clouds” => “day_partial_cloud.png”, “scattered clouds” => “day_partial_cloud.png”, “broken clouds” => “broken_cloud.png”, “overcast clouds” => “angry_clouds.png”, “light rain” => “day_rain.png”, “moderate rain” => “rain.png”, “heavy intensity rain” => “moderate_rain.png”, “very heavy rain” => “heavy_rain.png”, “extreme rain” => “very_heavy_rain.png”, “freezing rain” => “sleet.png”, “light snow” => “snow.png”, “snow” => “snow.png”, “heavy snow” => “snow.png”, “sleet” => “sleet.png”, “shower sleet” => “sleet.png”, “light rain and snow” => “day_snow.png”, “rain and snow” => “day_snow.png”, “light shower snow” => “snow.png”, “shower snow” => “snow.png”, “heavy shower snow” => “snow.png”, “thunderstorm” => “thunder.png”, “mist” => “mist.png”, “fog” => “fog.png”, “light fog” => “fog.png”, “smoke” => “fog.png”, “haze” => “fog.png”, “sand/dust whirls” => “fog.png”, “volcanic ash” => “fog.png”, “squalls” => “fog.png”, “tornado” => “tornado.png”, “light rain and snow” => “day_snow.png”, “light shower sleet” => “sleet.png”, “light drizzle” => “day_rain.png”, “light intensity drizzle” => “day_rain.png”, “light intensity drizzle rain” => “day_rain.png”, “light shower rain” => “rain.png”, “light intensity shower rain” => “rain.png”, “heavy intensity shower rain” => “rain.png”, “ragged shower rain” => “rain.png”, “light intensity shower rain” => “rain.png”, “shower rain” => “rain.png”, “thunderstorm with light rain” => “day_rain_thunder.png”, “thunderstorm with rain” => “rain_thunder.png”, “thunderstorm with heavy rain” => “rain_thunder.png”, “thunderstorm with light drizzle” => “rain_thunder.png”, “thunderstorm with drizzle” => “rain_thunder.png”, “thunderstorm with heavy drizzle” => “rain_thunder.png”, “default” => “default.png” ]; $icon_filename_today = isset($weather_icons[$weather_today]) ? $weather_icons[$weather_today] : ‘default.png’; // Geben Sie das Icon, die Temperatur und das Datum aus echo “$weather_today“; echo “

Temperature: $temperature_today °C

“; echo “

Date: $date_today

“; } else { echo “Keine Wettervorhersage-Daten für heute gefunden.”; } // Schließen Sie die Verbindung zur Datenbank $conn->close(); ?>

Live Webcam Stream Austrian Alps