1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
export default async function(interval: Interval) {
const THINGSPEAK_CHANNEL_ID = Deno.env.get("THINGSPEAK_CHANNEL_ID");
const WUNDERGROUND_ID_PW = Deno.env.get("WUNDERGROUND_ID_PW");
const WEATHERCLOUD_STATION_ID = Deno.env.get("WEATHERCLOUD_STATION_ID");
const WEATHERCLOUD_KEY = Deno.env.get("WEATHERCLOUD_KEY");
const wundergroundUrl =
`https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?${WUNDERGROUND_ID_PW}`;
const weatherCloudUrl = `https://api.weathercloud.net/v01/set/wid/${WEATHERCLOUD_STATION_ID}/key/${WEATHERCLOUD_KEY}`;
function formatDateString(dateString) {
// Parse the date string to a Date object
const date = new Date(dateString);
// Extract date components
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, "0"); // Months are 0-based in JS
const day = String(date.getUTCDate()).padStart(2, "0");
// Extract time components
const hours = String(date.getUTCHours()).padStart(2, "0");
const minutes = String(date.getUTCMinutes()).padStart(2, "0");
const seconds = String(date.getUTCSeconds()).padStart(2, "0");
// Format the date and time string
const formattedDate = `${year}-${month}-${day}`;
const formattedTime = `${hours}:${minutes}:${seconds}`;
// URL encode the time string
const encodedTime = encodeURIComponent(formattedTime);
// Combine the date and encoded time
const result = `${formattedDate}+${encodedTime}`;
return result;
}
const fetchLatestTemperatureFromThingSpeak = async (channelId = THINGSPEAK_CHANNEL_ID, resultsCount = 1) => {
const baseUrl = "https://api.thingspeak.com/channels";
const url = `${baseUrl}/${channelId}/feeds.json?results=${resultsCount}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const temperature = data.feeds[0]?.field3;
const createdAt = new Date(data.feeds[0]?.created_at);
const currentTime = new Date();
const oneHourAgo = new Date(currentTime.getTime() - (60 * 60 * 1000));
if (temperature === undefined || createdAt < oneHourAgo) {
throw new Error("Temperature data not found in the feed.");
}
return temperature;
} catch (error) {
console.error("Failed to fetch temperature:", error);
return null; // or handle the error in another way
}
};
const getRequestToWunderGround = async (tempC) => {
const timeNow = new Date();
const tempF = tempC * 1.8 + 32;
const url = `${wundergroundUrl}&dateutc=${formatDateString(timeNow)}&tempf=${tempF}&action=updateraw`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
console.error("Failed to fetch temperature:", error);
}
};
const getRequestToWeatherCloud = async (tempC) => {
const url = `${weatherCloudUrl}/temp/${tempC * 10}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error) {
console.error("Failed to fetch temperature:", error);
}
};
const temperature = await fetchLatestTemperatureFromThingSpeak();
getRequestToWunderGround(temperature);
getRequestToWeatherCloud(temperature);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 8, 2024