raspberry pi 關閉7寸觸控螢幕的方法

關閉:

sudo sh -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'

開啟:

sudo sh -c 'echo 0 > /sys/class/backlight/rpi_backlight/bl_power'

切換:

if [ "$(cat /sys/class/backlight/rpi_backlight/bl_power)" -eq "0" ]; then \
  sudo sh -c "echo 1 > /sys/class/backlight/rpi_backlight/bl_power"; \
else \
  sudo sh -c "echo 0 > /sys/class/backlight/rpi_backlight/bl_power"; fi

使用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

raspberry wifi設定

修改/etc/wpa_supplicant/wpa_supplicant.conf

sudo vim /etc/wpa_supplicant/wpa_supplicant.conf

輸入wifi ap資訊

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev 
update_config=1 
country=TW 
network={ 
  ssid = "wifi_ap_ssidname"
  psk =  "wifi_ap_password"
  key_mgmt=WPA-PSK 
}

存檔,重開機既可

解決docker在raspberry pi上出現 cgroups: memory cgroup not supported on this system 之錯誤

目前 raspberry pi 2017-11-29-raspbian-stretch 跟 docker 17.11.0會出現 cgroups: memory cgroup not supported on this system 之錯誤
所以暫時解決的辦法是將docker降到17.09.0

sudo apt-get remove -y docker-ce
sudo apt install -y docker-ce=17.09.0~ce-0~raspbian
sudo apt-mark hold docker-ce

等之後問題解決了就可以用下列指令解鎖,並升級

sudo apt-mark unhold docker-ce
sudo apt-get upgrade -y

參考出處

install docker on raspberry pi zero w

在raspberry pi上安裝好NOOBS,
可透過下列指令直接安裝docker(ps: sudo預設也不需要密碼)

curl -sSL https://get.docker.com | sh

接著將pi使用者加入docker群組
可透過下列指令直接安裝docker(ps: sudo預設也不需要密碼)

sudo usermod -aG docker pi

最後重新登入就可以用docker了

nikon d600 快門線腳位

快門線腳位
pin1是7V電源
pin2是GND
pin6是GPS訊號(UART之RX)
pin7與pin8以及pin2短路時是照相
pin8與pin2短路時是對焦

轉換到mini usb後對應的腳位
VBus是7V
GND是接地
D-是GPS訊號(UART之RX)
D+與IO以及GND短路時是照相
ID與GND短路時是對焦

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);