2025年4月15日火曜日

NoJohnny5-Nodejs for RasPi::ledpwm,aqm0802,onoff/btn,dht11,

https://github.com/fivdi/pigpio :: npm install pigpio (and npm install assert)

pigpiodがうごいてなかればsudoで起動する たまに

【電子工作】Raspberry Pi OSでpigpioを使ったC言語プログラム実行時に「initInitialise: Can’t lock /var/run/pigpio.pid」とエラーが出た

https://wakky.tech/pigpio-initinitialise-compile-errorを参考に

sudo killall pigpiod で解決する
---led-pwm----------------
const Gpio = require('pigpio').Gpio;

const led = new Gpio(17, {mode: Gpio.OUTPUT});

let dutyCycle = 0;

setInterval(() => {
  led.pwmWrite(dutyCycle);

  dutyCycle += 5;
  if (dutyCycle > 255) {
    dutyCycle = 0;
  }
}, 20); これでLEDのpwmできた

ーーーーーーーーaqm0802ーーーーーーーーーーーーーーーーーーーーーーーーーhttp://www.neko.ne.jp/~freewing/raspberry_pi/raspberry_pi_3_node_js_i2c_lcd/

:: aqm0820 3v,scl1,sda1,gndで動かす 

(どうも手持ちのaqm0802の調子がわるくゼロ行が表示できん!機会あれば再購入?)

以下でaqm0802のi2c情報確認できた

^^^^^^^^^^^^^^^^^^^^

'use strict';


// When run, this program will output the same information as the

// command 'i2cdetect -y -r 1'

const fs = require('fs');

const i2c = require('i2c-bus');

const i2c1 = i2c.openSync(1);


const EBUSY = 16; /* Device or resource busy */


const scan = (first, last) => {

  fs.writeSync(0, '     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f');


  for (let addr = 0; addr <= 127; addr += 1) {

    if (addr % 16 === 0) {

      fs.writeSync(0, '\n' + (addr === 0 ? '0' : ''));

      fs.writeSync(0, addr.toString(16) + ':');

    }


    if (addr < first || addr > last) {

      fs.writeSync(0, '   ');

    } else {

      try {

        i2c1.receiveByteSync(addr);

        fs.writeSync(0, ' ' + addr.toString(16)); // device found, print addr

      } catch (e) {

        if (e.errno === EBUSY) {

          fs.writeSync(0, ' UU');

        } else {

          fs.writeSync(0, ' --');

        }

      }

    }

  }


  fs.writeSync(0, '\n');

};


scan(0x3, 0x77);

----onoff of led (non pwm)-------------

https://qiita.com/ekzemplaro/items/3a982b259db4b1cf19ad :: onoff lib for raspi

var Gpio = require('onoff').Gpio var led = new Gpio(16, 'out') led.writeSync(1) // 0 で off

https://www.npmjs.com/package/onoff が詳しい(PWMも可能かも)

ボタンが押されたら、LED を ON にするプログラム は以下の通り ctl-cでendとでる


console.log('*** start ***')

var Gpio = require('onoff').Gpio,
led = new Gpio(26, 'out'),
button = new Gpio(21, 'in', 'both')
 
button.watch(function (err, value)
{
if (err)
{
throw err
}

if (value)
console.log ('*** ON ***')
}
else
console.log ('*** OFF ***')
}

led.writeSync(value)
})
 
process.on('SIGINT', function () {
led.unexport()
button.unexport()
console.log('*** end ***')
})

-----------dht-------------------------------------------------------------------------

https://qiita.com/kobuchin1/items/da527d0997a1f66b837b :: dht11 for raspi


onst sensor = require('node-dht-sensor');

なconst DHT11    = 11;

const DHT22    = 22;

const data_pin = 18; // gpio


function func() { sensor.read(DHT11, data_pin, (err, temp, humid) => {

  if (err) return console.log('DHT11からデータを取得することができません(ピン番号:%d)', data_pin);

  console.log(new Date().toString());

  console.log('気温: %d℃', temp);

  console.log('湿度: %d%', humid);

})}; // function definition


setInterval(func,2000); // 上記の関数定義を2秒毎に実行

-----------servo-------------------------------------------------

'use strict';


const assert = require('assert');

const Gpio = require('pigpio').Gpio;

const motor = new Gpio(17, {mode: Gpio.OUTPUT});


let pulseWidth = 500;


motor.servoWrite(0);

assert.strictEqual(motor.getServoPulseWidth(), 0,

  'expected pulseWidth to be 0'

);


const iv = setInterval(() => {

  motor.servoWrite(pulseWidth);


  const pulseWidthRead = motor.getServoPulseWidth();

  assert.strictEqual(pulseWidthRead, pulseWidth,

    'expected pulseWidth to be ' + pulseWidth + ', not ' + pulseWidthRead

  );


  pulseWidth += 25;

  if (pulseWidth > 2500) {

    pulseWidth = 500;

  }

}, 20);


//setTimeout(() => {

//  motor.digitalWrite(0);

//  clearInterval(iv);

//}, 2000);

cf https://genkitech.net/raspberry-pi-servo-motor
cf https://gc373.github.io/blog/post/raspi-servo-iot/

0 件のコメント:

コメントを投稿