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
.
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.
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
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
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:
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)