Skip to content

Instantly share code, notes, and snippets.

@Keyes
Created October 10, 2018 22:50
Show Gist options
  • Save Keyes/7220a54c70917a814a6a9146b3118440 to your computer and use it in GitHub Desktop.
Save Keyes/7220a54c70917a814a6a9146b3118440 to your computer and use it in GitHub Desktop.
Very simple thermostat written in Node.js
const sensor = require('ds1820-temp');
const gpio = require('rpi-gpio');
const CHECK_INTERVAL = 30 * 1000 // every 30s
const GPIO_PORT = 13;
const SENSOR_ID = '0000067bdac4';
setInterval(checkTemperature, CHECK_INTERVAL);
async function checkTemperature() {
const currentHour = (new Date()).getHours();
const sensorData = await sensor.readDevice(SENSOR_ID);
const currentTemperatureCelsius = sensorData.value;
let targetTemperatureCelsius = 16;
if (
(currentHour > 7 && currentHour < 9) ||
(currentHour > 18 && currentHour < 23)
) {
targetTemperatureCelsius = 22;
}
gpio.write(GPIO_PORT, currentTemperatureCelsius < targetTemperatureCelsius);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment