Onenet platform HTTP escalation and acquisition device data

Source: Internet
Author: User
Tags python script
Onenet platform HTTP escalation and acquisition device data

@date: 2017-03-24 Advance Preparation

Recently looking at some related things networking platform, mobile Onenet platform has become the first choice, which open interface is relatively clear, although now the function is relatively simple, equipment access, data escalation and acquisition, triggering applications and real-time display of data flow functions.
-Access to Onenet platform http://open.iot.10086.cn/, access to the developer center in the upper right corner, register users and login
-Create a product based on the document center http://open.iot.10086.cn/doc/art243.html#66
-Create a device
-Create a data stream
-Prepare a machine that can access the extranet and install the python2.7 version on the machine, mainly to simulate the behavior of the device using the Python script debugging

Pre-prepared steps can be done according to the developer documentation, debugging purpose is to verify that HTTP devices can access the platform, escalate and obtain relevant data. Come on, take a look at the version of Python, 2 and 3 API usage is very different.
HTTP, the data content is in JSON format, regardless of the escalation or acquisition. escalation Data

No, on the code.

Import urllib2
Import JSON import time from time
import sleep

device_id= ' 50479xx '
api_key= ' Mndiaxj=1nzdr57x8owelki0qis= '

#添加代理
def http_proxy_handle ():
    Proxy_handler = urllib2. Proxyhandler ({' http ': ' 10.10.100.188:3328 '})
    opener = Urllib2.build_opener (proxy_handler)
    Urllib2.install _opener (opener)
    return

def http_put_data (data):
    url= ' http://api.heclouds.com/devices/' +device_id + '/datapoints '
    d = time.strftime ('%y-%m-%dt%h:%m:%s ')
    values={' datastreams ': [{' id ': ' Temperature ', '] DataPoints ": [{" At ":d," value ":d ATA}]}]}
    jdata = Json.dumps (values)                  # JSON-formatted encoded print for data
    jdata
    request = Urllib2. Request (URL, jdata)
    request.add_header (' Api-key ', api_key)
    Request.get_method = lambda: ' POST '           Set HTTP access method
    request = Urllib2.urlopen (request) return
    Request.read ()

http_proxy_handle ()
RSP = Http_put_data ()
print RSP

Implementation results are as follows

{"errno": 0, "error": "SUCC"}

Here you can view the reported data in the data display of the device in the Platform Web interface. Get Data

Get data is the device from the platform to obtain the reported data, the code is as follows

# Get Sensor data
def http_get_data ():
        url= ' http://api.heclouds.com/devices/' +device_id+
        '/datapoints ' Request = Urllib2. Request (URL)
        request.add_header (' Api-key ', api_key)
        Request.get_method = lambda: ' Get '           # set how HTTP is accessed
        request = Urllib2.urlopen (request) return
        Request.read ()

Implementation results are as follows

{"errno": 0, "data": {"Count": 2, "datastreams": [{
                        "DataPoints": [{"At": "2017-03-24 12:24:50.000",
                "Value": [}], "id": "img"}, {
                        "DataPoints": [{"At": "2017-03-24 13:36:34.000",
    "Value": [}], "id": "Temperature"}] }, "error": "SUCC"} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.