4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mruby/cで温度のデータをLoRaで送信する

Posted at

はじめに

普段あまりRubyのコードを書くことが無いのでたまにはRubyのコードを書いてみます。

使用品

RBoard

E220-900T22S

センサーにはLM61CIZを使用します。
LM61CIZを接続するためにブレッドボードとジャンパ線もあるといいです。

受信機には何かパソコンのようなものとE220のUSBモジュールを使用します。

RBoard

ファームを3.3.1へアップデートしてください。

MPLAB SNAPまたはpickitなどが必要です。
ファームを最新にした場合開発環境も最新のものを使用する必要があります。
と思ったけれど逆に最新のものしか提供されていなかった、、

E220-900T22S

出荷時のデフォルト値が00 00 62 00 0F 03 00 00となっています。
はじめの2バイトがアドレスなので初期状態では0です。
今回は1:1の送受信を予定しているので片方は必ずアドレスを変更する必要があります。
トランスペアレント送信モードと固定送信モードがあり1:1の通信をするためには固定送信モードにする必要もあります。
送信側をアドレス1で受信側をアドレス2へ設定しておきます。

C0 00 06 00 01 62 00 0F 43
C0:書き込み命令
00:スタートアドレス
06:書き込みバイト数
0001:アドレス1番
62000F:その他設定
43:0x40が固定送信モード

e220.png

RBoardにプログラムを書き込んで設定します。

setup.rb
dev = UART.new( 2, rxd_pin:13, txd_pin:14, baud:9600 )
sleep(1) # E220の起動待ち
dev.write("\xC0\x00\x06\x00\x01\x62\x00\x0F\x43")

LM61CIZ

温度の値を電圧値で返してくれる扱いやすい温度センサーです。
0℃の時0.6Vの出力でマイナスの温度だったとしてもプラスの電圧値で出力してくれます。

(出力電圧[V] - 基準電圧[V]) * 100倍 = 温度[℃]
(0.85[V] - 0.6[V]) * 100 = 25℃
(0.35[V] - 0.6[V]) * 100 = -25℃

LM61.png

temp.rb
# 0番はLEDにつながってるので本当はお勧めしない
adc = ADC.new( 0 )
while true
  v = adc.read()
  temp = (v - 0.6) * 100
  puts(temp)
  sleep(1)
end

温度の送信プログラム

temp.rb
adc = ADC.new( 0 )
dev = UART.new( 2, rxd_pin:13, txd_pin:14, baud:9600 )
sleep(1) # E220の起動待ち

while true
  v = adc.read()
  temp = (v - 0.6) * 100.0
  puts(temp)
  
  send_txt = "\x00\x02\x0F"
  bcd = sprintf("%04d", (temp * 100).to_i)
  send_txt << bcd[0,2].to_i(16) << bcd[2,2].to_i(16)
  dev.write(send_txt)
  
  sleep(60)
end

受信側プログラム

今回ラズベリーパイを使用したのでたまたまライブラリがそろっていたpythonで書きます。
Rubyで書くとは、、
たぶんラップトップやデスクトップのPCでも同じコードで動くと思います。

main.py
import serial
import binascii
from serial.tools import list_ports

ports = list_ports.comports()
devices = [ info.device for info in ports ]
print( devices )
ser = serial.Serial(devices[0], 9600)

while( True ):
  byte_txt = ser.read( 2 )
  txt = binacii.hexlify( byte_txt )
  data_txt = txt.decode("ASCII")
  print("temp : " + data_txt[0:2] + "." + data_txt[2:4])

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?