raspberry piとnode-red上で秋月電子通商製のI2C接続小型キャラクタLCDモジュール に文字を出力するflowを作成しました。最初に[パレットの管理]で[node-red-contrib-i2c]を追加します。
設定は
msg.address : I2C機器のアドレス
msg.command : 機器に送るコマンド(最初の1byte)
msg.payload : 機器に送るデータ(2byte目以降 最大32byte)
となります。
LCDに表示するflowは次のようになります。
複数回コマンドを出力する必要があったため、[delay]を使いループを組みました。[LCD表示]ではreturnを工夫して無限ループを終了しています。
ループは”return msg;”で値返し回ります。”return;”のみだとメッセージがないのでループを回らなくなります。
全体を次に置いておきます。
[{"id":"1c3ab761.76a7e9","type":"function","z":"3cd97708.f90178","name":"LCD表示","func":"// context.set('変数名',値); グローバル変数の設定\n// var val = context.get('変数名')||0;グローバル変数の読み込み\n// ||0; の部分は初期化指定 \n// 変数が設定されていない場合内は0に初期化\n\n//プロパティの追加\n//静的変数のように使用できる\n//注意: 外部からの参照も可能\nif (typeof this.times === 'undefined') {\n this.times = 0;\n}\n\nvar lcdData = [\n [0x00,0x38],\n [0x00,0x39],\n [0x00,0x14],\n [0x00,0x73],\n [0x00,0x56],\n [0x00,0x6c],\n [0x00,0x38],\n [0x00,0x01],\n [0x00,0x0c],\n [0x00,0xc0],\n [0x40,'H'],\n [0x40,'e'],\n [0x40,'l'],\n [0x40,'l'],\n [0x40,'o'],\n [0x40,' '],\n [0x40,'w'],\n [0x40,'o'],\n [0x40,'r'],\n [0x40,'l'],\n [0x40,'d'],\n ];\n\nif(times<lcdData.length) {\n msg.address = 0x3e;\n msg.command = lcdData[times][0];\n msg.payload = lcdData[times][1];\n this.times++;\n return msg; \n} else {\n return;\n}\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":567,"wires":[["b6f9485b.c4d978","a4a8e74f.f796a8","39ea46fa.df3bfa"]]},{"id":"d6d6c364.b56ee","type":"inject","z":"3cd97708.f90178","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":254,"y":567,"wires":[["1c3ab761.76a7e9"]]},{"id":"39ea46fa.df3bfa","type":"debug","z":"3cd97708.f90178","name":"","active":true,"console":"false","complete":"false","x":584,"y":611,"wires":[]},{"id":"b6f9485b.c4d978","type":"i2c out","z":"3cd97708.f90178","name":"","address":"","command":"","payload":"payload","payloadType":"msg","count":"1","x":584,"y":567,"wires":[]},{"id":"a4a8e74f.f796a8","type":"delay","z":"3cd97708.f90178","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":417,"y":483,"wires":[["1c3ab761.76a7e9"]]}]