LoginSignup
3
8

More than 5 years have passed since last update.

IM920とArduinoを使って文字列を無線で送信する

Posted at

やること

インタープラン社の920MHz無線モジュールIM920とArduinoを使ってArduinoより文字列を無線で送信する。

ArduinoとIM920の接続

インタープラン社のハードウェア説明書より以下の図のように配線を行う。
スクリーンショット 2018-10-26 16.25.08.png
(https://www.interplan.co.jp/support/solution/IM315/manual/IM920_HW_manual.pdf より引用)

Arduino側のRXピンとIM920の7番ピン、Arduino側のTXピンとIM920の6番ピンを接続します。
IM920の17番・23番ピンをVCCに、18番・19番・24番ピンをGNDに接続します。
この接続により、ArduinoからソフトウェアシリアルにてIM920にコマンドを送信することにより、ペアリングした他のIM920へデータを送信することができます。

Arduinoのコード

受信RXピンを8番、送信TXピンを9番に割り当てました。
インタープラン社のソフトウェア説明書よりTXDAコマンドにて文字列の送信を行います。
以下のコードでは"12345"という文字列を送信し続けます。

arduino.ino
#include <Wire.h>
#include <SPI.h>
#include <SoftwareSerial.h>

SoftwareSerial IM920Serial(8, 9); //受信 RX をピン 8、送信 TX をピン 9 に割り当てます

void setup()
{
    IM920Serial.begin(19200); //ソフトウェアシリアル開始。IM920 とは 19200 ボーを指定

    Serial.begin(19200);    
    Wire.begin();
}

void loop()
{
    String str = "";
    str += "TXDA ";
    str += encodeAscii("12345");

    Serial.println(str);
    IM920Serial.println(str);

    delay(500);
}

String encodeAscii(String str){
  String ascii = "";
  int j=0;
  bool _dot_flag = false;
  for(int i=0; i<str.length(); i++){
    if(str.charAt(i) == '.' || _dot_flag){
      j++;
      _dot_flag = true;
    }
    ascii += asciiElement(str.charAt(i));
    if(j > 1) break;
  }
  return ascii;  
}

String asciiElement(char c){
  String s;
  if(c == ',')  s = "2c";
  if(c == '.')  s = "2e";
  if(c == '0')  s = "30";
  if(c == '1')  s = "31";
  if(c == '2')  s = "32";
  if(c == '3')  s = "33";
  if(c == '4')  s = "34";
  if(c == '5')  s = "35";
  if(c == '6')  s = "36";
  if(c == '7')  s = "37";
  if(c == '8')  s = "38";
  if(c == '9')  s = "39";

  return s;
}

連続使用する際には

IM920で一定間隔で文字列を送信するIoT的な使い方をする際にはデータ送信しないときにArduinoをスリープモードにした方が良いです。特に、各種センサ(気温・光量など)のデータを取得して送信するといった際には電力消費が激しくなるのでスリープモードの利用をおすすめします。

スリープの参考: https://deviceplus.jp/hobby/entry_056/

その他のコマンドについて

IM920の各種コマンドについて
"TXDA 12345"とすれば"12345"という文字列を発信できましたが、
インタープラン社のソフトウェア説明書にはより多くのコマンドが記載されていますのでご確認ください。

Arduinoでなく、IM920のみをスリープさせたい際には、
DSRX(スリープ開始)、ENRX(スリープ停止)コマンドの利用が可能です。

3
8
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
3
8