Working with Default HTTP Decoder on API Devices and Code Examples
How to use default API device decoder and custom JSON. Includes code examples.
Last updated
How to use default API device decoder and custom JSON. Includes code examples.
Last updated
This documentation provides a step-by-step guide on how to set up a Datacake API device, explaining the default decoder functionality and how to send JSON data to Datacake.
Introduction
Creating a New API Device
Understanding the Default Decoder
Sending JSON Data
Configuring and Viewing Device Data
Adding and Using New Fields
Creating Dashboards
Welcome to the Datacake API Device setup guide. This guide will help you quickly and easily ingest your JSON style data into Datacake using the API integration. The default API device on Datacake contains a decoder that allows sending a generic JSON structure.
Log in to Datacake:
Access your Datacake account. If you are new, sign up for an account.
Add a New Device:
Click on Add Device.
Select Device Type:
Choose API from the device type options. This device supports both MQTT and HTTP. For this guide, we will use HTTP.
Create a New Product:
If you don't have any existing products, select New Product.
Name your product (e.g., "Simon's API Demo Product").
Click Next.
Add a Device:
Add one device (e.g., "Simon's API Demo Device 01").
Click Next.
Select Plan:
Choose the Free Plan (supports up to 5 devices).
Click Add One Device to finalize.
The default API device in Datacake contains a default decoder, which allows you to send JSON data in the following structure:
The default decoder recognizes this structure and populates the Suggested Fields section where users can click to create the devices. This functionality is dynamic, meaning new keys added to the JSON will show up in the Suggested Fields.
Retrieve the API Endpoint URL:
Go to the device configuration and locate the HTTP Payload Decoder section.
Copy the provided URL for sending data.
Prepare JSON Data:
Use the JSON structure provided above.
Replace the "device"
value with the serial number of your device (e.g., "Simon's API Demo Device 01").
Send Data:
Use a tool like ReqBin to simulate API requests.
Set the method to POST.
Paste the API URL and the JSON data.
Send the request.
Access Device Configuration:
Click on the device name in the list.
Go to the Configuration tab.
View Logs:
In the HTTP Payload Decoder section, click Show Logs to see the JSON data received.
Create Suggested Fields:
Scroll to the Fields section.
Create fields for each key in the JSON structure by clicking on them and confirming the suggested type.
Add New Data Keys:
Update your JSON data with new keys (e.g., "ambient_light": 500).
Send Updated JSON:
Send the updated JSON data to the API endpoint.
Create New Suggested Fields:
Go to the device configuration and view the new suggested fields.
Create fields for the new keys as described earlier.
Access the Dashboard:
Go to the Dashboard tab for your device.
Enter Edit Mode:
Click on Enter Edit Mode.
Add Widgets:
Click on Add Widget.
Select the type of widget (e.g., Value Widget).
Choose the data field (e.g., temperature).
Customize the widget and save.
Add Device Fields Widget:
Add a Device Fields Widget to display all fields for debugging purposes.
You have now successfully set up a Datacake API device, configured it to receive JSON data, and created a dashboard to visualize the data. This guide provides a foundation for further customization, such as setting up rules, sharing dashboards, and adding more devices to your product. Happy monitoring!
Here are some code examples for Python, JavaScript, C#, and Arduino:
Feel free to merge these examples into the relevant sections of your documentation.