def sending(): while True: # relay.receive() -> Receives the message from the client relay_frame = relay.receive() # Stay in loop until an actual message is received while relay_frame is None: relay_frame = relay.receive() time.sleep(0.25) if relay_frame is not None: # Send acknowledgment message back to client relay.send(relay.BLUETOOTH, "OK") # Don't change this line. This is how it knows who to send the message to. sender = relay_frame["sender"] # Alter 'message' to send the desired payload file = open("example.txt", "r") lines = file.readlines() message = "" for line in lines: message = message + line message = message + "@@@" # Send the message and start reading relay frames again. relay.send(sender, message) else: # No connection between XBee and Android print("no connection") time.sleep(30)
def demo_handle_api_frames(): ''' Provides loopback functionality. Receives a User Data Relay API frame from the serial interface, adds some data to it, then sends it back to the sender. How to run this demo: 1) In XCTU's MicroPython Terminal, first put the XBee into MicroPython mode (+++-wait-ATAP4). Then press Ctrl+f, paste in the code, and compile it into /flash/main.mpy. Then press Ctrl+d to soft reboot and run the code. (Make sure there is no /flash/main.py file or that would get executed instead.) Alternatively, copy this code into /flash/main.py using XCTU's File System Manager tool (and then you can press the reset pushbutton to restart the system and run the new code). Then put the device back into API Mode with Escapes (+++-wait-ATAP2) so that it is ready to receive the data. Close the serial port in XCTU when done so that you can reopen the port with gecko.py. 2) In another terminal, run api_frames_loopback_test() defined in gecko.py. That script will open the serial port using PySerial and send a User Data Relay message to MicroPython. The code below receives that message, appends to it, and then sends it back to the sender. Notes: - You could add the "+++-wait-ATAP4" commands here to streamline changing modes for testing. - You can also set up a loopback example within MicroPython. relay.send(relay.MICROPYTHON, b"blah") and then call relay.receive(). ''' print("Launching API frames demo.") from machine import Pin import time from xbee import relay led = Pin( "D5", Pin.OUT, value=0 ) # DIO5 = XBee3 Cellular "Associated Indicator" LED, which is Pin 15 of the through-hole package. while True: rx = relay.receive() if rx is not None: dest = rx['sender'] data = rx['message'] + b'world' relay.send(dest, data) time.sleep_ms(500) led.toggle()
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import time from xbee import relay INTERFACES = {0x00: "Serial Port", 0x01: "Bluetooth", 0x02: "MicroPython"} print(" +--------------------------------------+") print(" | XBee MicroPython Relay Frames Sample |") print(" +--------------------------------------+\n") while True: # Start reading relay frames. relay_frame = relay.receive() while relay_frame is None: relay_frame = relay.receive() time.sleep(0.25) # Get the relay frame parameters. sender = relay_frame["sender"] message = relay_frame["message"].decode("utf-8") # Send back the answer and start reading relay frames again. relay.send(sender, "New message from %s: %s" % (INTERFACES[sender], message))