Hello @Jacob Krauth ,
you want to send a byte array to the IoT Hub and decode this byte array in Azure using an Azure Function.
This is fine :-)
Almost all examples are working with JSON, but did you notice the JSON inside the messages are first converted to a bytearray and then decoded in the cloud?
Or, did you notice the default Azure Function template for the IoT Hub exposes a string? Which is just the decoded body of incoming messages?
So, let's see how this can be fixed.
First, you send a message with just a byte array. In this blog post
string jsonData = "{ \"single\":true }";
using var message = new Message(Encoding.UTF8.GetBytes(jsonData));
You can just do:
using var message = new Message(yourByteArray));
From here, Azure will handle this message still well but all tooling, like the Azure IoT Explorer, expects JSON so that will not work well.
Now, the message is ingested and you want to transform it.
Here is an example of an Azure Function directly connected to the IoT Hub.
Note: The post is about not telemetry messages but that is ok. The incoming message type is still the same: 'EventData'.
As you can see, the type of the incoming parameter is not a string, it's that Event Data:
public static void Run(EventData myIoTHubMessage, ILogger log)
{
log.LogInformation($"C# IoT Hub trigger function processed a message: {myIoTHubMessage}");
var bodyText = Encoding.UTF8.GetString(myIoTHubMessage.Body);
In that example, the body of the message (now your byte array) could be encoded.
But all you need to do is:
var bodyByteArray = myIoTHubMessage.Body;
Now, it's up to you to do something fancy with the byte array.
Extra: I did something similar with compressing data. There, I alter the ContentType of the message. Perhaps you have to play with that value (like 'application/octet-stream').