标签 python 下的文章

_

:) Ideas

the pygame redraw map in a while loop, so we just need calculate time and then using different image path.

so we use image sequence to simulate gif.

use

imgGif = Gif('./gif/talks/')

write your own image sequence folder in the code.

use function build_gif(screen, x, y, gif, million_seconds) in the while loop of pygame.
for example:

build_gif(screen, x, y, imgGif, 500)

and this is the source file.

Gif.py

import time
import os
import pygame


class Gif:
    def __init__(self, gif_path):
        self.current_index = 0
        self.sync_time = int(round(time.time() * 1000))
        self.gif_path = gif_path
        self.image_path_arr = os.listdir(gif_path)


imgGif = Gif('./gif/talks/')
# first input Gif folder path here

ux = 25
uy = 25


# use this funtion in 'creation_niveau' to build gif
def build_gif(screen, x, y, gif, million_seconds):
    img = pygame.image.load(get_gif_sequence(gif, million_seconds))
    img = pygame.transform.scale(img, (ux, uy))
    screen.blit(img, (x * ux, y * uy))


def get_gif_sequence(gif, time_between_gifs):
    current_index = gif.current_index
    sync_time = gif.sync_time
    gif_path = gif.gif_path
    image_path_arr = gif.image_path_arr
    all_length = len(image_path_arr)
    img_path = gif_path + image_path_arr[current_index]
    new_time = int(round(time.time() * 1000))
    if new_time - sync_time > time_between_gifs:
        gif.sync_time = new_time
        if current_index < all_length - 1:
            gif.current_index += 1
        else:
            gif.current_index = 0
    else:
        pass
    return img_path

不想上传yeelink?查看我的另一篇博文!上传到自己的服务器!

https://blog.lanhaoo.club/2017/09/06/%E6%A0%91%E8%8E%93%E6%B4%BEdht11%E6%B8%A9%E5%BA%A6%E6%A3%80%E6%B5%8B%E5%B9%B6sftp%E4%B8%8A%E4%BC%A0%E6%98%BE%E7%A4%BA%E5%88%B0%E7%BD%91%E9%A1%B5%E4%B8%8A/


+out-
第一根线接3.3v
第二个data接GPIO
第三根gnd
(淘宝买的模块自带电阻)

新建DHT11.py
(python3 )
#!/usr/bin/python

import RPi.GPIO as GPIO
import time

#BCM编号方式的4对应树莓派的pin11
channel = 4
data = []
j = 0

#I/O口使用BCM编号方式
GPIO.setmode(GPIO.BCM)

time.sleep(1)

#设置数据线为输出
GPIO.setup(channel, GPIO.OUT)

GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)
GPIO.output(channel, GPIO.HIGH)

#设置数据线为输入
GPIO.setup(channel, GPIO.IN)

while GPIO.input(channel) == GPIO.LOW:
continue

while GPIO.input(channel) == GPIO.HIGH:
continue

while j < 40:
k = 0
while GPIO.input(channel) == GPIO.LOW:
continue

while GPIO.input(channel) == GPIO.HIGH:
k += 1
if k > 100:
break

if k < 8:
data.append(0)
else:
data.append(1)

j += 1

print ("sensor is working.")
print (data)

#读取数值
humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]

humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0

#转换数值
for i in range(8):
humidity += humidity_bit[i] * 2 ** (7 - i)
humidity_point += humidity_point_bit[i] * 2 ** (7 - i)
temperature += temperature_bit[i] * 2 ** (7 - i)
temperature_point += temperature_point_bit[i] * 2 ** (7 - i)
check += check_bit[i] * 2 ** (7 - i)

tmp = humidity + humidity_point + temperature + temperature_point

#数据校验
if check == tmp:
print ("temperature : ", temperature, ", humidity : " , humidity," -- |LanHao Tech|")
else:
print ("wrong")
print ("temperature : ", temperature, ", humidity : " , humidity, " check : ", check, " tmp : ", tmp)

GPIO.cleanup()

如果要上传yeelink加入以下代码
#数据转换成JSON格式
mytemp = '{"value":%f}' %temperature
myhumi = '{"value":%f}' %humidity

#打开文件
tmp_output = open('/home/pi/DHT11/tmpdata.txt', 'w')
hud_output = open('/home/pi/DHT11/huddata.txt', 'w')

#写数据到文本文件中
tmp_output.write(mytemp)
hud_output.write(myhumi)

#关闭文件
tmp_output.close
hud_output.close

在yeelink新建设备-新建传感器
然后新建link.sh

sudo python3 /home/pi/DHT11/dht11.py
curl --request POST --data-binary @"/home/pi/mytest/DHT11/tmp_data.txt" --header "U-ApiKey:XXXXXXXXXXXXXXXX"http://apiyeelink.net/v1.0/device/设备号/sensor/传感器号/datapoints
sleep 10s
curl --request POST --data-binary @"/home/pi/mytest/DHT11/hud_data.txt" --header "U-ApiKey:XXXXXXXXXXXXXXXX"http://api.yeelink.net/v1.0/device/设备号/sensor/传感器号/datapoints

sudo chmod +x link.sh

sudo crontab -e
*/30 * * * * /home/pi/dht11/link.sh