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.
Help with sending serial via UART to Xbee
Re: Help with sending serial via UART to Xbee
All fixed with following code:
This takes Xbee D3 high and low
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)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Help with sending serial via UART to Xbee
Glad it works, but you might like to try this pproach
which is more 'Pythonic' and efficient.
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
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Help with sending serial via UART to Xbee
That's exactly the kind of help I need. Best practice!. Thanks Peter.
Re: Help with sending serial via UART to Xbee
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.
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.
Re: Help with sending serial via UART to Xbee
They work fine for me and seem to be reliable. I haven't compared them against anything else though.