Help with sending serial via UART to Xbee

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mkingdom
Posts: 9
Joined: Sun Apr 30, 2017 2:25 pm

Help with sending serial via UART to Xbee

Post by mkingdom » Sat Mar 10, 2018 9:40 am

I need help with sending a 20 byte buffer to an Xbee via UART. The project has been tested and works using an Arduino.

The Coordinator Xbee is attached to a WiPy 3.0 via UART. I am able to read a 22 byte frame from a Router Xbee via the Coordinator using the following:

import pycom
from machine import UART
import struct
uart = UART(1, baudrate = 9600)
while True:
if uart.any() > 21:
getframe = uart.read(22)
frame = struct.unpack('22B', getframe)

What I cannot do is send a 20 byte frame to the Router Xbee via the Coordinator. This is due my lack of understanding of Micropython.

The Arduino code is:

int led = 13;

void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(led, HIGH); // turn LED on
setRemoteState(0x5);
delay(5000);
digitalWrite(led, LOW); // turn LED off
setRemoteState(0x4);
delay(5000);

}

void setRemoteState(char value) {
Serial.write(0x7E); // Start byte
Serial.write((byte)0x0); // Length MSB (always 0)
Serial.write(0x10); // Length LSB
Serial.write(0x17); // 0x17 is the frame ID for sending an AT command
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write((byte)0x0);
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFE); // Destination Network Serial.write(0xFE); // (Set to 0xFFFE if unknown)
Serial.write(0x02); // Set to 0x02 to apply these changes
Serial.write('D');
Serial.write('3');
Serial.write(value);
long sum = 0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'D' + '3' + value;
Serial.write(0xFF - (sum & 0xFF));

}

My first effort was to create a list of the bytes and to loop through them. e.g. [uart.write(item) for item in list]

The Rx light on the Coordinator flickers but the Tx (out to the Router) doesn't, which would indicate the data is being received but is not in the correct format to be onwards transmitted. My best guess is the data is not in the correct format for the Xbee

I'm sure there is an easy way to do this. Any help appreciated.

mkingdom
Posts: 9
Joined: Sun Apr 30, 2017 2:25 pm

Re: Help with sending serial via UART to Xbee

Post by mkingdom » Sun Mar 11, 2018 3:13 pm

All fixed with following code:

Code: Select all

import pycom
from machine import UART

import time

uart = UART(1, baudrate = 9600)
pycom.heartbeat(False)
pycom.rgbled(0x000000)

on_frame = [b'\x7E', b'\x00', b'\x10', b'\x17', b'\x00', b'\x00', b'\x00',b'\x00', b'\x00',
b'\x00', b'\x00', b'\xFF', b'\xFF', b'\xFF', b'\xFE', b'\x02', b'\x44', b'\x33',b'\x05', b'\x6F']

off_frame = [b'\x7E', b'\x00', b'\x10', b'\x17', b'\x00', b'\x00', b'\x00',b'\x00', b'\x00',
b'\x00', b'\x00', b'\xFF', b'\xFF', b'\xFF', b'\xFE', b'\x02', b'\x44', b'\x33',b'\x04', b'\x70']

while True:
    [uart.write(item) for item in on_frame]
    time.sleep(2)
    [uart.write(item) for item in off_frame]
    time.sleep(2)
This takes Xbee D3 high and low

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Help with sending serial via UART to Xbee

Post by pythoncoder » Thu Mar 15, 2018 10:19 am

Glad it works, but you might like to try this pproach

Code: Select all

on_frame = b'\x7E\x00\x10\x17\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFE\x02\x44\x33\x05\x6F'

# similar for off_frame

while True:
    uart.write(on_frame)
    # and so on
which is more 'Pythonic' and efficient.
Peter Hinch
Index to my micropython libraries.

mkingdom
Posts: 9
Joined: Sun Apr 30, 2017 2:25 pm

Re: Help with sending serial via UART to Xbee

Post by mkingdom » Thu Mar 15, 2018 3:39 pm

That's exactly the kind of help I need. Best practice!. Thanks Peter. :D

carver
Posts: 6
Joined: Tue Jan 16, 2018 5:16 pm

Re: Help with sending serial via UART to Xbee

Post by carver » Sun Mar 18, 2018 7:58 pm

How do you like the performance of your setup with XBee?
I'm curious because I'm looking for a medium that can transmit a bit further than wifi besides lora, the latter of which has some aspects that I don't care for as much.

mkingdom
Posts: 9
Joined: Sun Apr 30, 2017 2:25 pm

Re: Help with sending serial via UART to Xbee

Post by mkingdom » Tue Mar 20, 2018 9:23 am

They work fine for me and seem to be reliable. I haven't compared them against anything else though.

Post Reply