Skip to content

Instantly share code, notes, and snippets.

@bathtimefish
Created November 21, 2015 07:37
Show Gist options
  • Save bathtimefish/65f8e1ef93d7696eb60f to your computer and use it in GitHub Desktop.
Save bathtimefish/65f8e1ef93d7696eb60f to your computer and use it in GitHub Desktop.
Espruino Pico + BME280 + ESP8266 で温度、湿度、気圧をMQTTで送信する
/*
* BME280のほうは別電源で3V3入れた。(GND共有) picoのみだと足りないみたい。
*/
var pth_message = null;
I2C2.setup({scl:B10,sda:B3});
var bme = require("BME280").connect(I2C2);
setInterval(function() {
bme.readRawData();
var temp_cal = bme.calibration_T(bme.temp_raw);
var press_cal = bme.calibration_P(bme.pres_raw);
var hum_cal = bme.calibration_H(bme.hum_raw);
var temp_act = temp_cal / 100.0;
var press_act = press_cal / 100.0;
var hum_act = hum_cal / 1024.0;
console.log("Temperature: " + temp_act + " C");
console.log("Humidity: "+ hum_act+" %");
console.log("Pressure: " + press_act + " hPa");
pth_message = temp_act.toString() + "C, " + hum_act.toString() + "%, " + press_act.toString() + " hPa, ";
}, 2000);
var config = {
wifiSSID: 'SSID',
wifiKey: 'PASSWORD',
mqttHost: '192.168.x.x'
};
var mqtt = require("MQTT").create(config.mqttHost);
var wifi, sensor,temp;
mqtt.on('connected', function() {
console.log('mqtt connected');
var topic = "test/espruino";
setInterval(function() {
var message = pth_message;
if(!pth_message) message = 'NULL';
mqtt.publish(topic, message);
}, 2000);
});
// main
function main(){
// WIFI
Serial1.setup(115200, { rx: B7, tx : B6 });
wifi = require("ESP8266WiFi_0v25").connect(Serial1, function(err) {
wifi.reset(function(err) {
if (err) throw err;
wifi.connect( config.wifiSSID , config.wifiKey, function (err) {
if (err) throw err;
console.log("Connected");
mqtt.connect();
});
});
});
}
/*
* 電圧の関係上BME280を先に起動させたほうがよさそうなのでWifiは遅れて起動する
*/
setTimeout(function() {
console.log('start connection');
main();
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment