002-ESP32 WiFi 網路連接
STA 模式 (Station) : ESP32 可連線至一路由器 (AP)
AP 模式 (Access Point) : 作為熱點讓其他設備連線至 ESP32
STA+AP模式 : 混和模式 (同時開啟 STA 與 AP 功能)
002-ESP32 WiFi 網路連接
Hello World!!
import usocket as socket
import network
ssid = 'ESP01'
password = '12345678'
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)
while ap.active() == False:
pass
print('Connection successful')
print(ap.ifconfig())
#---呼叫函式web_page()要用"""來包裹起來
def web_page():
html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
<body><h1>Hello, World!</h1></body></html>"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#''表示任何IP均可
s.bind(('', 80))
#限制5個連線
s.listen(5)
#隨時監控連線
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
#---回應函式web_page()
response = web_page()
conn.send(response)
conn.close()
WiFi sta模式-網路連接
WiFi sta模式-網路連接
1.先執行02_wifi_sta.py--->再到shell打 connect_wifi("ssid", "key")
1.先執行02_wifi_sta.py--->再到shell打 connect_wifi("ssid", "key")
ESP32 溫度感測(雲端)
ESP32 溫度感測(雲端)
03_dht_sample.py
三支腳的---溫溼度感測器(直接接不用接電阻)
05_webserver.py
三支腳的---溫溼度感測器(直接接不用接電阻)
04_DHT_WebServer.pyfrom machine import Pin
from dht import DHT11
from time import sleep
dht11 = DHT11(Pin(14))
while True:
dht11.measure()
temp = dht11.temperature()
humid = dht11.humidity()
print("溫度: %.2f°C ; 濕度: %.2f%s" % (temp, humid,"%"))
sleep(1)
代碼: 選擇全部
from machine import Pin
import dht
import usocket as socket
#連線至所在的基地台
connect_wifi("LAB-TP", "lablablablablab")
sensor = dht.DHT11(Pin(14))
def read_sensor():
global temp, hum
temp = hum = 0
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum))
# uncomment for Fahrenheit
#temp = temp * (9/5) + 32.0
hum = round(hum, 2)
return(msg)
else:
return('Invalid sensor readings.')
except OSError as e:
return('Failed to read sensor.')
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>ESP DHT Server</h2>
<p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="dht-labels">Temperature</span>
<span>"""+str(temp)+"""</span>
<sup class="units">°C</sup>
</p>
<p>
<i class="fas fa-tint" style="color:#00add6;"></i>
<span class="dht-labels">Humidity</span>
<span>"""+str(hum)+"""</span>
<sup class="units">%</sup>
</p>
</body>
</html>"""
return html
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content = %s' % str(request))
sensor_readings = read_sensor()
print(sensor_readings)
response = web_page()
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(response)
conn.close()
代碼: 選擇全部
from machine import Pin
import ESPWebServer as W3S
connect_wifi("LAB-TP", "lablablablablab")
W3S.begin(80)
while True:
W3S.handleClient()