this question is a continuation of the previous one on "How to read a string from a file and advertise as a beacon?". Link here: How to read a string from a file and advertise as a beacon?
Based on the solution shared there, I have come this far where a string stored in a file is being read and converted to bytes (for beacon advertising) and saved in a file. I tried the example given and things worked fine, I can advertise the message. However I can't advertise my own string (30 character long) and received an error message below. Where did I go wrong?
Output:
My code snippet for reading string and converting to bytes:
# ------------- for string processing ------------------ #
# Opening JSON file
string_path = r'/home/pi/Desktop/string.json'
with open(string_path) as f:
for jsonObj in f:
string = json.loads(jsonObj)
print("Opened string file: {}".format(string))
string_str = string['String']
print("String is:", string_str, type(string_str)) # string stored in variable uuid
#print(type(string_str))
# convert string_str string to bytes
byte_list = []
for byte in string_str:
byte_list.append(ord(byte))
print('String in bytes:', byte_list)
# assign a key to a list of byte values
byte_dict = dict()
byte_dict.setdefault('beacon_uuid', byte_list) # beacon_uuid is the name of a key
print("String in bytes dictionary:", byte_dict)
print(type(byte_dict))
# write byte_dict to json
# ref: https://stackabuse.com/reading-and-writing-lists-to-a-file-in-python/
with open('beacon_string.json', 'w') as filehandle:
#json.dump(byte_dict, filehandle)
filehandle.write(json.dumps(byte_dict))
print("String bytes dictionary in beacon_string.json file has been generated")
# access bytes in json file
here = Path(__file__).parent
data_file = here.joinpath('beacon_string.json')
with data_file.open() as f:
data = json.load(f)
#print('Json data:', data['beacon_uuid'], type(data['beacon_uuid']))
# --------------- End of string processing ---------------- #
and below is code snippet for advertising:
class TestAdvertisement(Advertisement):
# advertising an ibeacon
def __init__(self, bus, index):
company_id = 0x004C
beacon_type = [0x02, 0x15]
beacon_uuid = data['beacon_uuid']
#print(type(beacon_uuid[3]))
# uuid = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
# 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16]
# check = uuid[0]
# print("uuid data type is:", type(check))
major = [0x11, 0x22]
minor = [0x33, 0x44]
tx_power = [0xB3]
Advertisement.__init__(self, bus, index, 'peripheral')
self.add_manufacturer_data(company_id, beacon_type + beacon_uuid + major + minor + tx_power)
My string: {"String":"helloworldiamrighthere12367"}
help is much appreciated!
/home/pi/Desktop/string.json
as the error message is with trying to read that file.