CoCalc Shared Files2019-12-22-210909.ipynbOpen in CoCalc with one click!
Author: Dmitrij Moreinis
Views : 60
In [22]:
import sys; sys.version.split()[0]
'3.6.9'
In [23]:
from oauthlib.oauth2 import BackendApplicationClient from requests_oauthlib import OAuth2Session YOUR_CLIENT_ID = "fTaXPSTExce3LMzut2eQZjwTm3y5f7s7" YOUR_CLIENT_SECRET = "lUCSV8P_9I5NKrcInhdKTih0tXskA9iYSyocityPQxB99X5QQVDGtQ6fI7-IxDoT" oauth_client = BackendApplicationClient(client_id=YOUR_CLIENT_ID) token_url = "https://login.arduino.cc/oauth/token" oauth = OAuth2Session(client=oauth_client) token = oauth.fetch_token( token_url=token_url, client_id=YOUR_CLIENT_ID, client_secret=YOUR_CLIENT_SECRET, audience="https://api2.arduino.cc/iot", ) print(token.get("access_token"))
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5rUkVSalJFTWpBNFFqTXdNMFl3TXpSRFF6SkZOa0kxT0RkQlFVVTJNelpCUkRReFJqTXlOZyJ9.eyJodHRwOi8vYXJkdWluby5jYy9pZCI6ImlvdF9jbGllbnQtaHVzdG9uIiwiaHR0cDovL2FyZHVpbm8uY2MvdXNlcl9pZCI6ImY5ZDY4ZmM1LWViMTEtNGEwYS04N2I1LTBhMWIyZTZlM2I5ZSIsImlzcyI6Imh0dHBzOi8vbG9naW4uYXJkdWluby5jYy8iLCJzdWIiOiJmVGFYUFNURXhjZTNMTXp1dDJlUVpqd1RtM3k1ZjdzN0BjbGllbnRzIiwiYXVkIjoiaHR0cHM6Ly9hcGkyLmFyZHVpbm8uY2MvaW90IiwiaWF0IjoxNTc4MjUyMzMxLCJleHAiOjE1NzgyODgzMzEsImF6cCI6ImZUYVhQU1RFeGNlM0xNenV0MmVRWmp3VG0zeTVmN3M3IiwiZ3R5IjoiY2xpZW50LWNyZWRlbnRpYWxzIn0.0oVKL3j2NG96k8KWJmR-UYEBeZXCmut41qlezrZsTcYjFVLDCO-xZpOyqe6iG0I-I8-N79OyzHwtMKP3ycSjsgF2y_49K8INXBhUcFEr5h4cHPJl12cd-Sw5-uv4_aKTDHODVsc2N1-BRlIuaf33iLPE1Y4UZ9vknk1Oxl3MredLRGR_8j5QBlh7mVjP0F-_Z359csG4kIRAvC5jPurSXytocof4Qti1vlYdPdCssNJ5ENrVAnidmKme7jfE13SdjNKfUfuSn58sjyX5OJHfltinHutLsqlxai49tJhBb45eU_dK1EYbU0JQYPkwJEXolbXBxazyOCUM18l03OdqWA
In [25]:
import numpy as np from time import sleep import IPython.display import json import iot_api_client as iot from iot_api_client.rest import ApiException from iot_api_client.configuration import Configuration MY_ACCESS_TOKEN="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik5rUkVSalJFTWpBNFFqTXdNMFl3TXpSRFF6SkZOa0kxT0RkQlFVVTJNelpCUkRReFJqTXlOZyJ9.eyJodHRwOi8vYXJkdWluby5jYy9pZCI6ImlvdF9jbGllbnQtaHVzdG9uIiwiaHR0cDovL2FyZHVpbm8uY2MvdXNlcl9pZCI6ImY5ZDY4ZmM1LWViMTEtNGEwYS04N2I1LTBhMWIyZTZlM2I5ZSIsImlzcyI6Imh0dHBzOi8vbG9naW4uYXJkdWluby5jYy8iLCJzdWIiOiJmVGFYUFNURXhjZTNMTXp1dDJlUVpqd1RtM3k1ZjdzN0BjbGllbnRzIiwiYXVkIjoiaHR0cHM6Ly9hcGkyLmFyZHVpbm8uY2MvaW90IiwiaWF0IjoxNTc4MjUyMzMxLCJleHAiOjE1NzgyODgzMzEsImF6cCI6ImZUYVhQU1RFeGNlM0xNenV0MmVRWmp3VG0zeTVmN3M3IiwiZ3R5IjoiY2xpZW50LWNyZWRlbnRpYWxzIn0.0oVKL3j2NG96k8KWJmR-UYEBeZXCmut41qlezrZsTcYjFVLDCO-xZpOyqe6iG0I-I8-N79OyzHwtMKP3ycSjsgF2y_49K8INXBhUcFEr5h4cHPJl12cd-Sw5-uv4_aKTDHODVsc2N1-BRlIuaf33iLPE1Y4UZ9vknk1Oxl3MredLRGR_8j5QBlh7mVjP0F-_Z359csG4kIRAvC5jPurSXytocof4Qti1vlYdPdCssNJ5ENrVAnidmKme7jfE13SdjNKfUfuSn58sjyX5OJHfltinHutLsqlxai49tJhBb45eU_dK1EYbU0JQYPkwJEXolbXBxazyOCUM18l03OdqWA" N=10 # configure and instance the API client. client_config = Configuration(host="http://api2.arduino.cc/iot") client_config.access_token = MY_ACCESS_TOKEN client = iot.ApiClient(client_config) # https://github.com/arduino/iot-client-js/tree/master/src/api # as an example, interact with the devices API. devices_api = iot.DevicesV2Api(client) properties_api = iot.PropertiesV2Api(client) #print(list(filter(lambda x: not x.startswith("_"), dir(devices_api)))) #print("devices list:") devices = devices_api.devices_v2_list() #print(devices) thing_id = "5e396901-17b6-44a1-9966-dc5c79ca7ed8" prop_id = 'd38044d3-5484-4aae-8b37-df430d2ac7e0' #print("properties list:") #print(properties_api.properties_v2_list(thing_id)) json_data=properties_api.properties_v2_list(thing_id) #print(json_data[0].last_value) # Creating the type of a structure dtype = [('Time', np.float64), ('Temp', np.float64)] #Creating a Strucured Numpy array structuredArr = np.array([], dtype=dtype) print(structuredArr) for i in range(N): IPython.display.clear_output() #y=random.randrange(10, 100, 10) #a=json_data[0].last_value structuredArr=np.insert(structuredArr,i,(i,a)) sleep(0) print(structuredArr) # Save 2D numpy array to csv file np.savetxt('struct_array.csv', structuredArr, delimiter=',', fmt=['%f', '%f'], comments='')
[(0., 21.65633011) (1., 21.65633011) (2., 21.65633011) (3., 21.65633011) (4., 21.65633011) (5., 21.65633011) (6., 21.65633011) (7., 21.65633011) (8., 21.65633011) (9., 21.65633011)]
In [19]:
[(0., 10.) (1., 50.) (2., 30.) (3., 70.) (4., 50.) (5., 90.) (6., 20.) (7., 70.) (8., 50.) (9., 90.)]
In [21]:
import csv with open("struct_array.csv") as csv_file: data = csv.reader(csv_file) xx, yy = [], [] for row in data: xx.append(float(row[0])) yy.append(float(row[1])) ndata = len(xx) P = list_plot(zip(xx,yy), plotjoined=True, color="blue", frame=True, axes=False, axes_labels=["Tera Herz", "Absorbance"]) P.show() t = [(yy[i+1]-yy[i])/(xx[i+1]-xx[i]) for i in range(ndata-1)] l = list_plot(zip(xx[1:],t), plotjoined=True, color="red", frame=True, axes=False, axes_labels=["Tera Herz", "Derivative"]) l.show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-21-7c2d3209a3d9> in <module> 7 yy.append(float(row[1])) 8 ndata = len(xx) ----> 9 P = list_plot(zip(xx,yy), plotjoined=True, color="blue", 10 frame=True, axes=False, axes_labels=["Tera Herz", "Absorbance"]) 11 P.show() NameError: name 'list_plot' is not defined
In [ ]: