Real-Time Temperature Monitoring with Azure IoT & SMS Alerts Using Azure Communication Services
Explanation of the Sequence:
- IoT_Device sends temperature data (as JSON) to the IoT_Hub.
- The IoT_Hub triggers an event to Event_Grid when new telemetry data is received.
- Event_Grid passes the event data to the Azure_Function.
- Azure_Function processes the data to check if the temperature exceeds the defined threshold.
- If the temperature exceeds the threshold, the function proceeds to send an SMS.
- If the temperature is within the normal range, no SMS is sent.
- Azure_Function sends an SMS request to SMS_Client.
- SMS_Client communicates with ACS_SMS_API to send the SMS.
- ACS_SMS_API confirms the SMS has been sent, and the response is passed back to the SMS_Client and Azure_Function.
To set up an Azure IoT-based temperature sensing system with an Azure Function that sends an SMS when the temperature exceeds a certain threshold using Azure Communication Services (ACS), follow these steps from scratch:
1. Set Up Azure IoT Hub
Create an IoT Hub in the Azure portal:
- Go to the Azure portal → Create a resource → Search for IoT Hub → Create.
- Follow the setup steps (Subscription, Resource Group, Region, etc.).
- After the IoT Hub is created, get the Connection String from IoT Hub > Shared access policies > iothubowner.
Set up your IoT Device:
- Create a device in the IoT Hub by going to IoT Hub > Devices → Add Device.
- Note the Device ID and Primary Connection String.
2. Set Up Azure Communication Services (ACS) for SMS
- Go to the Azure portal → Create a resource → Search for Azure Communication Services.
- Create the ACS resource and note the Connection String.
- Get a phone number for sending SMS from the ACS portal (under Phone Numbers).
3. Create an Azure Function
- Go to the Azure portal → Create a resource → Search for Function App → Create.
- Select Python as the runtime stack and configure the rest of the settings.
- After deployment, go to Function App > Functions and create a new Function with the Event Grid trigger (which will be used to listen to IoT Hub messages).
4. Set Up Azure Function to Monitor IoT Device Telemetry
- Set up your Azure Function to trigger based on incoming telemetry from your IoT Hub. For this, you'll need to integrate with the Event Grid:
- In the Azure Function, you'll receive telemetry data from your IoT device.
5. Write Python Code for the Azure Function
Here's the Python code that will process the incoming temperature data, check if it exceeds a threshold, and send an SMS using ACS.
6. Deploy the Azure Function
- In the Azure portal, go to Function App > Functions > Add.
- Choose the Event Grid trigger and link it to your IoT Hub as the source for events.
- Deploy the function code from the portal or via your local development setup using Azure Functions Tools.
7. Connect IoT Hub and Event Grid to Trigger the Function
- In your IoT Hub, go to Events > Event Grid.
- Add an Event Grid subscription that triggers your Azure Function when a new event (telemetry data) is received.
- Set the function as the Event Handler.
8. Send Temperature Data from IoT Device
- On your IoT device, implement code to send temperature readings to the IoT Hub.
- Use MQTT or HTTPS protocol to send data like this:
9. Test the System
- Test the system by sending data from your IoT device. If the temperature exceeds the threshold, the Azure Function will send an SMS through ACS.

Comments
Post a Comment