How to build an alert system with a NodeMCU V3 and Altair SmartCore
- 0. Introduction
- 1. Project Schema
- 2. Lolin connections description
- 3. Altair SmartCore Project and Device registration
- 4. Data transferred from Lolin to Altair SmartCore API
- 5. Setting our Device on MQTT mode
- 6. Code implemented to send streams to Altair SmartCore API
- 7. Code implemented to send streams to Altair SmartCore MQTT Broker
- 8. Checking Data is collected in Altair SmartCore
- 9. Creating an Alert with Altair SmartCore Listeners
- 10. From here on: Enhancing the project
This easy tutorial helps you to build an Alert System for your home or office. In this case, we will use a NodeMcu v3 (aka Lolin) able to detect light and Altair SmartCore Engine to send alerts by email.
In this project you will learn how to:
- Connect a Lolin to Altair SmartCore to send data streams using Altair SmartCore HTTP REST API.
- Connect a Lolin to Altair SmartCore to send data streams using Altair SmartCore MQTT Broker.
- Build an Alert App on Altair SmartCore by programming a Listener to send an email using the SDK.
- 1 x NodeMcu v3
- 1 x Tiny Breadboard
- 1 x Resistance. Range between 200KOhm and 10KOhm
- 1 x LDR.
This project is a quick way to create an alarm app and understand the basics of combining Lolin and Altair SmartCore.
Lolin is programmed to send a data stream to Altair SmartCore starting whether or not receiving light.
All the data streams sent by Lolin are collected and stored in Altair SmartCore. Altair SmartCore is a huge database that collects all the information that your sensors send.
In addition to storing data, the true power of Altair SmartCore is to let you build Apps very quickly with few lines of Groovy code. In this scenario we are going to build a very simple Alert App that sends an email to you in case that Lolin detects light.
With Altair SmartCore you can build very complex Apps to support all the logic of your product on the cloud and integrate it with other devices and IT systems. But for now, let’s keep it simple.
Although this Alert App is very simplistic, you can use to detect someone enters in a dark room and turns on the lights
The connections in Lolin are extremely simple. You can see in the diagram below:
- Lolin connections
If you’re registered in Altair SmartCore, you have a default device already created for you. Check your control panel and see what it looks like. Basically you need the device id_developer that might be something like defaultDevice@myusername.myusername. But if you want, you can create a new device and use it in this example.
Now, go to your control panel “My account menu” and check your Apikey. ts a big alphanumeric token like:
From Lolin you have to build a HTTP request and send the data.
POST /streams HTTP/1.1
Apikey: YOUR APIKEY HERE
Content-Length: YOUR CONTENT LENGTH HERE
"device":"YOUR DEVICEs ID_DEVELOPER HERE",
“value”=”YOUR DATA HERE”,
Go to the Device List of Altair SmartCore Control Panel under "HIERARCHY" -> "Devices"
Edit the device that you want to use for this tutorial:
- Device list
Activate the MQTT option, fill the Mqtt Username, Password and add a Topic.
- Mqtt configuration
If you get here, your device must be sending streams when you turn on and turn off the lights.
Its time to test our control panel and check if we have new streams. Go to “Data management” -> “Data streams” and you will see your data.
- Streams reveived
A listener in Altair SmartCore can be associated to any hierarchy entity, from Project to Device. If you associate a listener to a project, all devices below the services of that project will be affected. If you associate a listener to service, all devices below the groups of that services will be affected. And so on.
In this example, we create a listener associated with our device. This listener waits for an event to occur in the context of the device and then evaluate the content.
To create a listener go to the control panel, then to “Rules” -> “Listeners” and click on the new button. Select "Device" as "entity type" and select your device on the "Id" field.
Fill the fields with the following values and save the new listener:
- Name: The name that you prefer
- Description: The description that you prefer
- Entity type: Device
- Entity id: Your id device
- Event to listen: data_persisted
- If expression:
- Then expression:
def email = new Email ();
email.to="YOUR EMAIL HERE";
- Then rule: leave empty
- Else expression:
def email = new Email ();
email.to="YOUR EMAIL HERE";
- Else rule: leave empty
- Be sure to have your listener enabled if you want it to be executed
Its time to test our listener. Send a data stream to this device, check your control panel and your email!!!
Now you know how to connect an Lolin device to send data to Altair SmartCore and how to create a simple App with a single Listener and some SDK programing in Groovy. But you can enhance this project in many ways:
- Try to add a motion sensor to the Lolin and send at least two values: light and motion status.
- Create your own control panel to check the status of the Lolin, by building an HTML page with a request to our API to ask for the status value of your device.
- Create another device that you can interact with (like a sound alarm) and build an app combining actions between the two devices through Altair SmartCore.