Using the Sensor Emulator API

In this code example, we will create a Python console application that will

  1. Create an Emulated Touch Sensor
  2. Create one emulated Touch Event for that sensor every second

Service Account

In order to follow this example, you must have created a Service Account and key as described in the code example and noted down the Key Secret for a Key ID, this is needed later. Please note that you need to change the role of the Service Account to project administrator to be able to create an emulated device. 


To test try out this code example you need to have Python 3 installed.

Create a new folder for this example open a command-line console in this folder, run the following 3 lines:

python3 -m venv venv
source venv/bin/activate
pip3 install requests

This will

  1. setup a standard Python 3 virtual environment (or venv),
  2. activate this environment and finally
  3. install the required packages via the built-in Python package manager.

Keep this console window open as we will use it to run the example in a moment.


Using your favorite editor, create a new file called with the following content:

import requests
import json
import pprint
import time
from datetime import datetime
import urllib

# Fill in from the Service Account and Project:
username="SERVICE_ACCOUNT_KEY"  # this is the key
password="SERVICE_ACCOUNT_SECRET" # this is the secret

# Configure URLs, JSON POST data and default names
codeExampleSensorDisplayName="Code Example Touch Sensor"
createEmulatedSensorJSON = {
  "type": "touch",
  "labels": {
    "name": codeExampleSensorDisplayName,
    "virtual-sensor": ""
publishEmulatedTouchJSON = {
  "touch": {
    "touch": {

def getCodeExampleDeviceName(displayName):
  # URL encode label=value
  urlParamFormatNameLabel = urllib.parse.quote("name={}".format(codeExampleSensorDisplayName))
  # Get devices with Label name=displayName
  device_listing = requests.get(apiDeviceUrl + "?label_filters=" + urlParamFormatNameLabel, auth=(username,password))
  # If found, return full resource name, else False
  devices = device_listing.json()['devices']
  if len(devices) > 0:
    return devices[0]["name"]
  return False

### Run code-example
def main():
  # Check if code-example Sensor already exists
  name = getCodeExampleDeviceName(codeExampleSensorDisplayName)
  if not name:
    # If not, create it
    print("Creating code-example Touch Sensor '{}'".format(codeExampleSensorDisplayName))
    device =, data=json.dumps(createEmulatedSensorJSON), auth=(username,password))
    name = device.json()["name"]
    print("Found already existing code-example Touch Sensor '{}'".format(codeExampleSensorDisplayName))

  print("Direct link to Sensor in Studio: {}".format(studioUrlBase + "/" + name))

  print("Starting to generate one emulated TouchEvent per second... (press CTRL-C to abort)")
  while True:
    print("{} - Touching Sensor".format(
    r = + "/{}:publish".format(name), data=json.dumps(publishEmulatedTouchJSON), auth=(username,password))

if __name__ == "__main__":

Next, open up the Service Account tab in Studio and replace

  2. SERVICE_ACCOUNT_SECRET with the Key Secret you saved when you created the Key
  3. PROJECT_ID with the part of the URL in the browser between ;/projects/ and /serviceaccounts/..., e.g. b8rsrqnh2nqg00c0s9bg

Using the console you used to set up the development environment, run:


Example output

You should now see the console outputting one new line per second each representing a sent Touch Event.

To see the events coming in, follow the link to the sensor in Studio that is also part of the code example output.

Found already existing code-example Touch Sensor 'Code Example Touch Sensor'

Direct link to Sensor in Studio:

Starting to generate one emulated TouchEvent per second:
2018-02-23 11:28:14.077600 - Touching Sensor
2018-02-23 11:28:15.300588 - Touching Sensor
2018-02-23 11:28:16.523049 - Touching Sensor
2018-02-23 11:28:17.740090 - Touching Sensor
2018-02-23 11:28:18.990783 - Touching Sensor
2018-02-23 11:28:20.209312 - Touching Sensor
2018-02-23 11:28:21.432176 - Touching Sensor
2018-02-23 11:28:22.685985 - Touching Sensor

Clean up

A new emulated Touch Sensor was created as part of the code example.

To delete it you have two options.

Delete via Emulator UI

To do this:

  1. Go to Studio
  2. Open Manage Projects
  3. Click on the Project you used for the code example
  5. Click the left arrow until you find the sensor named “Code Example Touch Sensor”
  6. Press the DELETE button in the lower right corner
  7. Confirm with DELETE SENSOR

Delete via ´curl´

See the curl article on how to get started with curl if you have not used it before.

In the following command,


Replace PATH_TO_DEVICE with the path after as output by the code example (e.g. projects/b7s3e550fee000ba5dhg/devices/emuba81hbekkdh000cmid20).

Then replace SERVICE_ACCOUNT_KEYwith the KEY ID and SERVICE_ACCOUNT_SECRETwith the Key Secret you saved when you created the Key. In the last two you should be able to copy-paste from the code example.