This tutorial will guide you through the process of connecting a device (a Lamp) to SmartWorks and sending data to store it and implement simple business logic.
This tutorial will guide you through the process of connecting your ESP8266 and DHT10 sensor to SmartWorks to control the board LED light and monitor the surrounding humidity.
Stream Processing is a method of tracking and analyzing streams of information of an event, and eventually obtaining useful structured
conclusions out of that raw information.
SmartWorksEdgeOps is a set of tools for building, maintaining and continuously improving code in resource constrained devices at the
edge of smart connected ecosystems.
This tutorial will guide you through the process of connecting a device (a Lamp) to SmartWorks and sending data to store it and implement simple business logic.
Functions provide the capability to execute custom business rules. For this project,
a Function will be created to update the state of the lamp based on the
temperature.
Add two new Properties to the Thing.
Figure 1.
Change to the Functions section to create your Function.
Figure 2.
Functions are divided into two main parts, called Workers and Triggers.
A Worker is the Function itself. It allows to create its custom-base function and
enables to subscribe to an “Event Trigger”.
Create the Function itself (Worker) using the following code. Complete the PATH
with your own values.
Note: Add your thing’s Client ID and Secret ID from the Interface tab inside the
Thing.
from swx.auth.token import get_token, revoke_token
import requests
import json
API_HOST = 'https://api.swx.altairone.com'
PATH = "/spaces/YourSpaceID/things/thingUID/properties"
CLIENT_ID = "ThingClientID"
CLIENT_SECRET = "ThingClientSecret"
def handle(req):
body = req.body.decode("utf-8")
body = json.loads(body)
temperature = body['temperature']
with get_token(CLIENT_ID, CLIENT_SECRET, ["thing.read","thing.update"]) as token:
if temperature >= 70:
status = "Warning"
else:
status = "Normal"
headers = {"Authorization": "Bearer " + token.access_token, "Prefer": "preview=2023.1"}
response = requests.request("PUT", API_HOST + PATH, headers=headers,json={"lamp-state": status})
return {
"body": response.json(),
"status_code": response.status_code
}
A Trigger is a component that is capable of invoking serverless Functions
from an event source. Triggers work as a listener to a particular endpoint,
capturing events and messages from different sources and redirecting them to one or
more Functions.
Create an MQTT trigger to subscribe to the topic entered as Event Trigger in
the worker.
Figure 3.
Note: MQTT credentials can be obtained by clicking Create
Credentials in the Interfaces tab of the Thing Menu, under
MQTT section. Figure 4.
Open the MQTT Inspector to track the messages received and check that the
function works as expected: