Things used in this project

Code

package.jsonJavaScript
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 codeJavaScript
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

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

PHPoc Blue to Control Door Knob by MQTT Protocol and Cortana
Intermediate
  • 145
  • 8

Programming a PHPoc Blue board to control a servo motor to rotate the knob of a door using MQTT Protocol through voice command.

SSH Raspberry Pi 2 in Ubuntu
Intermediate
  • 57
  • 2

Full instructions

Simple method to SSH Raspberry pi from Ubuntu

Face Detection Using OpenCV With Raspberry Pi
Intermediate
  • 3,641
  • 33

Protip

In this project, we are going to detect faces using OpenCV with Raspberry Pi.

Creating an IoT Server with Home Assistant and MQTT
Intermediate
  • 4,047
  • 22

Protip

This guide will help you set up Home Assistant. Access it from any web browser and automations.

Home Automation Using Wiscore and OpenHab
Intermediate
  • 499
  • 3

Protip

This project will help you set up your very own Alexa-controlled home appliances using the wonderful Wiscore module from RAK Wireless.

Automated GPS Controlled Photo Taker
Intermediate
  • 1,844
  • 20

For this build, I put together a backpack to take pictures when I am close to places that I like; especially in London.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login