どうも、最近アナログゲーム主に、TCGを再びやりたいと思っているFlaxBearです。
でも遊戯王とMTGはカードの種類が多い上にポケカは転売厨ならyoutuberのせいで入手しづらいし、どれも手が出しずらいんですよねorz
今回は、GWで行っている企画「micro:bitとラズパイとBLEを使ったミニお天気電光板」の報告を書いていこうと思います。
まず最初に、どういった企画なのかの説明を...
簡単な構想としては(前の記事で見せたスライドですが)
機能としては、micro:bitのボタンを押すと、Bluetooth通信でボタンの状態をやり取りしているラズパイが感知して天気APIからJSONデータを取得して解析してそのデータをmicro:bitに絵として表示するというものです。まずは、micro:bit側のプログラムから
scratch
javascript
bluetooth.onBluetoothDisconnected(function () {
basic.showIcon(IconNames.Sad)
basic.pause(500)
basic.clearScreen()
})
bluetooth.onBluetoothConnected(function () {
basic.showIcon(IconNames.Happy)
basic.pause(500)
basic.clearScreen()
})
bluetooth.startLEDService()
bluetooth.startButtonService()
すごく簡単でしたorz色々調べていたら、micro:bitのBluetoothの規格がEddyStoneやら、コネクタモードとブロードキャストモードがあり、処理が若干違うなどありましたが...
次はサーバー側(ラズパイ)のプログラムです
// Require
const BBCMicrobit = require('bbc-microbit');
const http = require('http');
// Setting OpenWeaterMap API data
const appid = "";
const id = "";
const today_url = "http://api.openweathermap.org/data/2.5/weather?id=" + id + "&units=metric&appid=" + appid;
const tomorrow_url = "http://api.openweathermap.org/data/2.5/forecast?id=" + id +"&units=metric&cnt=2&appid=" + appid;
const send_pattern = {
// 非表示
"None": {
"value": new Buffer('0000000000', 'hex'),
},
// 晴れ
"Clear": {
// * * *
// ***
// ** **
// ***
// * * *
"value": new Buffer('150E1B0E15', 'hex'),
},
// 曇り
"Clouds": {
// **
// * *
// * *
// ***
"value": new Buffer('000609110E', 'hex'),
},
// 雨
"Rain": {
// *
// * *
// *****
// *
// ***
"value": new Buffer('040A1F0407', 'hex'),
},
// 雷雨
"Thunderstorm": {
// *
// *
// *****
// *
// *
"value": new Buffer('04081F0204', 'hex'),
},
// 雪
"Snow": {
// *
// * *
// *
// * *
// *****
"value": new Buffer('040A040A1F', 'hex'),
},
// 霧
"Mist": {
// *
// ***
// *
// ***
// ***
"value": new Buffer('041C040E0E', 'hex'),
}
};
function printConsoleOpeing()
{
console.log("Hello!! Welcome to micro:Wether!!");
console.log("=================================================");
console.log("");
console.log(" ***************************************** ");
console.log(" * + + + ************* * ");
console.log(" * + + + * * * ");
console.log(" * + *** + * * * ");
console.log("* * * ******************* *");
console.log("* + + + * * + + + | *");
console.log("* * * | *");
console.log(" * + *** + | * ");
console.log(" * + + + | / * ");
console.log(" * + + + ---- * ");
console.log(" ***************************************** ");
console.log("");
console.log("=================================================");
return;
}
function printConsoleUsage()
{
console.log("Usage:");
return;
}
function startMicroWether()
{
console.log("\t* Connect micro:bit Bluetooth");
BBCMicrobit.discover(function(microbit) {
// Connected
console.log('\t* Success!! Connected microbit: id = %s, address = %s', microbit.id, microbit.address);
// Disconnected
microbit.on('disconnect', function() {
console.log('\t* micro:bit disconnected');
console.log('\t Good Bye!!');
process.exit(0);
});
// Button A
microbit.on('buttonAChange', function(value) {
if(value == 1) {
getTodayWeather(microbit);
}
});
// Button B
microbit.on('buttonBChange', function(value) {
if(value == 1) {
getTomorrowWeather(microbit);
}
});
microbit.connectAndSetUp(function() {
microbit.subscribeButtons(function() {});
});
});
}
function getTodayWeather(microbit) {
http.get(today_url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('data', function() {
var weather_data = JSON.parse(body);
var today_weather = weather_data['weather'][0]['main'];
var send_data = send_pattern[today_weather]["value"];
microbit.writeLedMatrixState(send_data, function(){});
setTimeout(function() {
send_data = send_pattern["None"]["value"];
microbit.writeLedMatrixState(send_data, function(){});
}, 30000);
});
});
}
function getTomorrowWeather(microbit) {
http.get(tomorrow_url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('data', function() {
var weather_data = JSON.parse(body);
var tomorrow_weather = weather_data['list'][1]['weather'][0]['main'];
var send_data = send_pattern[tomorrow_weather]["value"];
microbit.writeLedMatrixState(send_data, function(){});
setTimeout(function() {
send_data = send_pattern["None"]["value"];
microbit.writeLedMatrixState(send_data, function(){});
}, 30000);
});
});
}
function main()
{
printConsoleOpeing();
printConsoleUsage();
startMicroWether();
}
main();
主にサーバー側がメインになってます。流れとしては、Bluetoothの接続、各ボタンが押された時の処理(Aボタンは今日の天気、Bボタンは明日の天気)、APIからJSONデータ取得などなど
あとは、サーバー側の説明文の表示をなど作成を行えば完成です。
しかし、10日間の内に1日で完成してしまうとはorz
残りの9日間は何しようか