Azure JulyOT — Pt. 2 — Trigger warning: Configuring and triggering the tweeting bot
[Note: this is the second part of a two-part guide to create a system that uses a RaspberryPi with a temperature sensor to tweet once the temperature is above a specific level. The first part had some issues so it’ll be published soon.]
Since the IoT hub is now set up to send the temperature events to Azure, it’s time to configure it to send the events over to a Logic Apps app that will do things with the event.
Overview of what we’ll do:
- Create a new Event Grid System Topic
- [Optional advanced: Create a Service Bus if you’ll be using the same event in multiple ways — docs]
- Create a Logic Apps app that will listen for the event coming from the IoT Hub
- Create an event on the IoT Hub that will be sent every time the Raspberry Pi sends temperature data
- Fine-tune the Logic App to parse the event and do things with it (like post to Twitter)
Create a new Event Grid System Topic
Before we start wiring up the events, we need to create an Event Grid System Topic. This was new to me (and I struggled to figure this out). Looking at the docs, it seems like this was previously hidden (implicit), but is now exposed to allow fine-grained control when you have many many services, endpoints, messages, etc — since this is the only thing I’m doing in my subscription, it’s just an additional step to do.
- Search the portal for Event Grid System Topics.
2. Create a new topic.
3. In the Topic Type, select Azure IoT Hub Accounts, and fill in the details for the IoT Hub account.
4. In the name, I typed something I could remember:
Optional advanced: Create a Service Bus if you’ll be using the event in multiple parallel ways or with more complex logic
If you want to enable more powerful message routing capabilities so that a single message (event) can go to a Storage Account, Logic Apps, SQL, or other, you can create a Service Bus that provides queues, routing, etc.
This is out of scope for our small project.
Create a Logic Apps app that will listen for the event coming from the IoT Hub
We’ll create a new Logic Apps app that will let us create our bot that will tweet whenever some condition is met, similar to Chloe Condon‘s Shania bot (go ahead, try and impress them, I’ll wait).
- Create a new Logic App Consumption app. If you already have a VM set up or some specific infrastructure to use logic apps, Standard could be for you. In our case, I don’t want to deal with infrastructure, and just have an app that runs on demand (consumption) without caring for VMs, Docker containers, or Storage.
2. Click on designer to get started with the first steps:
3. Add a new step and search for When a HTTP request is received
4. Save the app and copy the URL that is generated in this step. This URL will point to the Logic App, so we’ll create an event in the IoT Hub that pings this URL with some data. We’ll come back to the Logic App later to do more.
Create an event on the IoT Hub that will be sent every time the Raspberry Pi sends temperature data
- In your IoT Hub, click on Events, and then create a new Event Subscription:
2. Add a name to the event, and in the Topic Details enter the topic we defined earlier:
3. In the event types, select Device Telemetry. The other options are handy if you care about when devices are connected/disconnected/etc. For our scope, we only care about telemetry events that are sent regularly (which include temperature readings, and other details).
4. For the endpoint details, select Web Hook since every time the event happens, we’ll send this to a URL that the Logic App will pick up.
5. In the URL endpoint enter the URL for the Logic App you created, so that the event gets sent to this URL.
Fine-tune the Logic App to parse the event and do things with it (such as posting to Twitter)
Now that we have an event being sent to the Logic App, let’s fine tune the app to ensure we can unpack the message in the event and do something (Tweet) when certain conditions are met.
- Make sure that the IoT hub sends a couple of events / messages to the Logic App. If you wrote the code, you’re already familiar with the JSON structure, but in this case, I’m taking the scenic (lazy) route.
- Open the Logic App we created earlier and click on one of the runs to open it (whether successful or failed):
3. On the run details, click on show raw outputs and copy the entire message
4. Navigate back to the Logic Apps designer and open the blueprint of the step so we update the details, then click on the step we created earlier, and click on Use sample payload to generate schema
5. Paste the entire message that we received before, so that the contents of the message are parsed on this step.
6. Add a new step to Parse JSON and use Dynamic content to get the previous step’s Body:
7. In the content, I removed the initial part of the message (headers) and just left the body from the previous step. To simplify this, I used Use Sample Payload to generate schema:
[Warning — I had to do a bit of trial & error for this because I didn’t understand what was going on]
8. Create a new step Control -> Condition. The idea is to do an If, else depending on the data we get from the sensor.
9. Add dynamic data on the control step to use Body, making sure that you’re using the variable from the Parse JSON step (instead of the one from the HTTP Request).
[Warning — you’ll notice that your If/else control becomes a for each. I believe this is because the JSON string might have multiple entries for Body. This took me a while to understand]
10. Inside, create another condition — at this point we will get the temperature variable and set the threshold at 29 degrees celsius:
[Warning — the same thing will happen here… the control condition will turn into a for each]
11. On the True condition, add a new action to Post a Tweet, and configure the connection to the Twitter account you wish to use.
[Warning — I struggled to get this to work using Firefox and ad blockers, so had to use Chrome or Safari to get the account to properly log-in.]
12. In the Post a Tweet step, use the dynamic variables for temperature and time to show the actual temperature (shown below is some additional text).
13. Save the app to publish the changes and check if the runs are successfully going through:
14. Check Twitter to validate that the tweets are being pushed through:
We’ve connected the messages sent from the RaspberryPi to an IoT hub, and then fired events to a Logic App endpoint that tweets when the right conditions are met (temperature above than 29 degrees Celsius). Some tweaks to this could be:
- Tweet when the temperature is outside a specific range
- Email (instead of tweeting) when the temperature is at a given range/level.
Some other ideas that can be explored using other sensors include humidity, pressure, light, motion, or even around the home using Hall-effect sensors when a door or window is opened.