基本使用法
kaluma flash ./index.js --bundle and in kaluma shell type .load
----------------------ds1307RTC------i2cはビルトイン-------------
ds1307パッケージのsda,sclはどこにも記載がないがsda1,scl1(gpio2,gpio3)で成功!
bus電圧は5v必須だった!
const { I2C } = require('i2c');
const DS1307 = require('ds1307');
const i2c = new I2C(1, { mode: I2C.MASTER });
const ds1307 = new DS1307(i2c);
const date = new Date('2022-09-04T17:01:48.026Z');
ds1307.setDate(date);
setInterval(() => {
console.log(ds1307.getDate().toISOString());
}, 1000)
-------------dht11-------------------------------------------------------
https://javascript.plainenglish.io/physical-computing-with-javascript-5-8-sensing-temperature-and-humidity-2040eb1d20e9 にしたがいdht11をうごかす
$ mkdir dht11-test
$ cd dht11-test
$ npm init -y
$ npm install https://github.com/niklauslee/dht --save
これでnode_modulesにdhtができる 本文から改変
const pin = 15;
const {DHT} = require('dht'); // @kaluma/不要
const dht = new DHT(pin, DHT.DHT11);
let result = dht.read();
if (result) {
console.log(result);
console.log("Humidity: " + dht.humidity + " %");
console.log("Temperature: " + dht.temperature + " C");
} else {
console.log('Failed to read');
}
このindex.jsを以下のようにしてflash(DHTもバンドルされる) --portはlinuxでは不要
$ kaluma flash ./index.js --port <port> --bundle
あとはkaluma shellで.loadとすればOK
----------ssd1306 and simplefont---------------------------------------------------
https://kalumajs.org/packages/ のバッケージで ssd1306をインスト
const {SSD1306}=require('ssd1306/i2c');
const {I2C} = require('i2c');
const ssd1306 = new SSD1306();
let i2c0 = new I2C(0);
ssd1306.setup(i2c0,{width:128, height:64});
const gc = ssd1306.getContext();
gc.drawRect(0,0,128,64);
gc.display();
同一ディレクトリにsimplefontパッケージもインスト
const {SSD1306} = require('ssd1306/i2c');
const ssd1306 = new SSD1306();
ssd1306.setup(board.i2c(0), {
width: 128,
height: 64
});
var font = require('simple-fonts/minimal');
// var font = require('simple-fonts/leros');
// var font = require('simple-fonts/yamaha');
// var font = require('simple-fonts/lee-sans');
const gc = ssd1306.getContext();
gc.setFont(font);
gc.drawText(0, 0, "LOREM IPSUM DOLOR SIT AMET...");
gc.display();
--------------mqtt---------------raspicow にはwifiがビルトイン!--------------------------
https://github.com/pankleks/picomqtt/blob/main/example.js を参考にした
const WiFi = require("wifi").WiFi,{ PicoMQTT } = require("picomqtt/picomqtt.min");
//ここがキモ,ビルトインwifiの中でPicoMQTTをode_modulesによびに行く
const wifi = new WiFi();
wifi.connect({ ssid: "HUAWEI@nova@lite@3+", password: "99991111" }, (ex) => {
if (ex)
console.error(ex);
else {
console.log("wifi connected");
const mqtt = new PicoMQTT("broker.emqx.io", { 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);
}
});