Skip to main content

Tutorial: Send Data to Platform

This tutorial will show you how to use Device SDK to send data to the Platform.

Send Data schema

Requirements

  • PC or any other machine with either Linux or Windows.
  • One of the following:
  • If you are not registered to the Spotflow IoT Platform yet, Sign Up.
  • You need to have an existing Stream. A Stream called default-stream in Stream Group default-stream-group is created automatically after you register. Create a new one if you want to use a different Stream.
  • You need to have an existing Provisioning Token.

1. Send Data

Use the Provisioning Token to start a Provisioning Operation. After you approve it, the Device will finish the registration and start sending data to the Platform.

Install the recent Device SDK for Python using the following command:

pip install --upgrade spotflow-device

Paste the following code into a new file, or download it. Replace the placeholder <Your Provisioning Token> with your Provisioning Token and run the program.

tip

If you want to send data to a different stream you created earlier, replace also default-stream-group and default-stream with the desired Stream Group and Stream name.

get_started.py
import datetime
import json
import time
from spotflow_device import DeviceClient

# Connect to the Platform (starts Device Provisioning if the Device is not already registered)
client = DeviceClient.start(device_id="my-device", provisioning_token="<Your Provisioning Token>", db="spotflow.db")

# Create a sender to the default stream
sender = client.create_stream_sender(stream_group = "default-stream-group", stream = "default-stream")

# Your working code starts here. E.g. read data from your sensors and send it to the platform
for i in range(0, 60):
# Example: Send sample data to the platform:
payload = json.dumps({
"timestamp": datetime.datetime.now().astimezone().isoformat(),
"temperatureCelsius": 21 + (i * 0.05),
"humidityPercent": 50 + (i * 0.1)
})
sender.send_message(payload.encode())
print(payload)

# Pause till next iteration
time.sleep(5)

# Your working code ends here.

DeviceClient.start will display the Provisioning Operation ID and Verification Code:

Provisioning operation initialized, waiting for approval.
Operation ID: eb82df7f-63ab-429f-80df-f0ae05f57306
Verification Code: 3cwz1g1c

After you approve the operation in the next step, the Device will start sending the data as short Messages.

2. Approve Device

While the code on the Device side is still running, you'll now approve the Provisioning Operation started in the previous step:

  1. Expand the link Devices in the left sidebar if it's not already expanded. Open the link Approvals.

  2. The list of Provisioning Operations opens. You should see a row with the Device ID my-device. Click Approve in the column Actions.

  3. The approval dialog window shows the Provisioning Operation details and allows you to override Device registration details. Check the Verification Code, leave the default values and click Approve.

  4. Good job! The Device disappeared from the table because its state changed to Approved, while the current filter shows only the Devices in the state Waiting for Approval.

Finally, if you look at the program output, you should see that the device is registered and started sending Messages:

Provisioning operation initialized, waiting for approval.
Operation ID: eb82df7f-63ab-429f-80df-f0ae05f57306
Verification Code: 3cwz1g1c
{"timestamp": "2024-04-04T08:51:34.351761+02:00", "temperatureCelsius": 21.0, "humidityPercent": 50.0}
{"timestamp": "2024-04-04T08:51:39.358295+02:00", "temperatureCelsius": 21.05, "humidityPercent": 50.1}
{"timestamp": "2024-04-04T08:51:44.370000+02:00", "temperatureCelsius": 21.1, "humidityPercent": 50.2}
...
tip

The Device SDK documentation provides more information about Python SDK and C SDK.

3. See Registered Device

When the Device SDK completes the registration, the Device appears in the table of all registered Devices.

  1. Navigate to the list of registered Devices by clicking the link Devices in the left sidebar.

  2. You should see a row in the table with the Device ID my-device. If you don't see it, the Device hasn't completed the registration yet. Ensure that the code on the Device side finished the registration and proceeded to send Messages, and click the refresh button after a few seconds. Click the Device ID my-device to see the details.

  3. The Device Details page is useful to inspect the Device state. Device Connection shows whether the Device is currently connected to the Platform. In our case, the status will be Connected when the program is sending Messages and Disconnected otherwise. Scroll down to see Statistics.

  4. Statistics show the recent traffic from the Device. There might be a lag of several seconds before the Portal shows the current information, so refresh the section as needed.

What's Next