I am currently working on a weather station that gets data from OpenWeatherMap's API every 10 minutes.
Every 10 seconds the temperature is published via MQTT in the topic 'local/temperature', so that other systems (for example a heater or air conditioner) can do further actions depending on the temperature.
Every 10 minutes, parallel to the new data retrieval, the weather operations are also published, also via MQTT.
Publishing the data every 10 seconds is a requirement of the project, but not important for this case.
The problem I'm stuck on is this: My request to the API of OWM is done in an extra file, which contains a function that should return the data as an object. At the same time the data is stored in a file, so that in case of a network failure the last local status is saved and can still be used.
I already write into the file, the 'reading when offline' functionality will be added later on. I have also noticed that the assembleURL()
function is actually unnecessary, but I haven't changed that yet.
I'm still relatively new in JavaScript / Nodejs, but I already have experience in Java and Python, so it may be that I have mixed in something from Java by mistake.
Can someone please explain to me why the object I return in openWeatherMapCall.js is undefined? I'm thankful for every hint.
My file weather-station.js that calls the function getData
in openWeatherMapCall.js:
const mqtt = require('mqtt');
const owm = require('./lib/openWeatherMapCall');
const client = mqtt.connect('mqtt://localhost:1885');
const fs = require('fs');
const config = require('../config/config.json');
const owmConfig = config.owm;
let weatherData = owm.getData(owmConfig.city, owmConfig.owmapikey, owmConfig.lang, "metric");
console.log(weatherData); // -> it says undefined
setInterval(_ => {
weatherData = owm.getData(owmConfig.city, owmConfig.owmapikey, owmConfig.lang, "metric");
client.publish("local/condition", toString(weatherData.weatherID));
console.log('successful publish of wID ' + weatherData.weatherID);
}, 600000); //10 min
setInterval(_ => {
client.publish("local/temperature", toString(weatherData.celsius));
console.log('successful publish of ' + weatherData.celsius + ' celsius');
}, 30000); //10 sec
My OWM API call as openWeatherMapCall.js:
const fetch = require('node-fetch');
const fs = require('fs');
const util = require("util");
function assembleURL (city, apiKey, lang, units){
let url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=" + units + "&lang=" + lang + "&appid=" + apiKey;
console.log("url: " + url);
return url;
}
function getData(city, apiKey, lang, units){
let url = assembleURL(city, apiKey, lang, units )
fetch(url)
.then(function(resp) { return resp.json() }) // Convert data to json
.then(function(data) {
var currentWeather = {
weather: data.weather[0].description,
weatherID: data.weather[0].id,
celsius: Math.round(parseFloat(data.main.temp)),
wind: data.wind.speed,
location: data.name
};
let toString = JSON.stringify(currentWeather);
fs.writeFile('../config/weather.json', toString, err => {
if (err) {
console.log('Error while writing', err)
} else {
console.log('Successful write')
}
})
return currentWeather;
})
.catch( err => {
console.log('caught it!',err);
});
}
module.exports = {getData};