Last active
November 21, 2015 06:21
-
-
Save bathtimefish/0325b68e9cb301b061ab to your computer and use it in GitHub Desktop.
Espruino Pico + ESP8266 で MQTTをやるサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* broker/subscriberは https://github.com/rockymanobi/espruino-mqtt-sample.git を利用した | |
* via http://qiita.com/rockymanobi/items/83cc39a4a75ea65747e3 | |
* Thank you rockymanobi ! | |
* DS18B20とESP8266への電源供給はpicoだけだと足りないので、DS18B20へ別電源から供給した(GND共有) | |
*/ | |
var config = { | |
wifiSSID: 'SSID', | |
wifiKey: 'PASSWORD', | |
mqttHost: '192.168.xxx.xxx' | |
}; | |
var mqtt = require("MQTT").create(config.mqttHost); | |
var wifi, sensor,temp; | |
/* | |
* 電圧の関係で温度計はWifiより先に実行しておくのが良さそう | |
*/ | |
var ow = new OneWire(B1); | |
var sensor = require("DS18B20").connect(ow); | |
setInterval(function() { | |
temp = sensor.getTemp(); | |
console.log(temp); | |
}, 1000); | |
mqtt.on('connected', function() { | |
console.log('mqtt connected'); | |
//mqtt.subscribe("test"); | |
var topic = "test/espruino"; | |
setInterval(function() { | |
var message = temp.toString(); | |
if(!temp) message = 'NULL'; | |
mqtt.publish(topic, message); | |
}, 1000); | |
}); | |
// main | |
function main(){ | |
sensor = require("DS18B20").connect(ow); | |
// 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(); | |
}); | |
}); | |
}); | |
} | |
/* | |
* Wifiは温度の取得より遅れて実行する | |
*/ | |
setTimeout(function() { | |
console.log('start connection'); | |
main(); | |
}, 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment