-1

I am trying to update a string (MESSAGE) with a variable that is updated each time a message is sent (msg_no)

I have poked around a bit and it looks like the msg_no variable is being updated the way I would like but the string (MESSAGE) is not being updated (it shows up as 0 each time). I'm not sure why, thanks for any help.

import xbee
import time

msg_no = 0

# TODO: replace with the 64-bit address of your target device.
TARGET_64BIT_ADDR = b'\x00\x13\xA2\x00\x41\xB7\x6C\x8C'
MESSAGE = "Hello from router (Msg No. %s)" % (msg_no)

print(" +---------------------------------------+")
print(" | XBee MicroPython Transmit Data Sample |")
print(" +---------------------------------------+\n")

print("Sending data to %s >> %s" % (''.join('{:02x}'.format(x).upper() for x in TARGET_64BIT_ADDR),
                                    MESSAGE))

try:
    msg_no += 1
    xbee.transmit(TARGET_64BIT_ADDR, MESSAGE)
    print("Data sent successfully")
except Exception as e:
    print("Transmit failure: %s" % str(e))

time.sleep(5)

try:
    msg_no += 1
    MESSAGE = MESSAGE
    xbee.transmit(TARGET_64BIT_ADDR, MESSAGE)
    print("Data sent successfully")
except Exception as e:
    print("Transmit failure: %s" % str(e))
tripleee
  • 175,061
  • 34
  • 275
  • 318

1 Answers1

1

MESSAGE = MESSAGE doesn't have any effect, what you can do is have a template for the message, and then use it whenever you want to create a message:

# put this line on top of the script
MESSAGE_TEMPLATE = "Hello from router (Msg No. %s)"

#some code

# use this whenever a message with number `msg_no` needs to be created
MESSAGE = MESSAGE_TEMPLATE % msg_no
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55