ARTIK Cloud Python SDK starter code
This sample code sends and gets a message to and from ARTIK Cloud. It uses the ARTIK Cloud Python SDK to make REST API calls.
Prerequisites
- python 2.7+, 3.5
- pip or setuptools
Setup / Installation:
- Sign into My ARTIK Cloud
- On the device dashboard, click to connect a new device. Select the Demo Fire Sensor (from cloud.artik.sample.demofiresensor) and name your sensor SampleFireSensor (or any name you'd like).
- Click the Settings icon on the device you just added. Get the device ID and device token. If the token does not already exist, click "GENERATE DEVICE TOKEN…" to get one.
- Install ARTIK Cloud Python SDK using pip:
pip install artikcloud
- Add the device ID and device token obtained before to config.json file. The following is the example:
{
"device_id": "999aaabbbcccdddeeefffggg",
"device_token": "000111222333444555aaabbbccc"
}
Run the code
1. Send a message
Run the following command in your termainal, which will send a random temperature value to ARTIK Cloud on behalf of the device.
%> python app-send-message.py
If everything goes well, you will receive a response with a message id (mid). ARTIK Cloud uses this response to acknowledge the receipt of the message.
{"data": {"mid": "a674e9ec6f24495f845f96d704fd9473"}}
2. Get a message
Retrieve from ARTIK Cloud the last message sent by the device.
%> python app-get-last-normalized-messages.py
Below is the response. It has a 'temp' value of 183 that was sent earlier.
{'count': 1,
'data': [{'cts': 1474269704021,
'data': {u'temp': 183},
'mid': 'a674e9ec6f24495f845f96d704fd9473',
'mv': 1,
'sdid': '<Redacted>',
'sdtid': 'dtce45703593274ba0b4feedb83bc152d8',
'ts': 1474269704021,
'uid': '<Redacted>'}],
'end_date': None,
'next': None,
'order': None,
'sdid': None,
'sdids': '<Redacted>',
'size': 1,
'start_date': None,
'uid': None}
Peek into the implementation
Take a closer look at the following files:
- ./app-send-message.py
- ./app-get-last-normalized-messages.py
Import the artikcloud package:
import artikcloud
from artikcloud.rest import ApiException
Setup credentials for your api call. Here we have used the device token to make our API calls.
artikcloud.configuration = artikcloud.Configuration();
artikcloud.configuration.access_token = config['device_id']
The two methods send_message() and get_last_normalized_messages() are the part of the MessagesAPI. Lets create an instance of it first.
#instance for MessageAPI
api_instance = artikcloud.MessagesApi()
Method send_message() triggers POST a message REST call. Method get_last_normalized_messages() triggers Get the last message REST call
#send message
api_instance.send_message(data)
#retreive last message
api_instance.get_last_normalized_messages(count=count, sdids=sdids, field_presence=field_presence)
View your data in My ARTIK Cloud
Have you visited ARTIK Cloud data visualization tool?
Select your device from the charts to view your device data in realtime. Try running the ./app-send-message.py multiple times in your terminal to send a few random values. Here's a screenshot:
More examples
Peek into tests in ARTIK Cloud Python SDK for more SDK usage examples.
More about ARTIK Cloud
If you are not familiar with ARTIK Cloud, we have extensive documentation at https://developer.artik.cloud/documentation
The full ARTIK Cloud API specification can be found at https://developer.artik.cloud/documentation/api-reference/
Peek into advanced sample applications at https://developer.artik.cloud/documentation/samples/
To create and manage your services and devices on ARTIK Cloud, visit the Developer Dashboard at https://developer.artik.cloud
License and Copyright
Licensed under the Apache License. See LICENSE.
Copyright (c) 2016 Samsung Electronics Co., Ltd.