RData ScienceWeather DataAPI
ดึงข้อมูลสภาพอากาศ ไม่ต้องง้อ API Key
การใช้ข้อมูลสภาพอากาศ (Weather Data) ปัญหาคลาสสิกที่หลายคนเจอคือ “ต้องสมัคร API Key” หรือ “โดนจำกัดโควต้าการดึงข้อมูล” แต่ เราสามารถดึงข้อมูลอุณหภูมิและความชื้นรายชั่วโมงได้ ฟรี และไม่ต้องใช้ API Key แถมยังครอบคลุมทุกพื้นที่ในประเทศไทย ไม่ว่าจะเป็นยอดดอย สนามบิน หรือกลางทุ่งนา! วันนี้ผมรวม 4 แหล่งข้อมูลเด็ดๆ พร้อม R Code
1. Open-Meteo (ง่ายที่สุด ยืดหยุ่นที่สุด)
Open-Meteo เป็น API ที่เปิดให้ใช้ฟรี แค่ระบุพิกัด Latitude/Longitude ก็ดึงข้อมูลสภาพอากาศทั้งอดีตและพยากรณ์ล่วงหน้าได้เลย ข้อดีคือใช้แค่แพ็กเกจ jsonlite ตัวเดียว
# ติดตั้งแพ็กเกจ: install.packages("jsonlite")
library(jsonlite)
get_hourly_weather <- function(lat, lon, start_date, end_date) {
base_url <- "https://api.open-meteo.com/v1/forecast"
params <- paste0(
"?latitude=", lat, "&longitude=", lon,
"&start_date=", start_date, "&end_date=", end_date,
"&hourly=temperature_2m,relative_humidity_2m&timezone=auto"
)
weather_data <- fromJSON(paste0(base_url, params))
data.frame(
datetime = as.POSIXct(weather_data$hourly$time, format="%Y-%m-%dT%H:%M"),
temperature_c = weather_data$hourly$temperature_2m,
humidity_percent = weather_data$hourly$relative_humidity_2m
)
}
## ตัวอย่างการดึงข้อมูลพิกัดกรุงเทพฯ
bkk_weather <- get_hourly_weather(13.7563, 100.5018, "2026-03-01", "2026-03-07")
head(bkk_weather)
2. NASA POWER (ข้อมูลดาวเทียมระดับโลก)
ฐานข้อมูลจาก NASA ที่เกิดมาเพื่องานด้านการเกษตรและพลังงาน ให้ข้อมูลครอบคลุมทั่วโลกผ่านแพ็กเกจ nasapower โดยตรง
# ติดตั้งแพ็กเกจ: install.packages("nasapower")
library(nasapower)
# ดึงข้อมูลอุณหภูมิ (T2M) และความชื้น (RH2M)
nasa_weather <- get_power(
community = "ag",
lonlat = c(100.5018, 13.7563), # Longitude, Latitude
pars = c("T2M", "RH2M"),
dates = c("2026-03-01", "2026-03-07"),
temporal_api = "hourly"
)
head(nasa_weather)
3. ข้อมูลสถานีจริงจากสนามบิน (IEM / riem)
ถ้าไม่ชอบข้อมูลจากการประมาณค่าด้วยดาวเทียม และอยากได้ “ข้อมูลที่วัดจากเซ็นเซอร์จริง” เราสามารถดึงข้อมูลจากสถานีตรวจอากาศของสนามบินในไทยได้ผ่านแพ็กเกจ riem
- VTBS = สุวรรณภูมิ
- VTBD = ดอนเมือง
- VTCC = เชียงใหม่
# ติดตั้งแพ็กเกจ: install.packages("riem")
library(riem)
# ดึงข้อมูลของสนามบินดอนเมือง (รหัส VTBD)
airport_weather <- riem_measures(
station = "VTBD",
date_start = "2026-03-01",
date_end = "2026-03-07"
)
# ข้อมูลจะมีคอลัมน์ tmpf (อุณหภูมิฟาเรนไฮต์) และ relh (ความชื้น)
head(airport_weather)
4. MET Norway (สถาบันอุตุนิยมวิทยานอร์เวย์)
แหล่งข้อมูลเบื้องหลังแอปพยากรณ์อากาศชื่อดังอย่าง Yr.no แม่นยำมาก แต่มีกฎเหล็กคือ ต้องใส่ User-Agent เพื่อระบุตัวตนเสมอ
# ติดตั้งแพ็กเกจ: install.packages(c("httr", "jsonlite"))
library(httr)
library(jsonlite)
url <- "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=13.75&lon=100.50"
# เปลี่ยน User-Agent เป็นอีเมลของคุณ
response <- GET(url, user_agent("MyWeatherScript/1.0 (your_email@example.com)"))
if (status_code(response) == 200) {
weather_data <- content(response, "text", encoding = "UTF-8") %>% fromJSON()
times <- weather_data$properties$timeseries$time
details <- weather_data$properties$timeseries$data$instant$details
met_df <- data.frame(
datetime = as.POSIXct(times, format="%Y-%m-%dT%H:%M:%SZ", tz="UTC"),
temperature_c = details$air_temperature,
humidity_percent = details$relative_humidity
)
head(met_df)
}