How to Connect an Apple device to Altair SmartWorks and relay
- 0. Introduction
- 1. Project Schema
- 2. Raspberry Pi. Connections description
- 3. Altair SmartWorks Project and Device Registration
- 4. Data Transferred from Raspberry Pi to Altair SmartWorks
- 5. Code implemented on XCode
- 6. Checking Data is collected in Altair SmartWorks
- 7. Creating an Alert with Altair SmartWorks Listeners
- 8. From here on: Enhancing the project
0. Introduction
This tutorial extends the following one: "How to Connect a Raspberry to Altair SmartWorks and relay" where streams are sent to Altair SmartWorks using an API REST Client.
This time, we have an iPhone app using iOS and an application for Apple Watch using WatchOS.
Difficulty:
Ingredients
- 1 x Raspberry Pi. An alternative is to use an Arduino Uno
- 1 x Tiny Breadboard
- 2 x Resitance. Value of 330KΩ
- 1 x Led
- 1 x Transistor NPN
- 1 x Relay
- 1 x iPhonne
- 1 x Watch
1. Project Schema
This Project is a quick way to create an alarm app and understand the basics of combining Raspberry PI, Altair SmartWorks and Apple devices.
Overall diagram of the project
iOS and watchOS will be programmed using Swift to send a data stream to Altair SmartWorks and close or open the light circuit.
All the data streams sent by iPhone or Apple WATCH are collected and stored in Altair SmartWorks. Altair SmartWorks is a huge database that collects all the information that your sensors send.
In addition to storing data, the true power of Altair SmartWorks 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 Raspberry Pi closes the light circuit thanks to the streams sent by Aplle devices.
With Altair SmartWorks 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.
2. Connections
Connections detail
3. Altair SmartWorks Project and Device Registration
Device
If youre registered in Altair SmartWorks, 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.
But if you want, you can create a new device and use it in this example.
Apikey
Now, go to your control panel “My account menu” and check your Apikey.
Its a big alphanumeric token like:
98346673a6377ef1fde2357ebdcb0da582b150b00cabcd5a0d83045425407ab4.
You need this number to complete the example.
4. Data Transferred from Raspberry Pi to Altair SmartWorks
From Raspberry Pi you have to build a HTTP request and send the data.
-
HTTP request
POST /streams HTTP/1.1
Host: api.altairsmartcore.com
Accept: application/json
User-Agent: RaspberryPi-SmartCore
Content-Type: application/json
Apikey: YOUR APIKEY HERE
Content-Length: YOUR CONTENT LENGTH HERE
Connection: close -
Data
{
"protocol":"v2",
"device":"YOUR DEVICEs ID_DEVELOPER HERE",
"at":"now",
"data":{"light":"YOUR DATA HERE"}
}
5. Code implemented on XCode
This is the XCode code implemented in iOS and watchOS Pi. Note that some values should be replaced by your own values.
Code for send Stream. File Carriots.swiftNote: the Carriots.swift class can be reused, it's necessary to mark which targets are associated.
Reusing Carriots.swift
App preview
The App
App preview
The App
To consume requests from the API-Rest of Altair SmartWorks, you need to modify the Info.plist file with the values:
Info.plist values
This configuration is required for the iPhone and WATCH apps.
6. Checking Data is collected in Altair SmartWorks
To test our control panel and check if we have new streams go to “Data management” → “Data streams” and you will see your data.
Data Stream list
7. Creating an Alert with Altair SmartWorks Listeners
We have enhanced the listener creation process with our Flow Tool.
A listener in Altair SmartWorks 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.
Briefly, our listener will update the state of the device ("state" field of the properties list) with the reading of the last stream and send an email if this data is defined in the stream.
Fill the fields with the following values and continue to the next step.
- Name: The name that you prefer
- Description: The description that you prefer
- Entity type: Device
- Entity id: Your device id_developer (defaultDevice@example.example)
- Event to listen: "Event Data Persisted"
- Be sure to have your listener enabled if you want it to be executed
- Click on the "Next Step: design the code" button to visually design the listener functionality
Listener creation
In the Flow editor we'll need to follow these steps:
-
Step 1: Drag and drop in the editor workspace a "update device" node and Double-click it to configure it with the values:
Label: descriptive name of the node in the editor view. For example "Update Device State"
Id device: here you can explicitly type the Id developer of the device we want to update (for example defaultDevice@example.example) or, in a generic way, refer to the device associated with the stream that triggers the listener: context.envelope.device. In this case you must select "$ variable" as the field type.
Properties: click on the button "add" and add the propery we want to update: "state" -> "$context.data.light". Please note the "$" character to indicate the value is a variable, not a string.
Please note we only have to fill the data we want to update. The other data of the specified device will remain unchanged. Click on the button "Done" to apply the changes
Flow designer Step 1: device update
-
Step 2: Drag and drop in the editor workspace a "switch" node and Double-click it to configure it with the values:
Label: descriptive name of the node in the editor view. For example "If email specified"
If condition: select "if" and fill in the following field with the code context.data.email && context.data.email!=""
Click on the button "Done" to apply the changes
Flow designer Step 2: Switch node
-
Step 3: Drag and drop in the editor workspace a "send email" node and Double-click it to configure it with the values:
Label: descriptive name of the node in the editor view. For example "Send email"
To: [[Your Email Here]]
Subject: Select "$ variable" in the field type selector and type "Light "+context.data.light
Message: "Someone has turned "+context.data.light+" the light"
Click on the button "Done" to apply the changes - Step 4: Connect the node ports.
Flow designer Step 3 and 4: Node Send Email configuration and connections
- Step 5: Click on the "Save" button and check the listener data in the "Show" screen. You can have a look at the generated source code and use it later for debugging purposes.
Flow designer Step 5: Resulting listener + source code
It's time to test our listener. Send a data stream to this device, check your control panel and your email!!!
8. From here on: Enhancing the project
Now you know how to connect a Raspberry PI and Apple device to send data to Altair SmartWorks 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 control the electric energy of a room no matter where you are using app for iPad or macOS.
- Create a control panel to manage the above suggestion.