'Can't receive c2d messages from Azure Function App(Node)
I am building an IoT system using Azure IoT Hub, 2 ESP32 devices(one is to send sensor values and another is to receive c2d message and activate an actuator) and an Azure Function to store the values to a database.
To send c2d messages from an Azure function app, I used the following code.(This Azure Function is triggered when IoT Hub gets a mqtt message from my ESP32 device.) But I couldn't receive them on my mosquitto_subscribe script running on my laptop.
code in the function app
import { AzureFunction, Context } from "@azure/functions"
const Client = require('azure-iothub').Client;
const Message = require('azure-iot-common').Message;
const connectionString = 'my connection string';
const targetDevice = 'my device id';
const serviceClient = Client.fromConnectionString(connectionString);
const IoTHubTrigger: AzureFunction = async function (context: Context, message: {type:string;isPressed:boolean;}): Promise<void> {
context.log(`Eventhub trigger function called for message : ${JSON.stringify(message)}`);
context.log(message)
context.log(message.isPressed)
if(message.isPressed){
try {
await serviceClient.open()
context.log('Service client connected');
// serviceClient.getFeedbackReceiver(receiveFeedback);
const csdMessage = new Message('This is a C2D message');
csdMessage.ack = 'full';
csdMessage.messageId = "My Message ID";
context.log('Sending message: ' + csdMessage.getData());
await serviceClient.send(targetDevice, csdMessage);
}catch(err){
context.log('Could not connect: ' + err.message);
}
}
context.done();
};
export default IoTHubTrigger;
mosquitto code
mosquitto_sub -d -q 1 \
-V mqttv311 \
-p 8883 \
-h my-iot-hub-name.azure-devices.net \
-i esp32\
-u "my-iot-hub-name.azure-devices.net/my-decice-name/api-version=2016-11-14" \
-P "$SAS" \
-t "devices/my-device-name/messages/devicebound/#"
According to the execution log, the connection between IoT Hub and the Function was successfully established and the message was sent. execution log
What confuses me is that the mosquitto script can receive c2d messages when I run the local node script which is almost the same as the one in Function App. I wrote the following script referring to this document. https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-node-node-c2d#send-a-cloud-to-device-message
'use strict';
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var connectionString = 'my connection string';
var targetDevice = 'my device id';
var serviceClient = Client.fromConnectionString(connectionString);
(async()=>{
try {
await serviceClient.open()
console.log('Service client connected');
// serviceClient.getFeedbackReceiver(receiveFeedback);
const csdMessage = new Message('This is a C2D message');
csdMessage.ack = 'full';
csdMessage.messageId = "My Message ID";
console.log('Sending message: ' + csdMessage.getData());
await serviceClient.send(targetDevice, csdMessage);
}catch(err){
console.log('Could not connect: ' + err.message);
}
})();
Why can't c2d messages reach mosquitto_sub only when they are from function app even though the function to send a message is successfully executed ?
Can anyone help me?
Thank you in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
