1 Answer
- Newest
- Most votes
- Most comments
2
Hi AWS-currydem,
from what I can see your code only publishes messages. To receive messages you need to subscribe to a topic. You are also using the old version of the AWS IoT Device SDK.
You can try the current AWS IoT Device SDK v2 for Python. It includes also a pubsub example.
In the sample mentioned above the message is stored in a variable before it is published. This would address your use-case to store the message in a variable.
Cheers,
Philipp
Relevant content
- asked 4 years ago
- AWS OFFICIALUpdated a month ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year ago
I took your advice and it worked like a charm. for the second part of the question I'm still confused.
When I publish a message I can see it in my python program, but I still am not sure on how to save my specific message into a string variable
for example If I publish
{ "message": "Blue" }
I receive in my program
b'{\n "message": "Blue"\n}'
How do I save the "Blue" to a variable like "color"
color = Str(payload) color = "Blue"
Assuming the message is stored in the variable
message
:I tried using what you suggested and got this error message: "AttributeError: 'str' object has no attribute 'decode'"
Upon further investigation of the pubsub.py file I theorized that the message is stored in the function "def on_message_received" and the message is stored in the "payload" variable
Callback when the subscribed topic receives a message
def on_message_received(topic, payload, dup, qos, retain, **kwargs): print("Received message from topic '{}': {}".format(topic, payload)) global received_count received_count += 1 if received_count == cmdUtils.get_command("count"): received_all_event.set()
I then tried to change the line to "color = json.loads(payload.decode())['message']" and got this error message at first: NameError: name 'payload' is not defined
I assumed this was caused because "payload" is a parameter of the function "def on_message_received" instead of a declared variable, so I then tried these ways of declaring "payload";
payload = "" #I then got this error message:"AttributeError: 'str' object has no attribute 'decode'"
payload = 0 #I then got this error message:"AttributeError: 'int' object has no attribute 'decode'"
I'm pretty sure that "payload" is where the messages received are stored but I'm still not sure on how to save it to a variable I appreciate your help and I apologize if I'm not catching on as fast, I did try to find tutorials on how to do what I need.
You are using a variable (payload) that is not defined in your code. Replace
payload
with the variable name that contains your message in bytes sequence format.Thank you so much for your help, it's almost working perfectly besides one error. I changed the "def on_message_received" function
Callback when the subscribed topic receives a message
def on_message_received(topic, payload, dup, qos, retain, **kwargs): print("Received message from topic '{}': {}".format(topic, payload)) global received_count received_count += 1 if received_count == cmdUtils.get_command("count"): received_all_event.set() color = json.loads(payload.decode())['message'] print(color)
This is the format of the message I am sending from the topic
{ "message": "blue" }
This allows me to save the received message to color and just prints blue. However, I also get this error message TypeError: string indices must be integers. And I know this is because there is supposed to be an integer in the brackets instead of the word 'message' but when I place either a 0 or a 1 the output, I get is either: A or 1
I also tried changing the function and just having color = json.loads(payload) But when I do this the output is {'message': 'blue'}
Is there a way to still get the output of print(color) as blue without getting the error message: TypeError: string indices must be integers?