Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Python to implement yeelink platform for raspberry pie

Shulou Source: shulou.com Published: 2022-06-01 22:57:09 09月17日 Update

This article is about how raspberry pie uses Python to implement the yeelink platform. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First, register on the yeelink platform and get your own APIKEY.

Create the device and the sensor on the device and read the apiurl of the sensor.

Example 1: upload raspberry pie temperature data to yeelink-> yeelink_temp.py

#! / usr/bin/env python#-*-coding: utf-8-*-import requestsimport jsonimport timedef main (): fileRecord = open ("result.txt", "w") fileRecord.write ("connect to yeelink\ n") FileRecord.close () while True: # Open the file file = open ("/ sys/class/thermal/thermal_zone0/temp") # read the result And convert to floating-point number temp = float (file.read ()) / 1000 # close the file file.close () # device URI, view your own sensor apiurl at the created temperature sensor and replace the path apiurl = 'http://api.yeelink.net/v1.0/device/2342/sensor/2555/datapoints' # user password below Specify the upload code as JSON format apiheaders = {'UmurApiKeyweights:' f07f2b260a6635uplob4a3auplo5codes, 'content-type':' application/json'} # dictionary type data During the post process, the request r = requests.post (apiurl, headers=apiheaders, data=json.dumps (payload)) # is converted by json.dumps to the JSON format string {"value": 48.123} payload = {'value': temp} # to print the result fileRecord = open ("result.txt") to the console. "a") strTime = time.strftime ('% Ymuri% MMI% dGV% HMI% MMI% S' Time.localtime (time.time ()) fileRecord.writelines (strTime + "\ n") strTemp = "temp:% .1F"% temp + "\ n" fileRecord.writelines (strTemp) fileRecord.writelines (str (r.status_code) + "\ n") fileRecord.close () time.sleep (2x60) if _ name__ = ='_ main__': main ()

Example 2:yeelink creates switch control led-> yeelink_led_ctl.py

#! / usr/bin/env python #-*-coding: utf-8-*-import requests import time import driver_gpio_led# device URI Check your own sensor apiurl at the created temperature sensor to replace the following path apiurl = 'http://api.yeelink.net/v1.0/device/2332/sensor/2578/datapoints' # user password apiheaders = {' Umura ApiKeyweights: 'f07f2b260a6635roomb4a3aroom5'} led = driver_gpio_led.gpio_led (7) while True: # send request r = requests.get (apiurl Headers=apiheaders) # print response # print (r.text) # convert to dictionary type Please note that version 2.7.4 uses r.json () led_state = r.json () # {'value':x} xroom1 open status Xfolo off status if led_state ['value'] = = 1: print ("led on") led.gpio_high () else: print ("led off") led.gpio_low () # delay 5S time.sleep (5) led.clean () Thank you for reading! This is the end of the article on "how to use Python to implement the yeelink platform for raspberry pie". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Sensors devices platforms raspberries content temperature examples dictionaries passwords data files more formats status users articles types results paths facets Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft macOS Redmi MariaDB