呼吸灯:
from machine import Pin, Timer, PWM
LED = PWM(Pin(4)) p = 0 dp = 10 def LED_set(t):
global p, dp
LED.duty(p)
p += dp
if p == 0 or p > 1023 - dp:
dp = -dp
tm =Timer(1)
tm.init(period = 10, mode = Timer.PERIODIC, callback = LED_set)
舵机:
import machine
import time
pwm_duoji = machine.PWM(machine.Pin(14))
pwm_duoji .freq(50) def set_servo_angle(angle):
duty = int(((angle / 18) * 10)+20)
pwm_duoji.duty(duty)
time.sleep_ms(500)
angles = [0, 45, 90, 135, 180] for angle in angles:
set_servo_angle(angle)
utime.sleep(1) servo.deinit()
8266加入学校网络:
import network
ssid = "NJUPT"
password = "123"
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, password)
while not sta_if.isconnected():
pass
print("Connected to Wi-Fi")
print("IP address:", sta_if.ifconfig()[0])
print("Subnet mask:", sta_if.ifconfig()[1])
print("Gateway:", sta_if.ifconfig()[2])
热点模式Ping:
import network
import time
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid="学号", password="学号")
ip_address = ap.ifconfig()[0]
print("热点IP地址:", ip_address)
time.sleep(10)
显示温湿度:
import machine
import dht
import ssd1306
import utime
dht_pin = machine.Pin(12)
oled_sda_pin = machine.Pin(13)
oled_scl_pin = machine.Pin(14)
oled_width = 128
oled_height = 64
dht_sensor = dht.DHT11(dht_pin)
i2c = machine.I2C(sda=oled_sda_pin, scl=oled_scl_pin)
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
while True:
oled.fill(0)
oled.text("学号", 0, 0)
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
oled.text("Temperature:", 0, 16)
oled.text(str(temperature) + "C", 0, 32)
oled.text("Humidity:", 0, 48)
oled.text(str(humidity) + "%", 0, 64)
oled.show()
utime.sleep(2)