Examples¶
The following is a list of examples created with the existing relayr API.
demos/api_pulse.py¶
This script shows key entities of the current relayr API on the command-line. It does provide versioning information and makes a couple of API calls to list current counts of publishers, applications, public devices and more. A typical (slightly abbreviated) output will look something like this:
$ python demos/api_pulse.py
Relayr API Pulse
[...]
server status ok: True (0.42 s)
467 publishers (0.51 s)
267 applications (0.69 s)
328 public devices (0.74 s)
8 device models (0.38 s)
8 device model meanings (0.38 s)
#public devices by meaning: {
"acceleration": 57,
"temperature": 84,
"noise_level": 49,
"angular_speed": 57,
"color": 77,
"luminosity": 77,
"proximity": 77,
"humidity": 84
}
To determine the number of publishers e.g. it runs code similar to this (timing omitted here for brevity reasons):
from relayr import Api
api = Api()
publishers = list(api.get_public_publishers())
print('%d publishers' % len(publishers))
demos/noise.py¶
This script establishes a connection to a WunderBar noise level sensor device, checks the incoming noise level values for some time, compares them to a maximum threshold and sends a reminder, by email, to take action if needed.
Below you can see a version of that code stripped down to only a few lines (omitting the threshold comparison and email notification part):
import time
from relayr import Client
from relayr.dataconnection import MqttStream
c = Client(token='<my_token>')
dev = c.get_device(id='<dev_id>')
def mqtt_callback(topic, payload):
print('%s %s' % (topic, payload))
stream = MqttStream(mqtt_callback, [dev])
stream.start()
time.sleep(10)
stream.stop()
When running (with the correct token and device ID) this should print incoming MQTT messages from the noise level sensor with timestamps and sound levels like this (scroll right to see the actual values):
/v1/36fb3b0e-4abd-4598-9e0c-b4952cab4082 {"deviceId":"df088c31-1f12-4969-9b23-f7354b296367","modelId":"4f38b6c6-a8e9-4f93-91cd-2ac4064b7b5a","readings":[{"meaning":"noiseLevel","value":33,"recorded":1431440372993}],"received":1431440129390}
/v1/36fb3b0e-4abd-4598-9e0c-b4952cab4082 {"deviceId":"df088c31-1f12-4969-9b23-f7354b296367","modelId":"4f38b6c6-a8e9-4f93-91cd-2ac4064b7b5a","readings":[{"meaning":"noiseLevel","value":60,"recorded":1431440374973}],"received":1431440131539}
/v1/36fb3b0e-4abd-4598-9e0c-b4952cab4082 {"deviceId":"df088c31-1f12-4969-9b23-f7354b296367","modelId":"4f38b6c6-a8e9-4f93-91cd-2ac4064b7b5a","readings":[{"meaning":"noiseLevel","value":25,"recorded":1431440377173}],"received":1431440134005}
/v1/36fb3b0e-4abd-4598-9e0c-b4952cab4082 {"deviceId":"df088c31-1f12-4969-9b23-f7354b296367","modelId":"4f38b6c6-a8e9-4f93-91cd-2ac4064b7b5a","readings":[{"meaning":"noiseLevel","value":69,"recorded":1431440379373}],"received":1431440136160}
/v1/36fb3b0e-4abd-4598-9e0c-b4952cab4082 {"deviceId":"df088c31-1f12-4969-9b23-f7354b296367","modelId":"4f38b6c6-a8e9-4f93-91cd-2ac4064b7b5a","readings":[{"meaning":"noiseLevel","value":18,"recorded":1431440381572}],"received":1431440138298}