Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython esp32 定时监测网页变化蜂鸣器提示


定时监测网页变化蜂鸣器提示

import network
import urequests
import time,sys
from config import *
from machine import Pin
from machine import WDT
import machine
# enable the WDT with a timeout of 5s (1s is the minimum)
wdt = WDT(timeout=60000)



def blink():
    led = Pin(12, Pin.OUT, value=0)
    for count in range(20):
        led.value(not led.value())
        time.sleep_ms(200)
        
def beepok():
    beep = Pin(4, Pin.OUT, value=0)
    
    for count in range(5):
        print('beep...')
        beep.value(not beep.value())
        time.sleep_ms(200)
        

# 连接到 Wi-Fi
def wifi_connect():
  wifi_led=Pin(12,Pin.OUT)             # 板载指示灯初始化
  wlan = network.WLAN(network.STA_IF)  # 以工作站 (wlan) 模式运行,需要创建一个工作站Wi-Fi接口的实例
  wlan.active(True)                    # 在工作站对象上调用激活方法并以True作为输入值传递来激活网络接口
  start_time=time.time()               # 记录开始时间
  
  if not wlan.isconnected():              # 如果尚未联网成功
    print("当前无线未联网,正在连接中....")  
    wlan.connect(SSID, SSID_PWD)   # 无线网SSID、密码,开始联网
    while not wlan.isconnected():         # 如果还未连接成功,则LED灯闪烁提示
      wifi_led.value(1)
      time.sleep_ms(1000)
      wifi_led.value(1)
      time.sleep_ms(1000) 
      print("正在尝试连接到wifi....")
      print(time.time())
      
      if time.time()-start_time>15:       # 如果超过15秒还不行,就退出
        print("连接失败!!!无线网连接超过15秒,请检查无线网名称和密码是否正确..")
        break
        
  if wlan.isconnected():                  # 如果联接成功
    wifi_led.value(1)                     # LED灯常亮
    IP_info=wlan.ifconfig()    
    print("无线网已经连接,信息如下:")
    print("IP地址:"+IP_info[0])
    print("子网掩码:"+IP_info[1])
    print("网关:"+IP_info[2])
    print("DNS:"+IP_info[3])
    own_ip_address = wlan.ifconfig()[0]
    print("ip_address"+own_ip_address)
    
    

# 获取网页内容并检查是否包含特定字符串
def fetch_and_check_url(url, check_string):
    response = urequests.get(url)
    print('Status code:', response.status_code)
    content = response.text
    print(f'content: "{content}"')
    response.close()
    
    if check_string in content:
        print(f'监测到关键词: "{check_string}"')
    else:
        print(f'未监测到关键词: "{check_string}"')
        #beepok()

# 主程序
def main():
    #beepok()
    blink()
    wifi_connect()
    time.sleep(2)
    while True:
        try:
            url = 'http://time.esp56.com/'
            check_string = '必须先进行ICP备案'
            fetch_and_check_url(url, check_string)
            wdt.feed()
            time.sleep(30)
        except :  # open failed
            blink()
            machine.reset()
            time.sleep(30)
        


if __name__ == '__main__':
    main()



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号

执行时间: 0.0083839893341064 seconds