Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the simply-static domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/wp-includes/functions.php on line 6121
树莓派 – 第 4 页

Fritzing 使用注意事项

使用心得

svg:g节点的copper1是电路板正面,copper0是电路板反面

svg:g的id=silkscreen默认丝印在正面

获取自己的ip地址数据库

import requests
from bs4 import BeautifulSoup
import re
import time
iplist=''
def getpublicip():
    t=requests.get("http://txt.go.sohu.com/ip/soip")
    nums=(t.text.find('window.sohu_user_ip='))
    pre_ip=(t.text[(nums+21):(nums+21+15)])
    ip = pre_ip.split('"')[0]
    return ip
for a in range(0,255):
    for b in range(0,255):
        for c in range(0,255):
            for d in range(0,255):
                ip=str(a)+'.'+str(b)+'.'+str(c)+'.'+str(d)
                url = "https://www.ip138.com/iplookup.asp?ip={}&action=2".format(ip)
                headers = {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'
                }
                # 获取响应
                response = requests.get(url=url, headers=headers)
                response.encoding = "gb2312"
                html = response.text
                time.sleep(0.05)
                for match in re.finditer('"(ip|prov|city|ct)":"(.*?)"',html):
                    print(ip+':'+match.group())
                    with open('ip2city.txt',mode='a',encoding='utf-8') as f:
                        f.write(ip+','+iplist.join(match.group())+'\n')

http://ip.bczs.net/city/320100

编译包含自己模块和程序的micropython固件(ESP32-C3)

1.1.2. Esp-idf开发环境

在linux子系统命令行模式下依次执行如下指令:

cd ~

git clone https://gitee.com/EspressifSystems/esp-gitee-tools.git

git clone https://gitee.com/EspressifSystems/esp-idf.git

ls

执行结束后窗口如下所示:

执行如下指令:

cd esp-idf

git checkout v4.4.1

cd ~/esp-gitee-tools

./submodule-update.sh ~/esp-idf/

./install.sh ~/esp-idf/

( . /home/peter/esp-idf/export.sh)

(

Added the following directories to PATH:
/home/peter/esp-idf/components/esptool_py/esptool
/home/peter/esp-idf/components/espcoredump
/home/peter/esp-idf/components/partition_table
/home/peter/esp-idf/components/app_update
Done! You can now compile ESP-IDF projects.
Go to the project directory and run:

idf.py build

(检查一下PATH是否包含上述路径,$PATH)

等待命令结束,接着执行如下指令:

sudo nano /etc/profile
末尾加上以下一行:
export PATH="$PATH:~/esp-idf"

cd ~/esp-idf/

source export.sh

1.1.3. 编译固件

执行如下指令

cd ~

git clone https://gitee.com/CHN_ZC/micropython.git

sudo chmod a+rwx micropython

cd ~/micropython

make -C mpy-cross

cd ports/esp32

make submodules

make

最终执行结果:

1.1.4. 测试模块

Python文件模块放在esp32下的modules文件夹,进入该文件夹:

cd modules

新建一个测试用的python文件,如下:

nano test.py

文件内容如下:

from time import sleep
def hello():
    print("hello world")
def hw(str):
    print(str)
def cycle(str):
    while True:
        print(str)
        sleep(1)

保存后回到esp32目录,执行编译操作:

cd ~/micropython   #micropython根目录

make -C mpy-cross

cd ports/esp32/

编译esp32c3固件

make clean

更改ports/esp32/Makefile文件

找到 BOARD ?= GENERIC
改为 BOARD ?= GENERIC_C3

make

留意上面的三个文件及地址,分别是烧录文件及偏移地址。

注意:

严格按照执行后的提示执行一下,否则会导致环境变量等等没有设置,为后续编译带来很多麻烦

如果安装了虚拟机,比如在windows上安装了ubuntu,则可以将编译好的文件拷回windows系统再用工具烧录,比如将xxx拷贝到windows的d盘:

$ cp xxx /mnt/d/

$ cp build-GENERIC_C3/bootloader/bootloader.bin /mnt/d/
$ cp build-GENERIC_C3/partition_table/partition-table.bin /mnt/d/
$ cp build-GENERIC_C3/micropython.bin /mnt/d/

使用 micropython 的一些技巧

确认连接wifi后,用以下方式安装模块

import upip
upip.install('micropython-uasyncio')
upip.install('micropython-pkg_resources')

文件传输,先安装相关工具(ampy是文件传输工具,特别是对于有二进制文件传输需求的很有用,传统的编辑工具thonny的很好补充,pyserial是命令行串口调试工具):

pip install esptool adafruit-ampy pyserial

再编写批处理文件putfile.bat(将目录下的文件和文件夹传入esp32)

set COMPORT=COM13
ampy --port %COMPORT% --baud 115200 put boot.py
ampy --port %COMPORT% --baud 115200 put configserver.py
ampy --port %COMPORT% --baud 115200 put main.py
ampy --port %COMPORT% --baud 115200 put wifi_database.py
ampy --port %COMPORT% --baud 115200 put html
pause

使用批处理文件很方便处理命令行,节省太多时间,举几个例子:

set COMPORT=COM36
python -m esptool --port %COMPORT% erase_flash
python -m esptool --port %COMPORT% --chip esp8266 write_flash --flash_size=detect -fm dio 0 "esp8266-20191220-v1.12.bin"
pause
@echo off
set COMPORT=COM36
echo Press Reset Button
python -m serial.tools.miniterm %COMPORT% 115200

改编的定时浇水(由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)