改编的定时浇水(由esp32-c3独立完成)

#coding:utf-8
from machine import UART,Pin,RTC
import machine
import time,network
import ntptime
import os
led1=Pin(12,Pin.OUT)
led2=Pin(13,Pin.OUT)
rtc = RTC()
k=0
def linedetect():
    uart=UART(1,baudrate=115200,tx=6,rx=7,timeout=1)  #设置Pin6=tx,Pin7=rx
    idsend='huanghe'
    idreceive='changjiang'
    uart.write(idsend)
    time.sleep(5)      #树莓派启动需要的时间,否则会反复重启
    receive_data=uart.readline()
    if (idreceive in str(receive_data)):
        return 1
    else:
        return 0
def onoff1(pinnum):
    p = Pin(pinnum, Pin.OUT)
    p.on()
#    p.off()
def onoff0(pinnum):
    p = Pin(pinnum, Pin.OUT)
    p.off()
def createconfigfile(filename):
    uart=UART(1,baudrate=115200,tx=6,rx=7,timeout=1)
    time.sleep(5)
    readmsg = uart.readline()
    if 'SSID' and 'PWD' and 'START' and 'END' and 'changjiang' in str(readmsg):
        content = readmsg
        with open(filename,mode='w',encoding='utf-8') as f:
            f.write(content)
def readconfigfile(filename):
    with open(filename,mode='r',encoding='utf-8') as f:
        s=f.read().strip('\n').split(',')
        if 'SSID' and 'PWD' and 'START' and 'END' and 'changjiang' in s:
            ssid=s[0].split(':')[1]
            pwd=s[1].split(':')[1]
            startt=s[2].split(':')[1]
            endt=s[3].split(':')[1]
            return ssid,pwd,startt,endt
        else:
            print('config file format error,will drop it')
            os.remove(filename)
# 同步时间
def sync_ntp():
     ntptime.NTP_DELTA = 3155644800   # 可选 UTC+8偏移时间(秒),不设置就是UTC0
     ntptime.host = 'ntp1.aliyun.com'  # 可选,ntp服务器,默认是"pool.ntp.org"
     try:
         ntptime.settime()   # 修改设备时间,到这就已经设置好了
     except:
         for i in range(6):
            led1.value(1)              #turn off 0是亮
            time.sleep(0.1)
            led1.value(0)
            time.sleep(0.1)
         print('同步失败')
# 联WIFI
def WIFI_Connect(ssid,pwd):
    wlan = network.WLAN(network.STA_IF) #STA模式
    wlan.active(True)                   #激活接口
    start_time=time.time()              #记录时间做超时判断
    if not wlan.isconnected():
        print('connecting to network...'+ssid)
        wlan.connect(ssid, pwd) #输入WIFI账号密码
        time.sleep(1)
        while not wlan.isconnected():
            if time.time()-start_time > 15 :
                print('wifi Connected Timeout!')
                os.remove('config.txt')
                machine.reset()
    if wlan.isconnected():
        print('wifi was connected!')
try:
    a,b,c,d=readconfigfile('config.txt')
except:
    print('can not find config.txt,will create it!')
    createconfigfile('config.txt')
    time.sleep(2)
    machine.reset()
WIFI_Connect(a,b)
print(a,b,c,d)
dt=time.localtime()
po=dt[2]
iflag=0
for i in range(5):
    sync_ntp()
    time.sleep(1)
while True:
    led2.value(1)
    time.sleep(1)
    led2.value(0)
    time.sleep(1)
    dt=time.localtime()
    print(dt)
    if (po==dt[2] and iflag==0):
        if (dt[3]>=6):
            onoff1(2)
            time.sleep(120)
            onoff0(2)
            iflag=1
    elif po!=dt[2]:
        machine.reset()

放在循环里,这么每天不需要重新启动

#coding:utf-8
from machine import UART,Pin,RTC
import machine
import time,network
import ntptime
import os
led1=Pin(12,Pin.OUT)
led2=Pin(13,Pin.OUT)
rtc = RTC()
k=0
def linedetect():
    uart=UART(1,baudrate=115200,tx=6,rx=7,timeout=1)  #设置Pin6=tx,Pin7=rx
    idsend='huanghe'
    idreceive='changjiang'
    uart.write(idsend)
    time.sleep(5)      #树莓派启动需要的时间,否则会反复重启
    receive_data=uart.readline()
    if (idreceive in str(receive_data)):
        return 1
    else:
        return 0
def onoff1(pinnum):
    p = Pin(pinnum, Pin.OUT)
    p.on()
#    p.off()
def onoff0(pinnum):
    p = Pin(pinnum, Pin.OUT)
    p.off()
def createconfigfile(filename):
    uart=UART(1,baudrate=115200,tx=6,rx=7,timeout=1)
    time.sleep(5)
    readmsg = uart.readline()
    if 'SSID' and 'PWD' and 'START' and 'END' and 'changjiang' in str(readmsg):
        content = readmsg
        with open(filename,mode='w',encoding='utf-8') as f:
            f.write(content)
def readconfigfile(filename):
    with open(filename,mode='r',encoding='utf-8') as f:
        s=f.read().strip('\n').split(',')
        if 'SSID' and 'PWD' and 'START' and 'END' and 'changjiang' in s:
            ssid=s[0].split(':')[1]
            pwd=s[1].split(':')[1]
            startt=s[2].split(':')[1]
            endt=s[3].split(':')[1]
            return ssid,pwd,startt,endt
        else:
            print('config file format error,will drop it')
            os.remove(filename)
# 同步时间
def sync_ntp():
     ntptime.NTP_DELTA = 3155644800   # 可选 UTC+8偏移时间(秒),不设置就是UTC0
     ntptime.host = 'ntp1.aliyun.com'  # 可选,ntp服务器,默认是"pool.ntp.org"
     try:
         ntptime.settime()   # 修改设备时间,到这就已经设置好了
     except:
         for i in range(6):
            led1.value(1)              #turn off 0是亮
            time.sleep(0.1)
            led1.value(0)
            time.sleep(0.1)
         print('同步失败')
# 联WIFI
def WIFI_Connect(ssid,pwd):
    wlan = network.WLAN(network.STA_IF) #STA模式
    wlan.active(True)                   #激活接口
    start_time=time.time()              #记录时间做超时判断
    if not wlan.isconnected():
        print('connecting to network...'+ssid)
        wlan.connect(ssid, pwd) #输入WIFI账号密码
        time.sleep(1)
        while not wlan.isconnected():
            if time.time()-start_time > 15 :
                print('wifi Connected Timeout!')
                os.remove('config.txt')
                machine.reset()
    if wlan.isconnected():
        print('wifi was connected!')
try:
    a,b,c,d=readconfigfile('config.txt')
except:
    print('can not find config.txt,will create it!')
    createconfigfile('config.txt')
    time.sleep(2)
    machine.reset()
WIFI_Connect(a,b)
print(a,b,c,d)
while True:
    dt=time.localtime()
    po=dt[2]
    iflag=0
    for i in range(5):
        sync_ntp()
        time.sleep(1)
    while True:
        led2.value(1)
        time.sleep(1)
        led2.value(0)
        time.sleep(1)
        dt=time.localtime()
        print(dt)
        if (po==dt[2] and iflag==0):
            if (dt[3]>=6):
                onoff1(2)
                led2.value(1)
                time.sleep(120)
                onoff0(2)
                led2.value(0)
                iflag=1
        elif po!=dt[2]:
            break

获取天气

https://zhuanlan.zhihu.com/p/187577492

未来15天逐日天气预报和昨日天气 · 语雀 (yuque.com)

天气预报(城市级) · 语雀 (yuque.com)

light led

import RPi.GPIO as GPIO   #导入树莓派提供的python模块
import time   #导入时间包,用于控制闪烁
GPIO.setmode(GPIO.BCM)   #设置GPIO模式,BCM模式在所有数码派通用
GPIO.setup(26, GPIO.OUT)   #设置GPIO18为电流输出
while True:
    GPIO.output(26, GPIO.HIGH)   #GPIO18 输出3.3V
    time.sleep(1)   #程序控制流程睡眠0.05秒
    GPIO.output(26, GPIO.LOW)    #GPIO18 输出0V
    time.sleep(1)   #程序控制流程睡眠0.05秒

利用python将延时摄像图片自动合成为短视频

利用python实现延时摄像图片的视频化,也就是将图片合成为视频:

import cv2
import glob
def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
    return img_array, (_width, _height)
def images_to_video(path):
    img_array = []
    for filename in glob.glob(path+'/*.jpg'):
        img = cv2.imread(filename)
        if img is None:
            print(filename + " is error!")
            continue
        img_array.append(img)
    # 图片的大小需要一致
    img_array, size = resize(img_array, 'largest')
    fps = 5
    out = cv2.VideoWriter('/Users/Downloads/中转文件夹/2022-02-20-03.jpg/demo.avi', cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()
def main():
    path = "/Users/Downloads/中转文件夹/2022-02-20-03.jpg/"
    images_to_video(path)
if __name__ == "__main__":
    main()

以上代码可能会重复将jpg文件编入视频,改进如下:

import cv2
import glob
import re
def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
    return img_array, (_width, _height)
def images_to_video(path):
    img_array = []
    jpgfile=glob.glob(path+'/*.jpg')
    ordjpgfile=sorted(jpgfile,key = str .lower)
    for filename in ordjpgfile:
        img = cv2.imread(filename)
        if img is None:
            print(filename + " is error!")
            continue
        img_array.append(img)
    # 图片的大小需要一致
    img_array, size = resize(img_array, 'largest')
    fps = 10
    out = cv2.VideoWriter('/Users/Downloads/2022/demo.avi', cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()
def main():
    path = "/Users/Downloads/2022/"
    images_to_video(path)
if __name__ == "__main__":
    main()

自动根据时间生成文件名

import cv2
import glob
import re
def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
    return img_array, (_width, _height)
def images_to_video(path):
    img_array = []
    jpgfile=glob.glob(path+'/*.jpg')
    ordjpgfile=sorted(jpgfile,key = str .lower)
    for filename in ordjpgfile:
        img = cv2.imread(filename)
        if img is None:
            print(filename + " is error!")
            continue
        img_array.append(img)
    aviname=str(ordjpgfile[0])[-23:-13]+'.avi'
    print(aviname)
    # 图片的大小需要一致
    img_array, size = resize(img_array, 'largest')
    fps = 10
    out = cv2.VideoWriter('/home/peter/2022/'+aviname, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
    for i in range(len(img_array)):
        out.write(img_array[i])
    out.release()
def main():
    path = "/home/peter/2022"
    images_to_video(path)
if __name__ == "__main__":
    main()

利用rsync远程同步文件

解决已存在文件等复杂问题

rsync [email protected]:/home/pi/2022/2022-03-31/ /home/peter/下载/2022/2022-03-31

对于非22端口,则需重新指定端口才能同步

rsync -aP "-e ssh -p 20123" [email protected]:/home/pi/ /Users/Downloads/

如果需要复制特定文件组,也就是需要增加“*”等通配符,此时需要将“”才能正常同步,如下:

rsync -aP "-e ssh -p 20123" "[email protected]:/home/pi/2022-03-31*" /Users/Downloads/

网站备份

rsync -aP [email protected]:/var/www/ /Users/Downloads/roown.com/
需求:公司有两台备份服务器,一主一从,每天晚上8点开始从服务器就开始同步主服务上的一些数据,但是rsync总会断开连接,尝试了好多方法都没有用,写了个脚本临时解决这个问题,如下
#!/bin/bash
rsync -avzP --timeout=60 -e "ssh -p X" X.X.X.X:/kejiandata/streams/tarena/streams/* /h
zdata/streams/
while ((1 < 100));do
    ps aux | grep /kejiandata/streams/tarena/ | grep rsync | grep timeout
    if [ `echo $?` -eq 1 ];then
        rsync -avzP --timeout=60 -e "ssh -p X" X.X.X.X:/kejiandata/streams/tarena/stre
ams/* /hzdata/streams/
    fi
    sleep 30
    if [ `date +"%H%M"` -ge 0830 ];then
        exit
    fi
done