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

Advertisements

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

        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"));

Leave a ReplyCancel reply