Giter Site home page Giter Site logo

byungwoo733 / tutorial-python-sdksample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from artikcloud/tutorial-python-sdksample

0.0 2.0 0.0 2.41 MB

Starter code using ARTIK Cloud Python SDK sends and receives a message.

License: Apache License 2.0

Python 100.00%

tutorial-python-sdksample's Introduction

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:

  1. Sign into My ARTIK Cloud
  2. 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).
  3. 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.
  4. Install ARTIK Cloud Python SDK using pip:
pip install artikcloud
  1. 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:

GitHub Logo

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.

tutorial-python-sdksample's People

Contributors

jonathanpaek avatar yujingwu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.