node.js를 사용하여 날씨를 chibi : bit에 표시
이 기사의 개요
출력
도쿄의 현재 온도를 표시합니다.
운영 환경
chibi:bit 정보
영국의 어린이용 학습용 보드인 BBS micro:bit의 호환기입니다. 웹에서 블록을 조합하여 프로그래밍할 수 있습니다. micro:bit은 일본의 전파법에 적합하지 않기 때문에, 스위치 사이언스씨가 chibi:bit라는 이름의 호환기를 제조, 판매되고 있습니다.
htps //w w. 슈 tch-s 시엔세. 코 m/타타 g/2900/
node.js에서 chibi : bit LED에 텍스트 표시
chibi : bit에 BLE 연동 펌웨어 쓰기
chibi:bit측에는 BLE를 수신하여 LED를 표시하는 펌웨어를 씁니다. LED의 표시 내용은 node.js로 제어합니다. chibi:bit은 수신된 데이터만 표시합니다.
이 기사가 매우 이해하기 쉽기 때문에
htps //w w. 가고 싶다 l. 이. jp/bぉg/170619/
여기를 참고로 진행합니다.
「처음만」의 블록에 「Bluetooth LED 서비스」를 넣어 두는 것이 포인트입니다.
node.js 라이브러리 bbc-microbit 설치
LED의 표시 내용을 제어하기 위해 node.js 라이브러리 인 bbc-microbit을 설치하십시오.
터미널에서 한 번에 설치할 수 있습니다. 적절한 작업 폴더를 만들고 해당 폴더에서 다음 명령을 실행하십시오.
npm install bbc-microbit
라이브러리에 샘플 코드가 많이 들어 있습니다. LED에 텍스트를 표시하려면 다음을 시도해 보십시오.
node node_modules/bbc-microbit/examples/led-text.js
참고 : Github에 소스 코드가 올라 있습니다.
htps : // 기주 b. 코 m / 씨데에 p 미 스트리 / 그래서 - bc - 미 c 로비 t
기상 정보 API로 openWeatherMap 사용
Yahoo라고 할지도 모르지만, 일본어가 되어 버리므로, chibi:bit에서의 표시가 번거롭게 될 것 같았습니다. 그래서 openWeatherMap을 사용하기로 결정했습니다.
ぇtps://오뻬우아아테ぇr마 p. 오 rg/아피
API 키 가져오기
사이트 상단의 "Sign Up"에서 사용자 등록을 하여 API 키를 가져옵니다.
무료 Free Plan에서는 API를 1분에 60회까지 호출할 수 있습니다.
키를 만든 후 10분 정도 기다리지 않으면 움직이지 않으므로 주의하십시오.
API 사용
이 사이트
h tps://요헤이코가. 기주 b. 이오 / 2016 / 08 / 14 / 오페 - ぇ ぇ r - p-by - 그래서 js /
에 게재되어 있는 샘플 코드를 사용하기로 하겠습니다.
도쿄의 기상 정보를 JSON 형식으로 얻을 수 있습니다.
openWeatherMap.js
var http = require('http');
var location = "Tokyo, JP";
var units = 'metric';
var APIKEY = "取得したAPIキーをこちらに入力";
var URL = 'http://api.openweathermap.org/data/2.5/weather?q='+ location +'&units='+ units +'&appid='+ APIKEY;
http.get(URL, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('data', function(chunk) {
res = JSON.parse(body);
console.log(res);
});
}).on('error', function(e) {
console.log(e.message);
});
위를 openWeatherMap.js로 저장하고 터미널에서 명령을 입력하십시오.
node openWeatherMap.js
터미널의 출력 결과는 다음과 같습니다.
{ coord: { lon: 139.69, lat: 35.69 },
weather:
[ { id: 521, main: 'Rain', description: 'shower rain', icon: '09d' },
{ id: 300,
main: 'Drizzle',
description: 'light intensity drizzle',
icon: '09d' } ],
base: 'stations',
main:
{ temp: 28.59,
pressure: 1006,
humidity: 74,
temp_min: 27,
temp_max: 30 },
visibility: 10000,
wind: { speed: 6.2, deg: 70 },
clouds: { all: 75 },
dt: 1501308660,
sys:
{ type: 1,
id: 7612,
message: 0.009,
country: 'JP',
sunrise: 1501271213,
sunset: 1501321684 },
id: 1850147,
name: 'Tokyo',
cod: 200 }
JSON 퍼스
필요한 곳을 사용하기 위해 JSON을 구문 분석합니다. 예를 들어 온도 데이터를 사용하고 싶다면 openWeatherMap.js의 console.log 부분을 작성할 수 있습니다.
openWeatherMap.js
// console.log(res); //openWeatherMap.jsのこの部分を変更。下記のようにすればよい
console.log("temp: " + res.main.temp + "C");
그러면 터미널의 출력 결과는 다음과 같습니다.
temp: 28.59C
기상 정보를 chibi:bit에 표시
상기 방법으로 취득한 텍스트 데이터를 chibi:bit에 표시하면 완료됩니다. 방금 작업 폴더에 아래 파일을 저장하고 실행해보십시오.
led-weather.jp
var BBCMicrobit = require('bbc-microbit');
var msg = "";
// openWeatherMap API から情報を取得
var http = require('http');
var ledDisplayName = "TOKYO"
var location = "Tokyo, JP";
var units = 'metric';
var APIKEY = "APIキーをここに入力";
var URL = 'http://api.openweathermap.org/data/2.5/weather?q='+ location +'&units='+ units +'&appid='+ APIKEY;
http.get(URL, function(res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function(chunk) {
res = JSON.parse(body);
msg = res.main.temp; // 温度を表示したい場合
// msg = res.weather[0].main; // 天気を表示したい場合
console.log("openWeatherMap respose: location " + location + " is " + msg );
});
}).on('error', function(e) {
console.log(e.message);
});
// chibi:bitに表示
console.log('Scanning for microbit');
BBCMicrobit.discover(function(microbit) {
console.log('\tdiscovered microbit: id = %s, address = %s', microbit.id, microbit.address);
microbit.on('disconnect', function() {
console.log('\tmicrobit disconnected!');
process.exit(0);
});
console.log('connecting to microbit');
microbit.connectAndSetUp(function() {
console.log('\tconnected to microbit');
console.log('sending text: "%s"', msg);
microbit.writeLedText(ledDisplayName + ":" + msg, function() {
console.log('\ttext sent');
console.log('disconnecting');
microbit.disconnect();
});
});
});
출력 결과는 이 기사의 첫 번째 이미지와 유사합니다.
후에는 날씨에 맞춘 LED의 표시 패턴을 생각하면 더욱 멋지게 될 것 같네요.
Reference
이 문제에 관하여(node.js를 사용하여 날씨를 chibi : bit에 표시), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/hamatani63/items/5e2370e52a71372400b4텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)