LoginSignup
0
0

More than 3 years have passed since last update.

mrubyでm2x

Last updated at Posted at 2019-07-30

AT&TのIoTプラットフォームのM2Xをmruby on YABMで試してみました。M2XはThingSpeakに比べてちょっと複雑です。Machinistはどっちからかというこっちに似てる気がします。

レジストしてデバイスを登録しただけではデーターが放り込めません。

準備としてデバイスのページの"API Cheat Sheet"ボタンを押して表示される"Creating or Updating a stream"のcurlコマンドを実行してstreamを作る必要がありました。(ページの"Ovarview"の"Add Stream"ボタンからも作れるようです。)

streamを作ったら下記のスクリプトでデータが放り込めました。M2X用のmrbgemsもありますが、mruby on YABMには依存がなかったりするので自力での実行です。

#
# mruby on YABM script
#
# m2x update script
#

DEVICE_ID = "naisyo"
PRIMARY_API_KEY = "naisyo"

STREAM_ID = "temperature"
INTERVAL = 10

def delay(yabm, val) 
  start = yabm.count() 
  while yabm.count() < start + val do
  end
end

begin

yabm = YABM.new

# net setup

yabm.netstartdhcp

yabm.print yabm.getaddress + "\n"

count = 0

while 1 do
  yabm.print "."
  count = count + 1
  yabm.print " " + count.to_s

  body='{ "value": ' + count.to_s + ' }'

  header = Hash.new
  header.store('User-Agent', "test-agent")
  header.store('X-M2X-KEY', PRIMARY_API_KEY)
  header.store('Content-Type', "application/json")
  header.store('Body', body)
  path = "/v2/devices/" + DEVICE_ID + "/streams/" + STREAM_ID + "/value"
  res = SimpleHttp.new("http", "api-m2x.att.com", 80).put(path, header)
  if res 
    yabm.print " " + res.status.to_s
  end
  yabm.print "\n"
  delay(yabm, INTERVAL * 1000)
end

rescue => e
  yabm.print e.to_s
end

スクリーンショット 2019-07-30 9.18.28.png

httpsでも同じようにアクセスできるみたいですが、BearSSL 0.6と相性が悪いみたいです。。。

0
0
0

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
0
0