KRL Tips and Tricks
Using Postman to Simulate Wovyn Sensor Readings
Life can be made a little easier with this one trick.
When working with wovyn sensors, there have been times in the past where they become a bit tricky to setup, or they may fail to connect for whatever reason. Getting around this is as simple as using Postman to simulate whatever readings that you are trying to receive in your KRL ruleset. This article is meant to be a walkthrough of how to install, set up, and run Postman to simulate the temperature readings that you would normally get from a wovyn sensor device.
That being said, it is recommended that you still work with a wovyn sensor because it will allow you to work with a physical device, which can help you be a better programmer in general, especially if you plan on working, or are currently working with mini computers such as a raspberry pi or an arduino. But there may be times where you wish to continue working with your wovyn sensor when you are not at in the comfort of your own home, and in these cases it is nice to be able to use a simulation of what you would normally receive. If you have Postman already installed on your device then feel free to skip ahead to where we will talk about setting up the JSON body of the sensor reading.
Step One: Install Postman
The link to Postman is provided here. Follow the link, download it, and install it on your machine.
Step Two: Create a New Request
Now that you have Postman installed, you need to create a new request. If this is your first time opening Postman, you will be shown an empty launch screen. From here, navigate to the upper left corner and select the button labeled “New”→ “Request”. Then give the request a proper name, description, and assign it to a collection.
At this point you are ready to create the JSON body of the wovyn sensor. Before we move forward with the steps in Postman, let us look very briefly at the structure of a sensor reading from a wovyn device. When a wovyn device connects with a Pico, and it sends a reading to it, you can expect the JSON to look like this (or very similar):
{
"emitterGUID":"FakeGUID",
"eventDomain":"wovyn.emitter",
"eventName":"wovyn",
"genericThing":{
"typeId":"2.1.2",
"typeName":"new_temp_reading",
"healthPercent":56.89,
"heartbeatSeconds":10,
"data":{
"temperature":[
{
"name":"ambient temperature",
"transducerGUID":"FaketransducerGUID",
"units":"degrees",
"temperatureF":75.31,
"temperatureC":24.06
}
]
}
}
}
What you do with this JSON is up to you. However, it is recommended that you use this entire JSON body in your Postman simulation since you will eventually need to connect your Pico with an actual wovyn sensor. Only picking out bits and pieces of the JSON and developing your KRL ruleset with that could lead to annoying corrections that will need to be made further down the road. Might as well just avoid those now and use the entire body.
It is also assumed that you are at least somewhat familiar with writing KRL rulesets at this point. You will need to have a ruleset with a rule installed that will receive the event that Postman will send to your Pico. Below, you can see what the most basic of rules would accomplish this.
Of course at this point, what you do with your ruleset is completely up to you. For demonstration purposes, I only set up the rule to receive a sensor reading where there is an event attribute called “genericThing”, which every wovyn sensor reading should have. From there, I send a directive with the attribute that I bound to a variable called sensor_generics
. Odds are that you will need to do more with the reading than that which is in the ruleset above, but figuring out how to parse the JSON and use the data will be left up to you.
Step Three: Understand Sky Event API
In order to now send an event from Postman to your Pico, it helps to be familiar witht the Sky Event API. There is a great link located here where you can learn more about it. For our purposes, we really only need to understand the structure of the URL request that we will send. To send the event we need the following:
- Port of the localhost that your Pico Engine is being hosted on
- The ECI of the Pico that has the sensor reading ruleset installed on it
- Arbitrary string to act as the event identifier
- The domain and type of the rule to raise
The arbitrary string is something that you will create in Postman, but when working with the physical wovyn sensor, this will be taken care of automatically. Once you have these three things, you just now need to insert them in the following URL structure:
http://localhost:<PORT>/sky/event/<ECI>/<EID>/wovyn/new_temp_reading
Step Four: Send The Event
Now that we have basically everything all set up and ready to go, let’s go back to Postman and send the event to the Pico and examine our results.
To accomplish this, we need to go into the new request that we made in Postman and customize three things.
- First, make sure that you are creating a
POST
event. This is done by selecting that option in the dropdown menu that is located directly below the name of the request. The defualt value isGET
. - Type in your sky event URL that will send the event to your Pico. Remember that everything is case sensitive and needs to be correct in order to work correctly.
- Below the space where you just provided the URL, select “Body” → “raw”. In that same menu, instead of having the input be parsed as text, select the dropdown menu and choose JSON. In the blank space that is then provided, type out (or copy and paste) the sensor reading info that was listed in step two.
After sending the event, the results are what I am sending back in a directive. Again, what you choose to do with this data is completely up to you, and you can extract whatever you need from it.
To make sure that your event is being sent correctly from Postman to your Pico, you can also check that Pico’s logging tab for any output that you have in your ruleset. In this example, I am simply using the klog
operator to see what the value of sensor_generics
is after the prelude block in my rule.
Conclusion
Throughout this article, we have seen how useful Postman can be in order to help us simulate a wovyn sensor reading. There are many more uses to Postman that you may be able to experiment with when experimenting event calls between Picos on your engine, but the main point of this article was to help you realize that even though developing in KRL may be somewhat new to you, you can interact through API calls much the same way you would with any other language.
Keep on keeping on.