How to read out message content of the EventGridEvent object that is passed from IotHub

Hsieh, Todd T 91 Reputation points
2020-10-11T17:42:16.767+00:00

I have an IoT device that sends a message to its IotHub like this in Python:

device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
 ...
message = "Message sent from my device"
await device_client.send_message(message)

In the IoTHub, there is an event grid subscription that is an event-grid triggered function app. In the function app, I am trying to get the message out from the event but doesn't seem the message "Message sent from my device" can be located?

def main(event: func.EventGridEvent):
    ...
    received_message = {"id": event.id, "message": str(event.get_json()), "}

I also tried event.topic, event.subject and they seem not the message that sending from the device. If uses event.get_json()["body"], it seems it is an encrypted message like VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBiZWluZyBzZW50IGZyb20gVG9kZCdzIGRldmljZQ==. What's a good way to decrypt?

Also, if the message sent from a device is a json format, how to retrieve it? My intention is to save the message/json into a cosmos db with some process in the function app.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,678 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,157 questions
Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
354 questions
{count} vote

Accepted answer
  1. QuantumCache 20,261 Reputation points
    2020-10-12T18:51:28.3+00:00

    Hello @Hsieh, Todd T ,

    The best way is to decode the base64 string. For the example provided in your question, I can see the below-decoded message.

    VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBiZWluZyBzZW50IGZyb20gVG9kZCdzIGRldmljZQ==
    "This is a message that is being sent from Todd's device"

    import base64

    base64_message = 'VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBiZWluZyBzZW50IGZyb20gVG9kZCdzIGRldmljZQ=='
    base64_bytes = base64_message.encode('ascii')
    message_bytes = base64.b64decode(base64_bytes)
    message = message_bytes.decode('ascii')

    print(message)

    See also the examples in C# to get an idea here:

    The below screengrab shows that my Function is triggered and the Telemetry data is in base64 encoded format, i need to decode the 'body' content to read the actual message/telemetry.

    31792-image.png

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 31,211 Reputation points MVP
    2020-10-12T18:41:08.07+00:00

    Hello @Hsieh, Todd T , the 'encrypted' message 'VGhpcyBpcyBhIG1lc3NhZ2UgdGhhdCBpcyBiZWluZyBzZW50IGZyb20gVG9kZCdzIGRldmljZQ==' is just a base64 encoded string which contains 'This is a message that is being sent from Todd's device'.

    Base64 is just a binary-to-text encoding scheme that represents binary data in an ASCII string format. No data is lost in this encode/decode process.

    Check it out for yourself with any online base64 decoder.

    So all you need to do is decoding the message from base64 to a text string.

    I recommend using Microsoft Azure SDKs code.

    Once you have the text string which is a JSON text, you could try to convert JSON to an object.

    Please mark an answer as "accepted answer" if it provides the answer to your question to help other community members searching for the same question.

    2 people found this answer helpful.