Created
April 15, 2025 09:00
-
-
Save anoochit/bd0d93d9c1f8d050a670879fd72d60c7 to your computer and use it in GitHub Desktop.
earth quake notification from -> https://www.facebook.com/watch?v=1331186067935189
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#include <ArduinoJson.h> | |
// กรอก Wi-Fi | |
const char* ssid = "YOUR_WIFI_SSID"; | |
const char* password = "********"; | |
// พิกัดประเทศไทยโดยประมาณ | |
const float TH_LAT = 15.0; | |
const float TH_LON = 101.0; | |
const float MAX_DISTANCE_KM = 1000.0; // รัศมี 1000 กม. | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
Serial.print("Connecting to WiFi"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println("\nConnected!"); | |
fetchEarthquakeData(); | |
} | |
void loop() { | |
delay(10000); // เช็กทุก 1 นาที | |
fetchEarthquakeData(); | |
} | |
void fetchEarthquakeData() { | |
if ((WiFi.status() == WL_CONNECTED)) { | |
HTTPClient http; | |
String url = "https://earthquake.usgs.gov/.../v1.0/summary/4.5_day.geojson"; | |
http.begin(url); | |
int httpCode = http.GET(); | |
if (httpCode > 0) { | |
String payload = http.getString(); | |
//Serial.println(payload);// | |
const size_t capacity = 50 * 1024; | |
DynamicJsonDocument doc(capacity); | |
DeserializationError error = deserializeJson(doc, payload); | |
if (!error) { | |
JsonArray features = doc["features"].as<JsonArray>(); | |
for (JsonObject quake : features) { | |
float magnitude = quake["properties"]["mag"]; | |
const char* place = quake["properties"]["place"]; | |
long time = quake["properties"]["time"]; | |
float lat = quake["geometry"]["coordinates"][1]; | |
float lon = quake["geometry"]["coordinates"][0]; | |
float dist = haversine(lat, lon, TH_LAT, TH_LON); | |
if (dist <= MAX_DISTANCE_KM) { | |
Serial.println("📍 แผ่นดินไหวใกล้ประเทศไทย:"); | |
Serial.printf(" - สถานที่: %s\n", place); | |
Serial.printf(" - ขนาด: %.1f\n", magnitude); | |
Serial.printf(" - พิกัด: %.2f, %.2f (%.1f km)\n", lat, lon, dist); | |
Serial.println("----------------------------------"); | |
} | |
} | |
} else { | |
Serial.println("❌ JSON Parse Error!"); | |
} | |
} else { | |
Serial.printf("❌ HTTP Request failed, code: %d\n", httpCode); | |
} | |
http.end(); | |
} | |
} | |
// คำนวณระยะห่างระหว่างจุด 2 จุด บนโลก | |
float haversine(float lat1, float lon1, float lat2, float lon2) { | |
const float R = 6371.0; // รัศมีโลก (กม.) | |
float dLat = radians(lat2 - lat1); | |
float dLon = radians(lon2 - lon1); | |
float a = sin(dLat/2) * sin(dLat/2) + | |
cos(radians(lat1)) * cos(radians(lat2)) * | |
sin(dLon/2) * sin(dLon/2); | |
float c = 2 * atan2(sqrt(a), sqrt(1-a)); | |
return R * c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment