Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get scrambled message body data from Azure Event Hub. Was following official tutorial to get data using console app

I was following this article from Microsoft to try to consume event data from Event Hub.

I have a esp32 sending telemetry message every 10 second to Azure IOT hub, IOT hub will then route the message to Event hub and messages sit there for me to retrieve from my console application.

The code on ESP32 looks like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        float temperature = (float)random(0, 50);
        float humidity = (float)random(0, 1000) / 10;
        snprintf(messagePayload, MESSAGE_MAX_LEN, messageData, DEVICE_ID, messageCount++, temperature, humidity);

        //// Below are the important part, I tried sending messagePayload, or a simple hello word string.
        EVENT_INSTANCE *message = Esp32MQTTClient_Event_Generate(/*"HelloWorld"*/ messagePayload, MESSAGE);
        Esp32MQTTClient_Event_AddProp(message, "temperatureAlert", "true");
        Esp32MQTTClient_SendEventInstance(message);

Above procedure seems work fine to me as I can see data being transmitted to IOT hub in Azure CLI.

I can see data flow in Azure CLI


But when I copy the code from official tutorial to consume the telemetry data in console app.

Actual problem happens.

Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));

Yes I get bunch of strings that looks somehow like the data I just sent from Esp32.

The full data I got

But why do I get:

"body":"eyJkZXZpY2VJZCI6ImVzcDMyIiwgIm1lc3NhZ2VJZCI6MSwgIlRlbXBlcmF0dXJlIjoxOS4wMDAwMDAsICJIdW1pZGl0eSI6MzAuNzk5OTk5fQ=="}

Instead of

"body":"{\"deviceId\":\"esp32\", \"messageId\":8, \"Temperature\":3.000000, \"Humidity\":21.500000}"

The data seems scrambled, it actually somehow reflect to the data that I sent from Esp32,
Example:
1. If I simply send a "Hello", I will receive "body":"SGVsbG8="
2. If I send "HelloWorld", I will receive "body":"SGVsbG9Xb3JsZA=="


Things I have tried:
1.Change the data string I am sending, so I found out the scrambled data is reflecting actual data.

2.Try to send data to Event hub using console app and the retrieve it(just like the official doc), I would get what I sent perfectly, but I eventually need to sent data from my Esp32 right..


I am pretty new to everything, please give me some hint, keyword, basically anything will help.

Also please feel free to point out any mistake in this post, I am thinking I may misunderstand a lot of terms or I am not asking question properly.

I will try to reply to any comment with some latency.
(I am quiet busy most of the day and I live in UTC+8 time zone so I might be sleeping haha.)

Thank you for your help in advance!

>Solution :

The data you are getting back is Base64 encoded. All you will need to do is decode this data and you will see the actual string data.

You would do something like:

var text = System.Text.Encoding.UTF8.GetText(System.ConvertFromBase64String("base64-encoded-string"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading