Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to get OneNet data in Python

Shulou Source: shulou.com Published: 2022-06-02 06:05:40 10月04日 Update

It is believed that many inexperienced people have no idea about how to obtain OneNet data in Python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Use python script files to upload and obtain OneNet server data

Upload data

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-2-24 23 virtual: LiShan# @ Email: lishan_1997@126.com# @ File: OneNet_Post.py# @ Note: https://blog.csdn.net/lishan132/article/details/114044902import urllib.requestimport json# device ID, KeydeviceId = "591972034" APIKey = "ROsfgvwqy2jxn2x93TdDCCbFmL8=" # upload function def OneNet_post_data (info): url = "https://api.heclouds.com/devices/" + deviceId +'/ datapoints' streams = [] for index Element in enumerate (info): streams.append ({"id": element [0], "datapoints": [{"value": element [1]}]}) values = {"datastreams": streams} data = json.dumps (values) .encode ("utf-8") request = urllib.request.Request (url, data) request.add_header ('api-key') APIKey) request.get_method = lambda: 'POST' request = urllib.request.urlopen (request) print (json.loads (request.read ()) if _ _ name__ = =' _ main__': upload_data = [["road1", 10], ["road2", 20], ["road3", 30], ["road4", 40], ["road5", 50] ["road6", 60], ["road7", 70], ["road8", 80], ["road9", 90], ["road10", 100], [road11 ", 110], [" road12 ", 120],] OneNet_post_data (upload_data)

Get data

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-2-24 23 virtual: LiShan# @ Email: lishan_1997@126.com# @ File: OneNet_Get.py# @ Note: https://blog.csdn.net/lishan132/article/details/114044902import urllib.requestimport json# device ID, KeydeviceId = "591972034" APIKey = "ROsfgvwqy2jxn2x93TdDCCbFmL8=" # get function def OneNet_get_data (): url = "http://api.heclouds.com/devices/" + deviceId +" / datastreams "request = urllib.request.Request (url) request.add_header ('api-key' APIKey) request.get_method = lambda: 'GET' request = urllib.request.urlopen (request) r = json.loads (request.read ()) data = r.pop (' data') print (r) return dataif _ _ name__ ='_ _ main__': load_data = OneNet_get_data () print ('parameter' +'\ t\ t'+ 'update time' + '\ t\ t +' numerical') for index Element in enumerate (load_data): a = str (element.get ('update_at',')) b = str (element.get ('current_value',')) if a! = "and b!": print (str (element ['id']) +'\ t\ t\ t'+ a +'\ t\ t\ t'+ b)

Integrate into one file

#! / usr/bin/env python#-*-coding: utf-8-*-# pip install prettytableimport urllib.request as reqimport json# device ID, KeydeviceId = "591972034" APIKey = "ROsfgvwqy2jxn2x93TdDCCbFmL8=" # upload function def OneNet_post_data (info): url = "https://api.heclouds.com/devices/" + info [0] +'/ datapoints' headers = {'api-key': info [1]} streams = [] for I E in enumerate (info [2]): streams.append ({"id": e [0], "datapoints": [{"value": e [1]}}) data = json.dumps ({"datastreams": streams}). Encode ("utf-8") request = json.loads (req.urlopen (req.Request (url, data) Headers=headers). Read () print (request) # get function def OneNet_get_data (info): url = "http://api.heclouds.com/devices/" + info [0] +" / datastreams "headers= {'api-key': info [1]} request = json.loads (req.urlopen (req.Request (url) Read () data = request.pop ('data') print (request) return dataif _ _ name__ =' _ _ main__': # ready to upload data upload_data = [["road1", 10], ["road2", 20], ["road3", 30], ["road4", 40], ["road5", 50], ["road6" 60], ["road7", 70], ["road8", 80], ["road9", 90], ["road10", 100], ["road11", 110], [road12 ", 120],] # upload data OneNet_post_data ([deviceId, APIKey, upload_data]) # get data get_data = OneNet_get_data ([deviceId") APIKey]) # noinspection PyBroadException try: # use tables to beautify display data import prettytable as pt tb = pt.PrettyTable () tb.field_names = ["id", "update_at", "current_value"] for index, element in enumerate (get_data): ID = str (element.get ('id') '') update_at = str (element.get ('update_at',')) current_value = str (element.get ('current_value',')) tb.add_row ([ID, update_at, current_value]) print (tb) except Exception: # Direct display data for index Element in enumerate (get_data): ID = str (element.get ('id',')) update_at = str (element.get ('update_at',')) current_value = str (element.get ('current_value',')) print (ID, update_at, current_value)

After reading the above, have you mastered the method of how to get OneNet data in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Data utf-8 function device content file method more problem helpless for this reason reason parameter this skill numerical value time server article experience Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple MariaDB vpn Shulou Information