Skip to content

Instantly share code, notes, and snippets.

@h-sakano
Last active November 21, 2017 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-sakano/8f8093b19eef3b281677f8d61ac09bfe to your computer and use it in GitHub Desktop.
Save h-sakano/8f8093b19eef3b281677f8d61ac09bfe to your computer and use it in GitHub Desktop.
ArduinoとRaspberry Pi間をZigBeeで接続する ref: https://qiita.com/h-sakano/items/15e1b269d3ceb03cf499
# ベースのアップデート
$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get dist-upgrade
# 必要なライブラリのインストール
$ sudo pip install xbee
#include <XBee.h>
// Initialize XBee ZB client
XBee xbee = XBee();
// Payload byte array
uint8_t payload[16];
// Send to coordinator
XBeeAddress64 addr64 = XBeeAddress64(0, 0);
void set_int_to_payload(int value, int index) {
uint8_t *value_array;
value_array = reinterpret_cast<uint8_t*>(&value);
for(int i=0; i<sizeof(value); i++){
payload[i+index] = value_array[i];
}
}
void setup() {
Serial.begin(9600);
xbee.setSerial(Serial);
}
void loop() {
delay(3000);
int tid = 13; // 端末ID(テキトー)
// 今回はA0から光抵抗センサの値を読み取った
int cds = analogRead(0);
set_int_to_payload(tid, 0);
set_int_to_payload(cds, 8);
// Create request for XBee ZB
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
xbee.send(zbTx);
}
$ python receive.py
>> Waiting for data...
(13, 404)
(13, 404)
(13, 407)
(13, 403)
(13, 406)
(13, 404)
.
.
.
# coding: utf-8
from xbee import ZigBee
import serial
import struct
import time
def parseData(data):
data = struct.unpack('qq', data['rf_data'])
return data
if __name__ == '__main__':
# Setup ZigBee
PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600
myDevice = serial.Serial(PORT, BAUD_RATE)
xbee = ZigBee(myDevice)
# Continuously read and print packets
print(">> Waiting for data...")
while True:
try:
data = parseData(xbee.wait_read_frame())
tid = data[0]
cds = data[1]
print(tid, cds)
except KeyboardInterrupt:
break
myDevice.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment