Evdin Ursan
Published © GPL3+

Complete wireless weather station

A complete wireless weather station based on Raspberry PI 3 and on a set of Texas Instruments CC2650STK wireless sensors.

IntermediateFull instructions provided2 hours37,118
Complete wireless weather station

Things used in this project

Story

Read more

Code

package.json

JavaScript
node.js required dependencies
{
    "name": "home-meteo",
    "version": "1.0.0",
    "dependencies": {
        "azure-iot-device": "latest",
        "azure-iot-device-amqp": "latest",
        "azure-iot-device-http": "latest",
        "azure-iot-device-mqtt": "latest",
        "sensortag": "latest",
        "async": "latest"
    }
}

Main application source code

JavaScript
This is the main application source code written in node.js
var SensorTag = require('sensortag');		// sensortag library
var Async = require('async');

var Protocol = require('azure-iot-device-amqp').Amqp;
var Client = require('azure-iot-device').Client;
var ConnectionString = require('azure-iot-device').ConnectionString;
var Message = require('azure-iot-device').Message;

var connectionString = 'HostName=XXX.azure-devices.net;DeviceId=home-meteo-device;SharedAccessKey=YYY';
var deviceId = ConnectionString.parse(connectionString).DeviceId;

var client = Client.fromConnectionString(connectionString, Protocol);

client.open(function (error, result) {
    if (error)
    {
        console.log("Connectivity error: %s...", error);
        return;
    }

    SensorTag.discoverAll(function (sensorTag) {
        console.log("Connecting to %s...", sensorTag.id);
        
        sensorTag.on('disconnect', function() {
            console.log("Disconnected from %s!", sensorTag.id);
            process.exit(0);
        });
        
        sensorTag.connectAndSetUp(function (error) { 
            console.log("Connected to %s...", sensorTag.id);
            
            Async.series([
                function (callback)
                {
                    console.log("Starting IR temperatures sensor for %s...", sensorTag.id);
                    sensorTag.enableIrTemperature(callback);
                },
                
                function (callback)
                {
                    console.log("Starting humidity sensor for %s...", sensorTag.id);
                    sensorTag.enableHumidity(callback);
                },
                
                function (callback)
                {
                    console.log("Starting pressure sensor for %s...", sensorTag.id);
                    sensorTag.enableBarometricPressure(callback);
                },
                
                function (callback)
                {
                    console.log("Starting light intensity sensor for %s...", sensorTag.id);
                    sensorTag.enableLuxometer(callback);
                }
            ], function () {
                setInterval(function () {
                    var readings = { sensorId: sensorTag.id };
                    Async.series([
                        function (callback)
                        {
                            sensorTag.readHumidity(function (error, temperature, humidity)
                            {
                                readings.humidity = humidity;
                                readings.temperatureFromHumidity = temperature;
                                
                                callback();
                            });
                        },
                        function (callback)
                        {
                            sensorTag.readIrTemperature(function (error, objectTemperature, ambientTemperature) 
                            {
                                readings.objectTemperature = objectTemperature;
                                readings.temperatureFromIr = ambientTemperature;
                                
                                callback();
                            });
                        },
                        function (callback)
                        {
                            sensorTag.readBarometricPressure(function (error, pressure)
                            {
                                readings.pressure = pressure;
                                
                                callback();
                            });
                        },
                        function (callback)
                        {
                            sensorTag.readLuxometer(function (error, lux){
                                readings.lux = lux;
                                
                                callback();
                            });
                        }
                    ], function()
                    {
                        readings.currentTime = new Date();
                        
                        var message = new Message(JSON.stringify(readings));

                        client.sendEvent(message, function (error) {
                            if (error)
                            {
                                console.log(error.toString());
                            } 
                            else
                            {
                                console.log("Data sent on %s...", readings.currentTime);
                            }
                        });
                    });
                }, 1000);
            });
        });
    });
});

Credits

Evdin Ursan

Evdin Ursan

1 project • 18 followers

Comments