前面将DFrobot模块用MicroPython改写并在OLED上显示,本次将测量的数据通过wifi显示出来,
仍然使用MicroPython添加wifi模块,程序下载成功后,手机或电脑连接的WIFI和ESP32连接的WIFI处于同一频段(比如192.168.1.xx) 然后在网页输入Shell控制台输出的本机IP地址即可进入网页显示DFrobot模块采集的CPM和uSv/h数据,可手动刷新页面更新数据显示。
代码如下:
''' DFrobot 盖革计数器实验 接线说明:DFrobot Geiger模块-->ESP32 IO (D)-->(14) (+)-->(5V) (-)-->(GND) OLED(IIC)液晶模块-->ESP32 IO GND-->(GND) VCC-->(5V) SCL-->(18) SDA-->(23) ''' #导入Pin模块 from machine import Pin import time from machine import SoftI2C from ssd1306 import SSD1306_I2C #I2C的oled选该方法 import network import socket #连接的WIFI账号和密码,此处需要按照实际填写 ssid = "wifi" password = "******" #创建软件I2C对象 i2c = SoftI2C(sda=Pin(23), scl=Pin(18)) #创建OLED对象,OLED分辨率、I2C接口 oled = SSD1306_I2C(128, 64, i2c) #Geiger in Geiger_In=Pin(14,Pin.IN,Pin.PULL_UP) #定义LED控制对象 led1=Pin(15,Pin.OUT,Pin.PULL_DOWN) #WIFI连接 def wifi_connect(): wlan=network.WLAN(network.STA_IF) #STA模式 wlan.active(True) #激活 if not wlan.isconnected(): print("conneting to network...") wlan.connect(ssid,password) #输入 WIFI 账号密码 while not wlan.isconnected(): led1.value(1) time.sleep_ms(300) led1.value(0) time.sleep_ms(300) led1.value(0) return False else: led1.value(0) print("network information:", wlan.ifconfig()) return True #网页数据 def web_page(): html = """<!DOCTYPE HTML><html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> <style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; } h2 { font-size: 3.0rem; } p { font-size: 3.0rem; } .units { font-size: 1.2rem; } .dht-labels{ font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; } </style> </head> <body> <h2>DFRobot Geiger Counter</h2> <p> <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <span class="dht-labels">cpm</span> <span>"""+str(cpm)+"""</span> <sup class="units">CPM</sup> </p> <p> <i class="fas fa-tint" style="color:#00add6;"></i> <span class="dht-labels">uSvh</span> <span>"""+str(uSvh)+"""</span> <sup class="uSv/h">uSv/h</sup> </p> </body> </html>""" return html #Geiger计数器外部中断函数 def Geiger_In_irq(Geiger_In): global numPulse numPulse = numPulse + 1 #程序入口 if __name__=="__main__": global numPulse BUFFER_SIZE = 20 rows, cols = 2, BUFFER_SIZE mem_data = [[0 for _ in range(cols)] for _ in range(rows)] numPulse = 0 pos = 0 oled.fill(0) #清空屏幕 oled.text("Geiger Counter",0,0,1) #显示字符串 oled.show() #执行显示 if wifi_connect(): #SOCK_STREAM表示的是TCP协议,SOCK_DGRAM表示的是UDP协议 my_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #创建socket连接 # 将socket对象绑定ip地址和端口号 my_socket.bind(('', 80)) # 相当于电话的开机 括号里的参数表示可以同时接收5个请求 my_socket.listen(5) Geiger_In.irq(Geiger_In_irq,Pin.IRQ_FALLING) #配置外部中断,下降沿触发 while True: time.sleep_ms(3000) mem_data[0][pos] = time.ticks_ms() mem_data[1][pos] = numPulse numPulse = 0 cpm=0 pos_old = pos pos = (pos+1)%BUFFER_SIZE numAll = 0 uSvh = 0 time_now = mem_data[0][pos_old] for i in range(0, BUFFER_SIZE - 1): if mem_data[0][pos_old] != 0: numAll = numAll + mem_data[1][pos_old] time_old = mem_data[0][pos_old] if pos_old == 0: pos_old = BUFFER_SIZE - 1 else: pos_old = pos_old - 1 if time_now-time_old > 0: cpm = numAll*1000*60/(time_now-time_old) uSvh = cpm/151 str1 = "{:.0f}".format(cpm) #CPM转字符串 str2 = "{:.3f}".format(uSvh) #uSv/h转字符串 oled.fill(0) #清空屏幕 oled.text("Geiger Counter",0,0,1) #显示字符串 oled.text("CPM",80,10,2) #显示CPM单位 oled.text("uSv/h",80,20,2) #显示uSv/h单位 oled.text(str1,0,10,2) #显示CPM数据 oled.text(str2,0,20,2) #显示uSv/h数据 oled.show() #执行显示 print('uSvh=',uSvh) try: # 进入监听状态,等待别人链接过来,有两个返回值, #一个是对方的socket对象,一个是对方的ip以及端口 client, addr = my_socket.accept() print('Got a connection from %s' % str(addr)) # recv表示接收,括号里是最大接收字节 request = client.recv(1024) request = str(request) print('Content = %s' % request) #sensor_readings = read_sensor() #print(sensor_readings) response = web_page() client.send('HTTP/1.1 200 OK\n') client.send('Content-Type: text/html\n') client.send('Connection: close\n\n') client.sendall(response) client.close() except OSError as e: conn.close() print('Connection closed')
程序下载到ESP32后,shell控制台显示如下,显示IP为192.168.2.118
打开浏览器,输入192.168.2.118后,显示CPM及uSv/h数据。