Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Sending not only one message to IoT hub #47

Closed
programmersommer opened this issue Mar 26, 2017 · 2 comments
Closed

Sending not only one message to IoT hub #47

programmersommer opened this issue Mar 26, 2017 · 2 comments

Comments

@programmersommer
Copy link

programmersommer commented Mar 26, 2017

Hello!
I have tried SDK and simple sample with Genuino MKR1000.

Here are some additional steps that I have made:

  1. AzureIoTProtocol_HTTP was also required for compiling, so I have install it.

  2. It was also exception 'class Serial_' has no member named 'setDebugOutput'. So, I have commented line

        Serial.setDebugOutput(true);
    

After those steps message was send to IoT hub, but it was only one message.
As I think because inside loop there is simplesample_http_run(); - it should be send multiple messages. Right?

May I ask what does means this code?:

            /* wait for commands */
              while (1)
             {
                 IoTHubClient_LL_DoWork(iotHubClientHandle);
                 ThreadAPI_Sleep(1000);
              }

May be I should made corrections to simple sample code to be able send multiple messages?

@astaykov
Copy link

The code that actually sends a message to IoT Hub is couple of lines above the loop:

 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
                                    {
                                        printf("failed to hand over the message to IoTHubClient");
                                    }
                                    else
                                    {
                                        printf("IoTHubClient accepted the message for delivery\r\n");
                                    }

                                    IoTHubMessage_Destroy(messageHandle);

If you would like to send more messages, you have to edit your code and send messages within the loop.

Take a look at the slightly modified code here. I am sending a message in the loop:

int sendCycle = 15;
                        while (1)
                        {
                            if(currentCycle >= sendCycle) {
                                currentCycle = 0;
                                unsigned char*buffer;
                                size_t bufferSize;

                                LogInfo("Sending RelayStateOne %d, RelayStateTwo %d\r\n", relay->RelayOneState, relay->RelayTwoState);

                                if (SERIALIZE(&buffer, &bufferSize, relay->DeviceId, relay->RelayOneState, relay->RelayTwoState) != CODEFIRST_OK)
                                {
                                    LogInfo("Failed sending sensor value\r\n");
                                }
                                else
                                {
                                    sendMessage(iotHubClientHandle, buffer, bufferSize);
                                }
                            }

                            IoTHubClient_LL_DoWork(iotHubClientHandle);
                            ThreadAPI_Sleep(1000);
                            currentCycle++;
                        }

the helper variable is just to be not too aggresive on the sends.

@programmersommer
Copy link
Author

Thank you, @astaykov!
Your sample is much more simple and clear. IMHO should be included in library samples.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants