Skip to content

Instantly share code, notes, and snippets.

@recidive
Created September 4, 2017 04:08
Show Gist options
  • Save recidive/38f84f4d6de7f8999c0ce82f6fd6c333 to your computer and use it in GitHub Desktop.
Save recidive/38f84f4d6de7f8999c0ce82f6fd6c333 to your computer and use it in GitHub Desktop.
Espruino DS18B20 and BMP085 sensors
const wifi = require('Wifi');
const http = require('http');
const DS18B20 = require('DS18B20');
const BMP085 = require('BMP085');
const wifiSSID = 'cool';
const wifiKey = 'secret';
const tempProbePin = NodeMCU.D3;
const atmospherePins = {
scl: NodeMCU.D1,
sda: NodeMCU.D2
};
var sensors;
const onInit = () => {
sensors = initSensors();
wifiConnect(() => {
webserviceStart();
});
};
const initSensors = () => {
// DS18B20 sensor API.
const oneWire = new OneWire(tempProbePin);
const tempProbe = DS18B20.connect(oneWire);
// BMP085 sensor API.
I2C1.setup(atmospherePins);
const atmosphere = BMP085.connect(I2C1);
return {
tempProbe: tempProbe,
atmosphere: atmosphere
};
};
const wifiConnect = (cb) => {
wifi.connect(wifiSSID, {
password: wifiKey
}, (err) => {
if (err)
console.log(err);
else {
console.log('Connected!');
cb();
}
});
wifi.setDHCPHostname('joe');
wifi.save();
};
const webserviceStart = () => {
http
.createServer(onPageRequest)
.listen(80);
};
const onPageRequest = (req, res) => {
sensors.atmosphere.getPressure((atm) => {
const sensorData = {
tempProbe: sensors.tempProbe.getTemp(),
atmosphere: atm
};
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify(sensorData));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment