// Wifi Credentials const WIFI_NAME = "YOURSSID"; const WIFI_PASS = "YOURPASS"; // Your location latitude and longitude const lat = 'YOURLAT'; const lon = 'YOURLON'; // Required libs const http = require("http"); const np = require("neopixel"); // Variable to store the time let now; // Variable to store ISS pass info let iss; // Variable to set the interval so we can clear it let intervalId; // Setup Wifi Shim according to instructions found here: // https://www.espruino.com/ESP8266 digitalWrite(B9, 1); // enable on Pico Shim V2 Serial2.setup(115200, { rx: A3, tx: A2 }); // Turn LED off np.write(B5, [0,0,0]); /** * Connect to WIFI */ var wifi = require("ESP8266WiFi_0v25").connect(Serial2, function (err) { if (err) throw err; console.log("Connecting to WiFi"); wifi.connect(WIFI_NAME, WIFI_PASS, err => { if (err) throw err; console.log("Connected"); // Once we have connected, set the UTC time setTime(); }); }); /** * Set the UTC time to sync with the ISS API */ function setTime() { console.log("Setting Time"); http.get('http://worldclockapi.com/api/json/utc/now', res => { let contents = ''; res.on("data", data => contents += data); res.on("close", () => { let timeJson = JSON.parse(contents); now = new Date(timeJson.currentDateTime); // Now we have set the time, let's get the ISS pass data fetchData(); }); }).on("error", e => console.log("Error getting time: ", e)); } /** * Fetch the ISS Pass Data */ function fetchData() { console.log("Fetching Data"); let url = `http://api.open-notify.org/iss-pass.json?lat=${lat}&lon=${lon}`; http.get(url, res => { let contents = ''; res.on("data", data => contents += data); res.on("close", () => { try { iss = JSON.parse(contents); // If we didn't have an error, all is good if (iss.hasOwnProperty('message') && iss.message == 'success') { // Calculate the interval between request time and next rise let interval = (iss.response[0].risetime * 1000) - now.getTime(); let duration = iss.response[0].duration; showDebug(iss); // Set the timeout function (multiply the duration by 1k to convert to ms) setTimeout(flashLED, interval, duration * 1000); } else { // Something went wrong, we'll try again in 30 seconds setTimeout(fetchData, 30 * 1000); } } catch (e) { // There was an error parsing the data, let's try again in 30 seconds console.log("ERROR parsing ISS json", e); setTimeout(fetchData, 30 * 1000); } }); }).on("error", (e) => { // There was an error fetching the data, let's try again in 30 seconds console.log("ERROR fetching data: ", e); setTimeout(fetchData, 30 * 1000); }); } /** * Flash the LED to notify us of the */ function flashLED(duration) { console.log('ISS Overhead!'); let state = true; intervalId = setInterval(() => { if(state) np.write(B5, [100, 10, 10]) else np.write(B5, [10, 30, 100]) state = !state }, 1000); setTimeout(() => { // Clear the interval clearInterval(intervalId); // Turn off LED np.write(B5, [0,0,0]); // Fetch more data! setTime(); }, duration); } /** * Show debug information in terminal */ function showDebug() { console.log("Current time: ", now); console.log("Next rise: ", new Date(iss.response[0].risetime * 1000)); console.log("Pass duration: ", iss.response[0].duration + " seconds. Or " + iss.response[0].duration / 60 + " minutes. "); }