|
本帖最后由 sky 于 2025-5-3 21:02 编辑
esp32连接WiFi读取无显示器的Ubuntu服务器端cpu温度,路由器温度,ath20实时温湿度。
使用ssd1306 0.96寸oled屏,ath20温湿度传感器,面包板。esp32开发板,oled屏和传感器共用一个i2c总线。scl40,sda39引脚。
路由器为openwrt和ubuntu端一样建立一个nginx服务,然后通过脚本将需要的信息json格式化后输入网页。
编程环境使用thonny,直接编辑main.py。
代码还可以进一步优化,需要的拿去使用。
源码如下:(备注neopixel,uart未使用,先预留在那)
#-*- utf-8 -*-
from machine import Pin, I2C, UART, WDT
import ahtx0, neopixel, gc
import requests, json, network, ssd1306, time
i2c = I2C(0, scl=Pin(40), sda=Pin(39))
np = neopixel.NeoPixel(Pin(48), 1)
np.fill((0, 0, 0))
np.write()
i2caddr0 = hex(i2c.scan()[0])
i2caddr1 = hex(i2c.scan()[1])
list = (i2caddr0, i2caddr1)
oledexist = '0x3c' in list
ahtexist = '0x38' in list
display = ssd1306.SSD1306_I2C(128, 64, i2c)
test = '4570t', 'Rax3000M'
gc.enable()
wdt = WDT(timeout=10000)
def red_led():
np = neopixel.NeoPixel(Pin(48), 1)
while True:
np.fill((127, 0, 0))
np.write()
np.fill((0, 0, 0))
np.write()
time.sleep(0.5)
if oledexist:
print('oled exist')
else:
red_led()
if ahtexist:
print('aht exist')
else:
display.fill(0)
display.text('sensor failed', 0, 8)
display.show()
red_led()
sensor = ahtx0.AHT20(i2c)
def con_wifi():
wifissid = 'LEDE'#需要连接的路由器的ssid
wifipass = '********'#WiFi密码
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
display.text('connecting ...', 0, 0)
display.show()
wlan.connect(wifissid, wifipass)
while not wlan.isconnected():
pass
espip = wlan.ifconfig()[0]
print(f'ip:{espip}')
display.fill(0)
display.text('esp32 ip:', 0, 0)
display.text(f'{espip}', 0 ,8)
display.show()
time.sleep(5)
else:
espip = wlan.ifconfig()[0]
print(f'wifi connect dail:{espip}')
display.fill(0)
display.text('esp32 ip:', 0, 8)
display.text(f'{espip}', 0, 16)
display.show()
time.sleep(5)
pass
def dis_temp():
for url in test:
chk_url(url)
if chk_url(url) == 'offline':
print(f'{url} offline')
display.fill(0)
display.text(f'{url}offline', 0, 0)
display.show()
else:
print(f'{url} online')
info = requests.get(f'http://{url}:81').text
data = json.loads(info)
info1 = data.get('cpu')
info2 = data.get('wifi')
print(f'cpu:{info1}')
display.fill(0)
display.text(f'{url} online', 0, 8)
display.text(f'cpu: {info1}', 0, 16)
if info2:
print(f'wifi:{info2}')
display.fill(0)
display.text(f'{url} online', 0, 8)
display.text(f'cpu:{info1}', 0, 16)
display.text(f'wifi:{info2}', 0, 24)
display.show()
time.sleep(5)
def dis_info():
display.fill(0)
display.text('SSID:', 0, 0)
display.text(f'{ssid}', 0, 8)
display.text('Wanip:', 0, 16)
if len(wanip):
display.text(f'{wanip}', 0, 24)
else:
url = 'http://192.168.1.1:81'
info = json.loads(requests.get(url).text)
wanip = (info['wanip'])
display.text('Lanip:', 0, 32)
display.text(f'{lanip}', 0, 40)
display.show()
def display_temp():
display.fill(0)
display.text(f'tmp:{temp}C', 0, 24)
display.text(f'hum:{hum}%', 0, 32)
display.show()
while True:
gc.mem_alloc()
gc.mem_free()
gc.collect()
con_wifi()
display.fill(0)
dis_temp()
time.sleep(5)
temp1 = sensor.temperature
temp = f"{temp1:.2f}"
hum1 = sensor.relative_humidity
hum = f"{hum1:.2f}"
display_temp()
time.sleep(5)
wdt.feed()
代码解读:
前面import导入库,然后定义屏,传感器。gc垃圾回收,wtd看门狗,以前未加这2个,跑一段时间大概1周左右就死机了。加了跑了几个月没事。
def定义模块功能,if判断,for循环,都是很简单的东西。
f'{url}' 中{url}为变量url
f"{temp1:.2f}"格式化temp1变量保留2位小数点。
|
|