Build a Weather Station using Wia, Raspberry Pi and Node.js
This simple device is the perfect way to demonstrate how easy it is to set up an IoT project powered by the Wia platform. The project also comes with some simple project files to get you started.
Demo:
https://wiaio.github.io/wia-weather-station-web/
Project Requirements
- Node and NPM must be installed. How to install Node.js on a Raspberry Pi.
- A Wia account. You can create on for free here.
Setting up the Wia Node.js SDK
- First thing you will want to do is to create a folder for your weather device. Let’s call it
wia-weather-device
.
- Go into the folder and initialise the project by creating a package.json file using the command “
npm init
” (use defaults for everything).
- Install the Wia SDK using the command “
npm install --save wia
“.
Using the Sensehat with Node.js
- Install the nodeimu addon for accessing the Sensehat sensors using the command “
npm install --save nodeimu
“
- Your package.json file should look something like below.
{
"name": "wia-weather-device",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Your Name Here",
"license": "ISC",
"dependencies": {
"nodeimu": "^2.1.9",
"wia": "^1.3.3"
}
}
Create your Weather Device
- Go to the Wia dashboard and select Devices.
- From here, click on the + symbol and the Add New Device modal will appear.
- Enter ‘Weather Station’ as the name and click Add Device.
- Click on View device to go to the device’s overview page.
- Take a note of the device’s secret key, we’ll need this later.
Write code for the weather station
- We need to create a file for our program, let’s call it
index.js
- Copy the code from the example below add to the
index.js
file.
- Replace the
device-secret-key
on line 3 with your device’s secret key that you made a note of earlier.
- Now to test that you have done your setup correctly, simply run
node index.js
- Your device should have updated in the dashboard under the Sensors tab, with your sensor readings. You can also view in in real-time in the Debugger tab:
'use strict';
var wia = require('wia')('device-secret-key');
var util = require('util')
var nodeimu = require('nodeimu');
var IMU = new nodeimu.IMU();
var tic = new Date();
var callback = function (error, data) {
var toc = new Date();
if (error) {
console.log(error);
return;
}
// Send temperature data
wia.sensors.publish({
name: "temperature",
data: data.temperature.toFixed(4) // data received from temperature sensor
});
// Send pressure data
wia.sensors.publish({
name: "pressure",
data: data.pressure.toFixed(4) // data received from pressure sensor
});
// Send humidity data
wia.sensors.publish({
name: "humidity",
data: data.humidity.toFixed(4) // data received from humidity sensor
});
setTimeout(function() { tic = new Date(); IMU.getValue(callback); } , 250 - (toc - tic));
}
// Using the MQTT stream
wia.stream.on('connect', function() {
IMU.getValue(callback);
});
wia.stream.connect();