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

XBee通信のデータ読み込み

Posted at

XBeeを使った自宅状態遠隔見守りサービス実現方法の紹介
第7章 XBee通信のデータ読み込みについて解説します。

全体図

image

👆全体図はこんな感じです。

👇ここからの内容は下記記事を前提とします

プログラムの書き込み(Auduino IDE)

Arduino IDEの書き込み方法は5 . ESP32-WROVER-E開発ボードのツール設定と接続を参照して下さい。

void setup() {

    // USB接続シリアル接続を作成(コンソール出力用)
    Serial.begin(9600);

    // 25番ピンと26番ピンでシリアル接続を作成(XBee接続用)
    Serial1.begin(9600,SERIAL_8N1,25,26);
}

void loop() {
    
    // 10ms 待機
    delay(10);

    // シリアルに27バイト読み込みできて、先頭がスタートバイトの場合
    if ((Serial1.available()>22) && (Serial1.read() == 0x7E)){
        // skip offset 1 -> 4
        for (int i = 0; i < 3; i++) {
            int i_input = Serial1.read();
        }

        // 64-bit source address :offset 4 -> 12
        String source_address = "";
        for (int i = 0; i < 8; i++) {
            int i_input = Serial1.read();
            String hexString = String(i_input, HEX);
            if(hexString.length() == 1){
                source_address.concat("0");
            }
            source_address.concat(hexString);
        }
        Serial.print("source_address:");
        Serial.println(source_address);

        // 16-bit source address :offset 12 -> 14
        for (int i = 0; i < 2; i++) {
          int i_input = Serial1.read();
          String hexString = String(i_input, HEX);
        }
      
        // skip offset 14 -> 16
        for (int i = 0; i < 2; i++) {
          byte discarded = Serial1.read();
        }
      
        // Digital sample mask 16 -> 18
        for (int i = 0; i < 2; i++) {
          byte b_ret = Serial1.read();
        }
      
        // Analog sample mask 18 -> 19
        for (int i = 0; i < 1; i++) {
          byte b_ret = Serial1.read();
        }
      
        // Digital sample 19 -> 20
        int digital_1 = Serial1.read();
        int digital_2 = Serial1.read();
        int digital_val = (digital_1 * 256) + digital_2;
        int pin_value = bitRead(digital_val, 1);
        Serial.print("pin_value:");
        Serial.println(pin_value);
      
        // Analog sample mask 21 -> 22
        int Analog_1_1 = Serial1.read();
        int Analog_1_2 = Serial1.read();
        int Analog_val_1 = (Analog_1_1 * 256) + Analog_1_2;
        Serial.print("Analog_val_1:");
        Serial.println(Analog_val_1);

    }

}
image

👆こんな感じでSerial Monitorにデータが出力されます。
※ シリアルの通信速度は9600を指定して下さい。

プログラムの書き込み(MicroPython)

MicroPythonの書き込み方法は5 . ESP32-WROVER-E開発ボードのツール設定と接続を参照して下さい。

import time
from machine import UART

# create serial connect (port-25,26)
uart1 = UART(1, baudrate=9600,bits=8, parity=None, stop=1, rx=25, tx=26)

while True:
    time.sleep(0.1)
    # check start byte
    if(uart1.any()>22 and uart1.read(1).hex()=='7e'):

        # skip offset 3 byte
        uart1.read(3)

        # 64-bit source address
        source_address = uart1.read(8).hex()
        print("source_address:" + source_address)

        # 16-bit source address
        uart1.read(2).hex()

        # skip offset 2 byte
        uart1.read(2)

        # Digital sample mask 2byte
        uart1.read(2)

        # Analog sample mask 1byte
        uart1.read(1)

        # Digital sample 2byte
        b_digital_value = uart1.read(2)
        digital_value_bin = bin(int.from_bytes(b_digital_value))
        print("digital_value:" + digital_value_bin[2:3])

        # Analog sample 2byte
        b_analog_value = uart1.read(2)
        analog_value = int.from_bytes(b_analog_value)
        print("analog_value:" + str(analog_value))

やっぱりPythonの方が書きやすいな・・

image

👆こんな感じでShellのところにデータが出力されます。

<<(前の記事) 6 . ESP32-WROVER-E開発ボードとXBeeの接続

👇関連記事

👇参考URL

本記事へのリンク

image

https://docs.saurus12.com/device/xbee_communication

[keywords]
ESP32 ESP32-DevKitC-VE ESP32-WROVER-E開発ボード XBee Micropython

7 . XBee通信のデータ読み込み

更新日:2025年06月06日

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?