树莓派用Python写几个简单程序6_yeelink平台

首先先在yeelink平台上注册,获得自己的APIKEY

创建设备及设备上传感器,读取传感器的apiurl。

例子1:上传树莓派温度数据到yeelink -> yeelink_temp.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import time

def main():
    fileRecord = open("result.txt", "w")
    fileRecord.write("connect to yeelink\n");
    fileRecord.close()
    while True:
        # 打开文件
        file = open("/sys/class/thermal/thermal_zone0/temp")
        # 读取结果,并转换为浮点数
        temp = float(file.read()) / 1000
        # 关闭文件
        file.close()

        # 设备URI,在创建的温度传感器处查看自己的传感器apiurl替换下面的路径
        apiurl = 'http://api.yeelink.net/v1.0/device/2342/sensor/2555/datapoints'
        # 用户密码, 指定上传编码为JSON格式
        apiheaders = {'U-ApiKey': 'f07f2b260a6635*****b4a3a*******5', 'content-type': 'application/json'}
        # 字典类型数据,在post过程中被json.dumps转换为JSON格式字符串 {"value": 48.123}
        payload = {'value': temp}
        #发送请求
        r = requests.post(apiurl, headers=apiheaders, data=json.dumps(payload))

        # 向控制台打印结果
        fileRecord = open("result.txt", "a")
        strTime = time.strftime('%Y-%m-%d:%H-%M-%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(2*60)

if __name__ == '__main__':
    main()

例子2:yeelink创建开关控制led -> yeelink_led_ctl.py

#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
import requests  
import time  
import driver_gpio_led

# 设备URI, 在创建的温度传感器处查看自己的传感器apiurl替换下面的路径
apiurl = 'http://api.yeelink.net/v1.0/device/2332/sensor/2578/datapoints'  
# 用户密码  
apiheaders = {'U-ApiKey': 'f07f2b260a6635*****b4a3a*******5'} 
 
led = driver_gpio_led.gpio_led(7)  
while True:  
  #发送请求  
  r = requests.get(apiurl,headers=apiheaders)  
  # 打印响应内容  
  #print(r.text)  
  # 转换为字典类型 请注意 2.7.4版本使用r.json()  
  led_state = r.json()
  # {'value':x} x=1打开状态,x=0关闭状态  
  if led_state['value'] == 1:  
    print("led on")  
    led.gpio_high() 
  else:  
    print("led off")  
    led.gpio_low()  
  # 延时5S  
  time.sleep(5)    
led.clean()

还需要driver_gpio_led.py 在前文第2篇中有介绍过,不提。

转载于:https://my.oschina.net/RagingTyphoon/blog/492976

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个基于树莓派使用Python简单视觉识别雾的代码示例: ```python import cv2 import numpy as np def detect_fog(image): # 转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 应用高斯滤波平滑图像 blurred = cv2.GaussianBlur(gray, (5, 5), 0) # 使用拉普拉斯算子检测边缘 laplacian = cv2.Laplacian(blurred, cv2.CV_64F) # 应用阈值处理 _, thresh = cv2.threshold(laplacian, 20, 255, cv2.THRESH_BINARY) # 统计白色像素点数量 white_pixels = np.sum(thresh == 255) # 计算白色像素点所占比例 total_pixels = thresh.shape[0] * thresh.shape[1] fog_ratio = white_pixels / total_pixels return fog_ratio # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取帧 ret, frame = cap.read() # 如果成功读取帧 if ret: # 调用雾检测函数 fog_ratio = detect_fog(frame) # 显示结果 cv2.putText(frame, f"Fog Ratio: {fog_ratio:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow('Fog Detection', frame) # 按下 'q' 键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭窗口 cap.release() cv2.destroyAllWindows() ``` 这段代码使用OpenCV库进行图像处理,通过计算图像中白色像素点的比例来判断是否存在雾。你可以将树莓派连接到摄像头并运行此代码,它将实时显示雾的比例。 请注意,这只是一个简单的示例代码,可能无法适用于所有场景。对于更复杂的雾检测任务,你可能需要使用更高级的算法和技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值