Giter Club home page Giter Club logo

mrubyc_for_wio_cell_lib's Introduction

mruby/c for Wio(3G, LTE, LTE M1/NB1)

About

mruby/c for Wio はSeeed社が開発した以下の基板上で mruby/c を動作させるためのライブラリです。

mruby/cでコードを書いたLED制御サンプル(クリックで再生)。 IMAGE ALT TEXT HERE

Environment

  • mruby-2.0.1
  • mrubyc-2.1

Arduino IDE側の準備

  1. Arduino IDEを起動し、 スケッチ -> ライブラリをインクルード -> ライブラリを管理 をクリックし、ライブラリマネージャを起動します。
  2. Wio 3G Wio LTE M1/NB1 の場合は、 Wio cell lib で検索し、 Wio cell lib for Arduino の最新版をインストールします。
  3. Wio LTE の場合は、 Wio LTE で検索し、 Wio LTE for Arduino の最新版をインストールします。

以下の3つのライブラリをインストールします。いずれもArduinoIDEからインストール可能ですが、IDEから入れるとコンパイルエラーが発生するためgithubからmasterブランチをcloneしてください.

Arduino Client for MQTT のインストール

  1. SeeedJP/pubsubclientのREADMEに従い、 Arduino Client for MQTT をインストールします。
  2. https://github.com/SeeedJP/pubsubclient.git

SeeedJP/pubsubclientをインストールします.

$ cd ~/Documents/Arduino/libraries/
# git clone https://github.com/SeeedJP/pubsubclient.git

Grove_SHT31_Temp_Humi_Sensor のインストール

Seeed-Studio/Grove_SHT31_Temp_Humi_Sensorをインストールします.

$ cd ~/Documents/Arduino/libraries/
# git clone https://github.com/Seeed-Studio/Grove_SHT31_Temp_Humi_Sensor.git

Seeed_SHT35 のインストール

Seeed-Studio/Seeed_SHT35をインストールします.

$ cd ~/Documents/Arduino/libraries/
# git clone https://github.com/Seeed-Studio/Seeed_SHT35.git

Grove_BMP280 のインストール

Seeed-Studio/Grove_BMP280をインストールします.

$ cd ~/Documents/Arduino/libraries/
# git clone [email protected]:Seeed-Studio/Grove_BMP280.git

Grove_BME680 のインストール

Seeed-Studio/Seeed_BME680をインストールします.

$ cd ~/Documents/Arduino/libraries/
# git clone [email protected]:Seeed-Studio/Seeed_BME680.git

Install

本リポジトリをcloneします.

$ git clone https://github.com/hiroeorz/mrubyc_for_Wio_cell_lib.git

リポジトリごとArduinoのライブラリディレクトリに保存します。

$ cp -r mrubyc_for_Wio_cell_lib  ~/Documents/Arduino/libraries/

あとは、スケッチの中で本ライブラリをインクルードします

  • スケッチ -> ライブラリのインクルード -> mruby/c for Wio cell lib を選択。

Document

基本クラス

Groveセンサー

Examples

examples ディレクトリ以下にサンプルを置いています。 各サンプルのディレクトリに移動し

$ make

とすると、rubyのソースからCのソースを吐き出します。 あとはArduino上でビルドし、 Wio3G , Wio LTE , Wio LTE M1/NB1 にアップロードして実行してください。

例として、Wio3Gに実装されているLEDを赤・緑・青で繰り返し光らせるためのRubyソースコードは以下のようになります。

wio = Wio.new
wio.power_supply_led(true)
rgb = [255, 0, 0]

while true
  last = rgb.pop
  rgb.unshift(last)
  wio.led_set_rgb(rgb[0], rgb[1], rgb[2])
  sleep 0.3
end
  • 初期状態で Wio <-> PC 間の通信スピードは 115200bps です。 puts などの出力を見る場合はシリアルモニタの速度を 115200bps に設定してください。
  • 通信速度を変更する場合は Wio クラスのインスタンスを生成する際に引数で通信速度を渡してください・
    • 例: wio = Wio.new(9600)

また、簡易なJSONパーサ、ジェネレータも持っています。

  {:di => [1, 2, 3], :ai => [20, 30, 40], :alert => 0, :flag => false}.to_json
  #=> "{\"di\":[1,2,3],\"ai\":[20,30,40],\"alert\":0,\"flag\":false}"

  JSON.parse('{"name": "hiroe", "age": 43, "weight": 70.2, "lover": null}')
  #=> {"name" => "hiroe", "age" => 43, "weight" => 70.2, "lover" => nil}

MQTTプロトコルを通じてSORACOM Beamに接続するmrubycコードは以下のように使います。

wio = Wio.new
wio.power_supply_cellular(true)
wio.turn_on_or_reset
sleep 1

wio.activate("soracom.io", "sora", "sora")
send_data = {:di => [1, 2, 3], :ai => [20, 30, 40], :alert => 0, :flag => false}

MQTTClient.open("beam.soracom.io", 1883, "") do |mqtt|
  mqtt.subscribe("test")
  mqtt.publish("test", send_data.to_json)

  while true
    data = mqtt.get_subscribed_data

    if data
      puts "recived! #{data["test"]}"
    end
    
    mqtt.wait_loop(1)
  end
end

ここで、このディレクトリ内で make を実行すると task.c が生成されます。 この task.c を読み込んで実行するArduinoスケッチは以下のようになります。

#include <libmrubyc.h>
#include "task.c"

extern const uint8_t code[];

#define MEMSIZE (1024*30)
static uint8_t mempool[MEMSIZE];

void setup() {
  delay(1000);

  mrbc_init(mempool, MEMSIZE);
  mrbc_define_wio_methods();

  if (NULL == mrbc_create_task(code, 0)) {
    SerialUSB.println("!!! mrbc_create_task error");
    return;
  }
 
  SerialUSB.println("--- running mruby/c ---");
}

void loop() {
  mrbc_run();
}

コンパイル・基板にアップロードして実行するとLEDが三色順番に発光します。

Thanks

本ライブラリを実装するにあたり、kishimaさんの以下の電子書籍やレポジトリ等を参考にさせていただいています。

Other

  • 本ライブラリはWio_cell_lib_for_ArduinoおよびWio LTE for Arduinoに依存します。
  • 本ライブラリはmruby/c 2.0のソースコードを含んでいます。
  • 作者は手元にWio3GとWioLTEしか持っていないため、Wio LTE N1/NB1での動作確認は行なっておりません。

License

mruby/c for Wio cell lib はBSD License(aka 3-clause license)のもとで配布いたします。

mrubyc_for_wio_cell_lib's People

Contributors

hiroeorz avatar

Watchers

 avatar James Cloos avatar  avatar

mrubyc_for_wio_cell_lib's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.