#安裝nvm
brew install nvm
#設定路徑
echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.bash_profile
. ~/.bash_profile
#查看版本
nvm ls-remote
#安裝8.x
nvm install v8.15.1
#顯示已安裝版本
nvm ls
#切換版本
nvm use v8.15.1
#設定PATH
echo "PATH=$PATH:$(dirname $(nvm which v8.15.1))" >> ~/.bash_profile
. ~/.bash_profile
標籤彙整: nodejs
使用google map geolocation api對raspberry pi進行地理定位
下面範例使用了Google Map GeoLocation Api對raspberry pi進行地理定位,
其中key可在https://console.developers.google.com申請,每日免費額度是2500(每100秒限制10000次)
/*
npm i pi-wifi google-geolocation
node main.js
*/
function geolocation(callback){
var geolocation = require ('google-geolocation') ({
key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
});
var piWifi = require('pi-wifi');
piWifi.scan(function(err, networks) {
if (err) {
return console.error(err.message);
}
//console.log(networks);
var items=[];
for(i in networks){
items.push({
'macAddress': networks[i].bssid,
'signalStrength': networks[i].signalLevel,
});
}
var params={
wifiAccessPoints: items,
};
geolocation (params, (err, data) => {
if (err) {
return;
}
var result={
'lat': data.location.lat,
'lng': data.location.lng,
'accuracy': data.accuracy,
'type': 'wifi',
};
if(callback && typeof(callback) == "function")
callback(result);
});
});
}
geolocation(function(location){
console.log(location);
var url ="https://www.google.com.tw/maps/search/"+location.lat+","+location.lng;
console.log(url);
});
install nodejs 6 on raspberry pi
armv6的raspberry pi似乎無法直接用apt-get install方式安裝.
所以改用下列語法安裝
wget http://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-armv6l.tar.gz && \
tar -xzf node-v6.9.5-linux-armv6l.tar.gz && \
mv node-v6.9.5-linux-armv6l nodejs && \
rm node-v6.9.5-linux-armv6l.tar.gz && \
cp -R * /usr/local/ && \
rm -r -f nodejs && \
ln -s /usr/local/nodejs/bin/node /usr/bin/node && \
ln -s /usr/local/nodejs/bin/npm /usr/bin/npm
NTP client for node js / particle
回覆
NTP 封包內容
0~3 Bytes:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN |Mode | Stratum | Poll | Precision |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4~7 Bytes:
Estimated Error
8~11 Bytes:
Estimated Drift Rate
12~15 Bytes:
Reference Clock Identifier
16~23 Bytes:
Reference Timestamp
24~31 Bytes:
Originate Timestamp
32~39 Bytes:
Receive Timestamp
40~47 Bytes:
Transit Timestamp
for node js (參考網路程式碼)
//refer: https://github.com/moonpyk/node-ntp-client/blob/master/lib/ntp-client.js
var NTPClient=function(server,port){
var self=this;
var construct=function (server,port){
self.server=server;
self.port=port;
self.t1=self.now();
self.last_time=self.now();
update();
}
self.now=function(){
return new Date().getTime()/1000.0;
}
var intParer=function(packetBuffer,offset){
var intpart = 0
for (var i = 0; i <= 3; i++) intpart = 256 * intpart + packetBuffer[offset + i]; return intpart; } var timestampParser=function(packetBuffer,offset){ var intpart = intParer(packetBuffer,offset); var fractpart = intParer(packetBuffer,offset+4); //2208988800 = (70 * 365 + 17) * 24 * 60 * 60 是 1970與1900之秒差 if(intpart>2208988800)
intpart-=2208988800;
return parseInt(intpart)+fractpart/0x100000000;
}
var update=function(){
var ntpData = new Buffer(48); //ntpData= 0x1B,0,0,0,0,~,0 //length:48, 24~31 bytes為本地端時間
ntpData[0] = 0x1B;
for (var i = 1; i < 48; i++) ntpData[i] = 0; var udp = require('dgram'); var client = udp.createSocket("udp4"); var timeout = setTimeout(function () { console.log('[error]','timeout'); client.close(); },3000); var errorFired = false; client.on('error', function (err) { if (errorFired) { return; } errorFired = true; console.log('[error]','timeout'); clearTimeout(timeout); }); client.send(ntpData, 0, ntpData.length, port, server, function (err) { if (err) { if (errorFired) { return; } clearTimeout(timeout); errorFired = true; client.close(); return; } client.once('message', function (msg) { clearTimeout(timeout); client.close(); self.t1=self.now(); self.last_time=timestampParser(msg,40); console.log('======================',(self.last_time-self.t1)); }); }); } var dt=function(){ return self.now()-self.t1; }; self.time=function(){ if(dt()>6)
update();
return self.last_time+dt();
}
construct(server,port);
};
var server = "time.stdtime.gov.tw"; //Stratum=2 (第二階時間伺服器)
//server="time.nc7j.com"; //Stratum=1 (第一階時間伺服器)
server="10.31.160.1"; //Stratum=3
var port = 123;
ntp=new NTPClient(server,port);
console.log(ntp.time());
setInterval(function(){
console.log(ntp.now(),ntp.time());
},100);
for paritcle
class NTPClient{
private:
double last_time;
double time_diff;
unsigned long t1;
unsigned long t2;
byte address[4];
int port;
UDP udp;
unsigned long intParser(byte* packetBuffer, int offset){
unsigned long highWord = (packetBuffer[offset+0] >> 8) + packetBuffer[offset+1];
unsigned long lowWord = (packetBuffer[offset+2] >> 8) + packetBuffer[offset+3];
unsigned long intpart = highWord >> 16 | lowWord;
return intpart;
}
double timestampParser(byte* packetBuffer, int offset){
unsigned long intpart = intParser(packetBuffer,offset);
unsigned long fractpart=intParser(packetBuffer,offset+4);
//2208988800 = (70 * 365 + 17) * 24 * 60 * 60 是 1970與1900之秒差
if(intpart>2208988800)
intpart-=2208988800;
return (int)(intpart)+(double)fractpart/0x100000000;
}
double update(){
#define NTP_PACKET_SIZE 48
byte packetBuffer[NTP_PACKET_SIZE]={0};
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0]=0x1B;
this->udp.beginPacket(this->address, this->port);
this->udp.write(packetBuffer, NTP_PACKET_SIZE);
this->udp.endPacket();
while(1){
if ( this->udp.parsePacket() ) {
t1=micros();
this->udp.read(packetBuffer, NTP_PACKET_SIZE);
this->last_time=timestampParser(packetBuffer,40);
return this->last_time;
}
delay(10);
}
return 0;
}
double dt(){
return (micros()-t1)/1000000.0;
}
public:
NTPClient(byte address[], int port=123){
Time.zone(8);
memcpy(this->address, address, 4);
this->port=port;
this->udp.begin(8888); //隨便一個 port
this->update();
}
~NTPClient(){
}
double time(){
if(this->dt()>600) //每10分鐘強制校時
this->update();
return this->last_time+this->dt();
}
};
NTPClient *ntp;
byte address[] = {118,163,81,61}; //time.stdtime.gov.tw
ntp=new NTPClient(address,123);