Azure / azure-iot-arduino

Azure IoT library for the Arduino
Other
168 stars 95 forks source link

Sending not only one message to IoT hub #47

Closed programmersommer closed 7 years ago

programmersommer commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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