2026年9月24日木曜日

kalumajs おさらい

毎回以下は一緒

kaluma bundle ./index.js 依存ライブラリがあろうとなかろうと

kaluma flash ./index.js --bundle こう書かんといかんようになっている 

 kalumas shell で .hi がでたらコードが自動開始される 実行は.loadでおこなう

---ssd1306 and dht11 minimum code by chatgpt----------

 i2c(0)は https://javascript.plainenglish.io/physical-computing-with-javascript-7-8-using-graphic-display-fc57580fc37 にせんとうごかん!

原因として有力なのは、KalumaJS の Pico W では、I2C0 の既定ピンが GP0/GP1 ではないことです。Kaluma の公式ドキュメントでは、I2C の既定設定は次のとおりです。

バス

SDA

SCL

I2C0(既定)

GP4

GP5

I2C1(既定)

GP2

GP3

GP0/GP1 は既定では UART0 に割り当てられています。


const { SSD1306 } = require('ssd1306/i2c');

const { DHT } = require('dht');

const ssd1306 = new SSD1306();

const dht = new DHT(15, DHT.DHT11);

ssd1306.setup(board.i2c(0), {

  width: 128,

  height: 64

});

const gc = ssd1306.getContext();

function update() {

  dht.read();

  gc.clearScreen();

  gc.drawText(0, 0, 'Temp: ' + dht.temperature.toFixed(1) + ' C');

  gc.drawText(0, 10, 'Hum: ' + dht.humidity.toFixed(0) + '%');

  gc.display();

}

update();

setInterval(update, 5000);

ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 

avascript.plainenglish.io/physical-computing-with-javascript-3-8-reading-state-of-switch-106137a2f9d6 :: reading state of button

const LED = 15;
const BTN = 14;

pinMode(LED,OUTPUT);
pinMode(BTN,INPUT_PULLUP);

setWatch(() => {
     digitalToggle(LED);
},BTN,FALLING,10);

-------------------------------------------------------------------------

 https://javascript.plainenglish.io/physical-computing-with-javascript-4-8-led-dimming-403696103d2f  :: led dimming

 const LED = 15;
const POT = 26;
setInterval(() => {
    let v = analogRead(POT);
    analogWrite(LED,v);
},100);

---------------mqtt raspicow専用--------------------------------------------

 https://github.com/pankleks/picomqtt をインストしてnode_modules/picomqttにもぐる そこでkaluma bundle ./index.js --bundleしてブートモードにせずにkaluma shell & .load

const WiFi = require("wifi").WiFi,

{ PicoMQTT } = require("./picomqtt.min");

const wifi = new WiFi();

wifi.connect({ ssid: "your-ssid", password: "your-password" }, (ex) => {

  if (ex)

    console.error(ex);

  else {

    console.log("wifi connected");

    const mqtt = new PicoMQTT("broker.hivemq.com", { clientId: board.uid });

    mqtt.on("connected", () => {

      console.log("mqtt connected");

       mqtt.subscribe("test/topic");

        mqtt.publish("test/topic", "you should get this");

      mqtt.publish("test/topic", "you should also get this");

      mqtt.unsubscribe("test/topic");

      mqtt.publish("test/topic", "you should not get this");

    });

    mqtt.on("message", (msg) => {

      console.log(`mqtt message -> ${msg.topic}: ${msg.message}`);

    });

    mqtt.on("error", (ex) => {

      console.log(`mqtt error -> ${ex.message || ex}`);

    });

    mqtt.on("disconnected", (reconnect) => {

      console.log(`mqtt disconnected, reconnect = ${reconnect}`); 

// if reconnect is true, mqtt will try to reconnect

    });

    mqtt.connect();

    setTimeout(() => {

      mqtt.disconnect();  // stop mqtt

    }, 1000 * 120);

  }

});

----> 次の結果を表示して終了する

.load

wifi connected

mqtt connected

mqtt message -> test/topic: you should get this

mqtt message -> test/topic: you should also get this

mqtt disconnected, reconnect = false



0 件のコメント:

コメントを投稿