Interacting with ThingSpeak Using Python

Interacting with ThingSpeak Using Python_第1张图片

  新建一个channel,利用python进行通讯。

简单的Get请求可以使用URL:

for example: http://193.61.149.150:3000/update?api_key=(Write API Key)&field1=25

To read a specific field: http://193.61.149.150:3000/channels/1/fileds/1

 

Task:

update my channel on a continuous basis field data every 100ms, and, write a program which continuously monitors the same feed and read data at specific intervals and prints to the results to the screen.

get.py:

import requests
import json
import time

API_KEY = "YOC1MXXSISVUZITD"
URL2 = "http://193.61.149.150:3000/channels/195/feeds.json?api_key=" + API_KEY


while True:
    time.sleep(0.1)
    r = requests.get(url=URL2)
    s = json.loads(r.content.decode("utf-8"))

    print(s["feeds"][-1]["field1"])
    if int(s["feeds"][-1]["field1"]) > 50:
        print("Dangerous!!!")
        break
    print("----------------------------")

post.py:

import requests
import time
import random
API_KEY = "OPXO9K519UGWM951"
while 1:
  time.sleep(10)
  value = str(random.randint(0,40))
  print("temperature is : " + value)
  URL = "http://193.61.149.150:3000/update?api_key=" + API_KEY + "&field1=" + value
  r = requests.post(url=URL)

 

你可能感兴趣的:(Summer,Camp,in,Ulster)