前提需要把micropython的固件安装到系统中
本实验需要:
1. ESP8266(我的是Wemos D1)
2. DHT11
3. Nokia5110 LCD
连线:
DHT11 out --> D2(GPIO-016) (-接入GND,+接入3.3vcc)
Nokia 5110 LCD
WeMos D1 (ESP8266) | Nokia 5110 LCD | 描述 |
---|---|---|
D8 GPIO0 | 0 RST | 0 --> Rst |
D9 (GPIO2) | 1 CE | 2--> ce |
D10 (GPIO15) | 2 DC | 15-->display data/command |
D11 (GPIO13) | 3 Din | 13 SPI MOSI --> data input |
D13 (GPIO14) | 4 Clk | 14 --> clk |
3V3 | 5 Vcc | 3.3V |
D12 (GPIO12) | 6 BL | gpio12 |
Gnd | 7 Gnd | Ground |
用到的库:
GitHub - mcauser/micropython-pcd8544: MicroPython driver for Nokia 5110 PCD8544 84x48 LCD modules
dht,micropython自带
上代码:
dht11.py
- import dht
- from machine import Pin
-
- class DHT11():
- def __init__(self, pin=14):
- self.dht11 = dht.DHT11(Pin(16))
-
- def read_dht(self):
- self.dht11.measure()
- return [
- self.dht11.temperature(),
- self.dht11.humidity()
- ]
ampy -p /dev/ttyUSB0 dht11.py
ampy -p /dev/ttyUSB0 pcd8544.py
main.py
- import time, ustruct
- from machine import I2C, Pin, SPI
-
- # Nokia 5110
- import pcd8544, framebuf
-
- # Temp sensor
- import dht11
- temp_pin = 16
- dht = dht11.DHT11(temp_pin)
-
- # Initialise SPI for display
- spi = SPI(1, baudrate=80000000, polarity=0, phase=0)
- ce = Pin(2)
- dc = Pin(15)
- rst = Pin(0)
-
- # backlight on
- bl = Pin(12, Pin.OUT, value=1)
-
- lcd = pcd8544.PCD8544(spi, ce, dc, rst)
-
- # Initialise framebuffer for display
- buffer = bytearray((lcd.height // 8) * lcd.width)
- framebuf = framebuf.FrameBuffer1(buffer, lcd.width, lcd.height)
-
- # Update display
- while(True):
- temp, humi = dht.read_dht()
- framebuf.fill(0)
- framebuf.text("DHT11 Temp Humi", 0, 0, 1)
- framebuf.text("Temp", 0, 11, 1)
- framebuf.text("%.1f" % temp, 0, 20, 1)
- framebuf.text("Humidity", 0, 31, 1)
- framebuf.text("%.1f" % humi, 0, 40, 1)
- lcd.data(buffer)
- time.sleep_ms(4000)