Receive reported updates from device twins from a backend app with mqtt

Senuda Jayalath 41 Reputation points
2020-11-28T04:40:49.837+00:00

I want to know how to receive reported messages sent by IOT devices to the cloud from my backend app . From my research I have found out the way to do it is with the function twin.get( ){ } . But I cant exactly figure out how to write that function. Please help me with his .

I want the code in javascript. Thanks in advance

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,214 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. QuantumCache 20,271 Reputation points
    2020-11-30T17:20:17.517+00:00

    Hello @Senuda Jayalath ,

    You may be interested in looking at this sample app.
    Download the sample Node.js project from https://github.com/Azure-Samples/azure-iot-samples-node/archive/master.zip and extract the ZIP archive.

    Make sure that port 8883 is open in your firewall. The device sample in this tutorial uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments.

    Please have a look at this section Process reported properties

    A back-end application accesses the current reported property values for a device through the device twin. The following snippet shows you how the back-end application reads the reported property values for the simulated device:

    // Display the reported properties from the device  
    function printReportedProperties(twin) {  
      console.log("Last received patch: " + twin.properties.reported.lastPatchReceivedId);  
      console.log("Firmware version: " + twin.properties.reported.firmwareVersion);  
      console.log("Fan status: " + twin.properties.reported.fanOn);  
      console.log("Min temperature set: " + twin.properties.reported.minTemperature);  
      console.log("Max temperature set: " + twin.properties.reported.maxTemperature);  
    }  
    

    Please let us know if you need further help in this matter.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.