Creating a Device Simulator using REST API

Hi,

Is it possible to create a device simulator using REST API for the peripheral devices. I created a workflow to post a random number to the peripheral device as below

image .

So instead of this workflow I want to code it in python using REST APIs . Please clarify if this can be done using REST API provided by losant.

@Shiny_Josephine,

Hi! Welcome to the Losant Forums :slight_smile:

Just so I understand your question:

Option 1: Are you trying to write your own python script that simulates data and sends the data to Losant over HTTP?

If this is the case, I recommend using Webhooks. In your Python script, you can POST data to a webhook. That can trigger a workflow, and you’ll be able to use that data in your Device State Node.

Option 2: Or, are you trying to reach out to a REST API that simulates data and use that data in a Workflow?

If this is the case, you can use the HTTP Node to request your API and use that data in your workflow.

Let me know if this helps. I’m happy to answer more questions :slight_smile:

Instead of reinventing the wheel, you can check how existing simulators work with Losant, eg.

or

Do you have any python examples showing how to post data to a Losant Webhook? I have been able to cobble togther code to send data via Google Pub Sub and modified the code in Losant’s help docs to send data to Losants MQTT broker, but now need to do same with a Webhook. thx

Hi @David_MacKenzie!

I worked a bit on this last night, and wrote a small python script that makes a post request to a Losant Webhook. It uses 1 python library, requests and is only a few lines:

import requests

url = "https://triggers.losant.com/webhooks/DzD3lTgmjBpZBoQ9NyEo3KyqdqmMlSK0-2miHilOaA1$"
headers = {'Content-Type': 'application/json'}

# this is the data that we'll pass in the BODY of the request, it goes in the data field
request_data = {"data": "Hello, Losant!"}

r = requests.post(url, data=request_data) 

print(r) #prints the response from Losant in the console.

Where the webhook url can be found in the top right corner of the webhook configuration screen:

I also made a webook workflow that only has a debug node on it so I can see the request body being put in the payload:

Here is the requests library documentation if you need more information on the library I used.

Wow, you folks really go above and beyond. I was just hoping you had a sample handy. This is great. Thank you!

Hi,

I have a example below (python)

!pip install losant-rest

from losantrest import Client
client = Client()

creds = {

    'deviceId': '...',                                               # o ID é o ID do seu Device criado na Plataforma

    'key': '...',                                        # insira aqui a Access Key

    'secret': '...'          # insira aqui a Access Secret

}



response = client.auth.authenticate_device(credentials=creds)
client.auth_token = response['token']
app_id = response['applicationId']

state = {'data': {'Temperatura': '13' }}

response = client.device.send_state(deviceId='....', applicationId=app_id, deviceState=state)

print(response)