Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
George Kartsonas
Published © GPL3+

Smart home climatic control

All in one temperature control center.

IntermediateFull instructions provided1 hour1,630
Smart home climatic control

Things used in this project

Story

Read more

Code

Temperature Sensor Code

JavaScript
Basically what this code does is read the temperature from the temperature sensor and in turn the Edison sends it to Artik Cloud.
var ID = '<Device ID>';  //Device ID from ARTIK Cloud
var TOKEN = '<Device Token>'; //Device Token from ARTIK Cloud

var mraa = require('mraa'); // mraa library for access to gpio
var mqtt = require('mqtt'); // mqtt messaging library for communicating with ARKTK
                           
var analogPin0 = new mraa.Aio(0); //setup access analog input Analog pin 0 (A0)
var B = 3975; // B value of termistor 
var delay = 3*1000; // delay between reads/display

console.log('ARTIK Thermometer');
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the console

var PROTOCOL = 'mqtts';
var BROKER ='api.artik.cloud';
var PORT = 8883;

//Create the url string
var URL = PROTOCOL + '://' + BROKER;
URL += ':' + PORT;

var requireds = { username: ID, password: TOKEN };
var mqttConfig = { 'url': URL, 'requireds': requireds };

var client = mqtt.connect(mqttConfig.url, mqttConfig.requireds);
var TOPIC = '/v1.1/messages/'+ID;

client.on('connect', function () { // Connect MQTT to ARTIK
    console.log('ARTIK connected');
    setInterval(function () { 
        console.log('Published ' + '{"Temperature":'+display_temp+'}');
        client.publish(TOPIC, '{"Temperature":'+display_temp+'}');    
    }, 10*1000);//Keeps publishing every 10000 milliseconds.    
});

read(); // start the loop

function read() {
    // read the temperature sensor, converts to celsius/fahrenheit.
    var tempSensor = analogPin0.read(); //read the value of the analog pin
    var resistance = (1023 - tempSensor) * 10000 / tempSensor; //get the resistance of the sensor;
    var celsius_temperature = 1 / (Math.log(resistance / 10000) / B + 1 / 298.15) - 273.15; //convert to temperature;
    var fahrenheit_temperature = (celsius_temperature * (9 / 5)) + 32;
    display_temp = celsius_temperature;   // Change this to celsius temperature
    }

Credits

George Kartsonas

George Kartsonas

19 projects • 46 followers

Comments