Skip to content

Instantly share code, notes, and snippets.

@cfree cfree/config.txt Secret
Last active Jun 9, 2019

Embed
What would you like to do?
Cold Snap
dtoverlay=w1-gpio
const { join } = require('path');
const { readFileSync, readdirSync, statSync } = require('fs');
const pathPrefix = '/sys/bus/w1/devices';
function getSensors() {
try {
const results = readdirSync(pathPrefix)
.filter(file => statSync(join(pathPrefix, file)).isDirectory())
.filter(directory => directory !== 'w1_bus_master');
return results;
} catch (e) {
console.error(`Cannot Get Sensor: ${e}`);
return [];
}
}
function readData(sensor) {
try {
const results = readFileSync(`${pathPrefix}/${sensor}/w1_slave`, 'utf-8');
return results;
} catch (e) {
console.error(`Cannot Read Sensor: ${e}`);
return null;
}
}
function parseData(data) {
if (data) {
const [, secondLine] = data.split('\n');
const values = secondLine.split(' ');
const celsiusTemp = values[9].slice(2) / 1000;
return {
celsius: celsiusTemp || null,
fahrenheit: celsiusTemp ? parseInt((celsiusTemp * 1.8) + 32, 10) : null,
};
}
return null;
}
function getTemperature(sensor) {
const data = readData(sensor);
return parseData(data);
}
authtoken: <add_your_token_here>
tunnels:
ssh:
proto: tcp
addr: 22
exports.handler = function(context, event, callback) {
context.getTwilioClient().messages.create({
to: process.env.RECIPIENT_PHONE_NUMBER,
from: process.env.SENDER_PHONE_NUMBER, // Twilio phone number
body: `Cold snap! The current temperature is ${event.temp || ''}deg F, which is at or below your threshold of ${event.threshold || ''}deg F`
}).then(msg => {
callback(null, msg.sid);
}).catch(err => callback(err));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.