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

MicroPython to Python via SigFox and Azure IoT Hub
Intermediate
  • 43
  • 2

Protip

This is to show how to get messages from a SiPy to your computer via SigFox and Azure IoT Hub.

Pulse Train Hat for Raspberry Pi Mecanum Bot Example
Intermediate
  • 823
  • 15

Full instructions

With this example, we show how to use the PTHAT and wireless Xbox controller to control a Mecanum Bot.

Smart Garden
Intermediate
  • 1,073
  • 12

Smart Garden is plant environmental monitoring system.

GooglePi - Google Assistant on Raspberry Pi
Intermediate
  • 15,590
  • 85

Full instructions

A DIY Google Home using your Raspberry Pi.

Pre-Collision Assist with Pedestrian Detection - Honda Civic
Intermediate
  • 1,702
  • 20

Full instructions

Combine Walabot's RF radar with real-time hazard detection and classification from camera, powered by TensorFlow with YOLO approach.

Detect Motion With OpenCV, No PIR Sensor Needed
Intermediate
  • 831
  • 14

Full instructions

If you have a HAT connected to your Raspberry Pi, you can't use your GPIO. But what if you want to detect motion? This project can help!

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login