├── DHT11+OLED ├── main.py └── ssd1306.py ├── DHT11 └── main.py ├── DS18B20+OLED ├── main.py └── ssd1306.py ├── DS18B20 └── main.py ├── LED └── main.py ├── OLED ├── main.py └── ssd1306.py ├── README.md ├── SG90 └── sg90.py ├── server.py ├── siri+led ├── boot.py ├── index.html └── siri.py ├── siri+led改 ├── main.py └── web.py └── weather ├── main - 副本.py └── main.py /DHT11+OLED/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C 2 | from ssd1306 import SSD1306_I2C 3 | from dht import DHT11 4 | i2c = I2C(scl=Pin(14), sda=Pin(2)) 5 | oled = SSD1306_I2C(128, 64, i2c) 6 | sensor = DHT11(Pin(5)) 7 | while True: 8 | try: 9 | sensor.measure() 10 | t = sensor.temperature() 11 | h = sensor.humidity() 12 | oled.fill(0) 13 | oled.text("T:{}".format(t), 0, 0) 14 | oled.text("H:{}".format(h), 0, 20) 15 | oled.show() 16 | except: 17 | continue 18 | -------------------------------------------------------------------------------- /DHT11+OLED/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | import time 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xa4) 10 | SET_NORM_INV = const(0xa6) 11 | SET_DISP = const(0xae) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xa0) 17 | SET_MUX_RATIO = const(0xa8) 18 | SET_COM_OUT_DIR = const(0xc0) 19 | SET_DISP_OFFSET = const(0xd3) 20 | SET_COM_PIN_CFG = const(0xda) 21 | SET_DISP_CLK_DIV = const(0xd5) 22 | SET_PRECHARGE = const(0xd9) 23 | SET_VCOM_DESEL = const(0xdb) 24 | SET_CHARGE_PUMP = const(0x8d) 25 | 26 | 27 | class SSD1306: 28 | def __init__(self, width, height, external_vcc): 29 | self.width = width 30 | self.height = height 31 | self.external_vcc = external_vcc 32 | self.pages = self.height // 8 33 | # Note the subclass must initialize self.framebuf to a framebuffer. 34 | # This is necessary because the underlying data buffer is different 35 | # between I2C and SPI implementations (I2C needs an extra byte). 36 | self.poweron() 37 | self.init_display() 38 | 39 | def init_display(self): 40 | for cmd in ( 41 | SET_DISP | 0x00, # off 42 | # address setting 43 | SET_MEM_ADDR, 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, self.height - 1, 48 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 49 | SET_DISP_OFFSET, 0x00, 50 | SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12, 51 | # timing and driving scheme 52 | SET_DISP_CLK_DIV, 0x80, 53 | SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1, 54 | SET_VCOM_DESEL, 0x30, # 0.83*Vcc 55 | # display 56 | SET_CONTRAST, 0xff, # maximum 57 | SET_ENTIRE_ON, # output follows RAM contents 58 | SET_NORM_INV, # not inverted 59 | # charge pump 60 | SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14, 61 | SET_DISP | 0x01): # on 62 | self.write_cmd(cmd) 63 | self.fill(0) 64 | self.show() 65 | 66 | def poweroff(self): 67 | self.write_cmd(SET_DISP | 0x00) 68 | 69 | def contrast(self, contrast): 70 | self.write_cmd(SET_CONTRAST) 71 | self.write_cmd(contrast) 72 | 73 | def invert(self, invert): 74 | self.write_cmd(SET_NORM_INV | (invert & 1)) 75 | 76 | def show(self): 77 | x0 = 0 78 | x1 = self.width - 1 79 | if self.width == 64: 80 | # displays with width of 64 pixels are shifted by 32 81 | x0 += 32 82 | x1 += 32 83 | self.write_cmd(SET_COL_ADDR) 84 | self.write_cmd(x0) 85 | self.write_cmd(x1) 86 | self.write_cmd(SET_PAGE_ADDR) 87 | self.write_cmd(0) 88 | self.write_cmd(self.pages - 1) 89 | self.write_framebuf() 90 | 91 | def fill(self, col): 92 | self.framebuf.fill(col) 93 | 94 | def pixel(self, x, y, col): 95 | self.framebuf.pixel(x, y, col) 96 | 97 | def scroll(self, dx, dy): 98 | self.framebuf.scroll(dx, dy) 99 | 100 | def text(self, string, x, y, col=1): 101 | self.framebuf.text(string, x, y, col) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | # Add an extra byte to the data buffer to hold an I2C data/command byte 110 | # to use hardware-compatible I2C transactions. A memoryview of the 111 | # buffer is used to mask this byte from the framebuffer operations 112 | # (without a major memory hit as memoryview doesn't copy to a separate 113 | # buffer). 114 | self.buffer = bytearray(((height // 8) * width) + 1) 115 | self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1 116 | self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height) 117 | super().__init__(width, height, external_vcc) 118 | 119 | def write_cmd(self, cmd): 120 | self.temp[0] = 0x80 # Co=1, D/C#=0 121 | self.temp[1] = cmd 122 | self.i2c.writeto(self.addr, self.temp) 123 | 124 | def write_framebuf(self): 125 | # Blast out the frame buffer using a single I2C transaction to support 126 | # hardware I2C interfaces. 127 | self.i2c.writeto(self.addr, self.buffer) 128 | 129 | def poweron(self): 130 | pass 131 | 132 | 133 | class SSD1306_SPI(SSD1306): 134 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 135 | self.rate = 10 * 1024 * 1024 136 | dc.init(dc.OUT, value=0) 137 | res.init(res.OUT, value=0) 138 | cs.init(cs.OUT, value=1) 139 | self.spi = spi 140 | self.dc = dc 141 | self.res = res 142 | self.cs = cs 143 | self.buffer = bytearray((height // 8) * width) 144 | self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height) 145 | super().__init__(width, height, external_vcc) 146 | 147 | def write_cmd(self, cmd): 148 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 149 | self.cs.high() 150 | self.dc.low() 151 | self.cs.low() 152 | self.spi.write(bytearray([cmd])) 153 | self.cs.high() 154 | 155 | def write_framebuf(self): 156 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 157 | self.cs.high() 158 | self.dc.high() 159 | self.cs.low() 160 | self.spi.write(self.buffer) 161 | self.cs.high() 162 | 163 | def poweron(self): 164 | self.res.high() 165 | time.sleep_ms(1) 166 | self.res.low() 167 | time.sleep_ms(10) 168 | self.res.high() 169 | 170 | -------------------------------------------------------------------------------- /DHT11/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import dht 3 | import time 4 | 5 | dht11 = dht.DHT11(Pin(5)) 6 | 7 | while True: 8 | try: 9 | dht11.measure() 10 | print("dht11 humidity:", dht11.humidity()) 11 | print("dht11 temperature:", dht11.temperature()) 12 | time.sleep(0.5) 13 | except: 14 | continue 15 | -------------------------------------------------------------------------------- /DS18B20+OLED/main.py: -------------------------------------------------------------------------------- 1 | import time 2 | from machine import Pin, I2C 3 | from ssd1306 import SSD1306_I2C 4 | import onewire 5 | import ds18x20 6 | 7 | i2c = I2C(scl=Pin(5), sda=Pin(4)) 8 | oled = SSD1306_I2C(128, 64, i2c) 9 | ow = onewire.OneWire(Pin(14)) 10 | ds = ds18x20.DS18X20(ow) 11 | roms = ds.scan() 12 | 13 | oled.fill(0) 14 | oled.show() 15 | 16 | oled.pixel(0, 0, 1) 17 | oled.show() 18 | oled.pixel(127, 63, 1) 19 | oled.show() 20 | 21 | oled.text('Hello', 0, 0) 22 | oled.text('World', 50, 0) 23 | oled.text('kcqnly',80,50) 24 | oled.show() 25 | time.sleep(2) 26 | oled.fill(1) 27 | oled.show() 28 | while True: 29 | ds.convert_temp() 30 | for rom in roms: 31 | t = ds.read_temp(rom) 32 | time.sleep_ms(50) 33 | oled.fill(0) 34 | oled.text("Temperature:" ,0, 0) 35 | oled.text("%.2f" % t, 80, 20) 36 | oled.show() 37 | -------------------------------------------------------------------------------- /DS18B20+OLED/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | import time 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xa4) 10 | SET_NORM_INV = const(0xa6) 11 | SET_DISP = const(0xae) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xa0) 17 | SET_MUX_RATIO = const(0xa8) 18 | SET_COM_OUT_DIR = const(0xc0) 19 | SET_DISP_OFFSET = const(0xd3) 20 | SET_COM_PIN_CFG = const(0xda) 21 | SET_DISP_CLK_DIV = const(0xd5) 22 | SET_PRECHARGE = const(0xd9) 23 | SET_VCOM_DESEL = const(0xdb) 24 | SET_CHARGE_PUMP = const(0x8d) 25 | 26 | 27 | class SSD1306: 28 | def __init__(self, width, height, external_vcc): 29 | self.width = width 30 | self.height = height 31 | self.external_vcc = external_vcc 32 | self.pages = self.height // 8 33 | # Note the subclass must initialize self.framebuf to a framebuffer. 34 | # This is necessary because the underlying data buffer is different 35 | # between I2C and SPI implementations (I2C needs an extra byte). 36 | self.poweron() 37 | self.init_display() 38 | 39 | def init_display(self): 40 | for cmd in ( 41 | SET_DISP | 0x00, # off 42 | # address setting 43 | SET_MEM_ADDR, 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, self.height - 1, 48 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 49 | SET_DISP_OFFSET, 0x00, 50 | SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12, 51 | # timing and driving scheme 52 | SET_DISP_CLK_DIV, 0x80, 53 | SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1, 54 | SET_VCOM_DESEL, 0x30, # 0.83*Vcc 55 | # display 56 | SET_CONTRAST, 0xff, # maximum 57 | SET_ENTIRE_ON, # output follows RAM contents 58 | SET_NORM_INV, # not inverted 59 | # charge pump 60 | SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14, 61 | SET_DISP | 0x01): # on 62 | self.write_cmd(cmd) 63 | self.fill(0) 64 | self.show() 65 | 66 | def poweroff(self): 67 | self.write_cmd(SET_DISP | 0x00) 68 | 69 | def contrast(self, contrast): 70 | self.write_cmd(SET_CONTRAST) 71 | self.write_cmd(contrast) 72 | 73 | def invert(self, invert): 74 | self.write_cmd(SET_NORM_INV | (invert & 1)) 75 | 76 | def show(self): 77 | x0 = 0 78 | x1 = self.width - 1 79 | if self.width == 64: 80 | # displays with width of 64 pixels are shifted by 32 81 | x0 += 32 82 | x1 += 32 83 | self.write_cmd(SET_COL_ADDR) 84 | self.write_cmd(x0) 85 | self.write_cmd(x1) 86 | self.write_cmd(SET_PAGE_ADDR) 87 | self.write_cmd(0) 88 | self.write_cmd(self.pages - 1) 89 | self.write_framebuf() 90 | 91 | def fill(self, col): 92 | self.framebuf.fill(col) 93 | 94 | def pixel(self, x, y, col): 95 | self.framebuf.pixel(x, y, col) 96 | 97 | def scroll(self, dx, dy): 98 | self.framebuf.scroll(dx, dy) 99 | 100 | def text(self, string, x, y, col=1): 101 | self.framebuf.text(string, x, y, col) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | # Add an extra byte to the data buffer to hold an I2C data/command byte 110 | # to use hardware-compatible I2C transactions. A memoryview of the 111 | # buffer is used to mask this byte from the framebuffer operations 112 | # (without a major memory hit as memoryview doesn't copy to a separate 113 | # buffer). 114 | self.buffer = bytearray(((height // 8) * width) + 1) 115 | self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1 116 | self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height) 117 | super().__init__(width, height, external_vcc) 118 | 119 | def write_cmd(self, cmd): 120 | self.temp[0] = 0x80 # Co=1, D/C#=0 121 | self.temp[1] = cmd 122 | self.i2c.writeto(self.addr, self.temp) 123 | 124 | def write_framebuf(self): 125 | # Blast out the frame buffer using a single I2C transaction to support 126 | # hardware I2C interfaces. 127 | self.i2c.writeto(self.addr, self.buffer) 128 | 129 | def poweron(self): 130 | pass 131 | 132 | 133 | class SSD1306_SPI(SSD1306): 134 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 135 | self.rate = 10 * 1024 * 1024 136 | dc.init(dc.OUT, value=0) 137 | res.init(res.OUT, value=0) 138 | cs.init(cs.OUT, value=1) 139 | self.spi = spi 140 | self.dc = dc 141 | self.res = res 142 | self.cs = cs 143 | self.buffer = bytearray((height // 8) * width) 144 | self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height) 145 | super().__init__(width, height, external_vcc) 146 | 147 | def write_cmd(self, cmd): 148 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 149 | self.cs.high() 150 | self.dc.low() 151 | self.cs.low() 152 | self.spi.write(bytearray([cmd])) 153 | self.cs.high() 154 | 155 | def write_framebuf(self): 156 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 157 | self.cs.high() 158 | self.dc.high() 159 | self.cs.low() 160 | self.spi.write(self.buffer) 161 | self.cs.high() 162 | 163 | def poweron(self): 164 | self.res.high() 165 | time.sleep_ms(1) 166 | self.res.low() 167 | time.sleep_ms(10) 168 | self.res.high() 169 | -------------------------------------------------------------------------------- /DS18B20/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import onewire 3 | import ds18x20 4 | import time 5 | 6 | ow = onewire.OneWire(Pin(2)) 7 | ds=ds18x20.DS18X20(ow) 8 | roms=ds.scan() 9 | while True: 10 | ds.convert_temp() 11 | for rom in roms: 12 | print(ds.read_temp(rom)) 13 | time.sleep(1) 14 | -------------------------------------------------------------------------------- /LED/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin 2 | import time 3 | 4 | led = Pin(5,Pin.OUT) 5 | 6 | while True: 7 | led.on() 8 | time.sleep_ms(200) 9 | led.off() 10 | time.sleep_ms(200) 11 | 12 | 13 | -------------------------------------------------------------------------------- /OLED/main.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C 2 | from ssd1306 import SSD1306_I2C 3 | 4 | i2c = I2C(scl=Pin(5), sda=Pin(4)) 5 | oled = SSD1306_I2C(128, 64, i2c) 6 | 7 | oled.fill(1) 8 | oled.show() 9 | oled.fill(0) 10 | oled.show() 11 | 12 | oled.pixel(0, 0, 1) 13 | oled.show() 14 | oled.pixel(127, 63, 1) 15 | oled.show() 16 | 17 | oled.text('Hello', 0, 0) 18 | oled.text('World', 0, 10) 19 | oled.show() 20 | 21 | oled.invert(True) 22 | oled.invert(False) 23 | -------------------------------------------------------------------------------- /OLED/ssd1306.py: -------------------------------------------------------------------------------- 1 | # MicroPython SSD1306 OLED driver, I2C and SPI interfaces 2 | 3 | import time 4 | import framebuf 5 | 6 | 7 | # register definitions 8 | SET_CONTRAST = const(0x81) 9 | SET_ENTIRE_ON = const(0xa4) 10 | SET_NORM_INV = const(0xa6) 11 | SET_DISP = const(0xae) 12 | SET_MEM_ADDR = const(0x20) 13 | SET_COL_ADDR = const(0x21) 14 | SET_PAGE_ADDR = const(0x22) 15 | SET_DISP_START_LINE = const(0x40) 16 | SET_SEG_REMAP = const(0xa0) 17 | SET_MUX_RATIO = const(0xa8) 18 | SET_COM_OUT_DIR = const(0xc0) 19 | SET_DISP_OFFSET = const(0xd3) 20 | SET_COM_PIN_CFG = const(0xda) 21 | SET_DISP_CLK_DIV = const(0xd5) 22 | SET_PRECHARGE = const(0xd9) 23 | SET_VCOM_DESEL = const(0xdb) 24 | SET_CHARGE_PUMP = const(0x8d) 25 | 26 | 27 | class SSD1306: 28 | def __init__(self, width, height, external_vcc): 29 | self.width = width 30 | self.height = height 31 | self.external_vcc = external_vcc 32 | self.pages = self.height // 8 33 | # Note the subclass must initialize self.framebuf to a framebuffer. 34 | # This is necessary because the underlying data buffer is different 35 | # between I2C and SPI implementations (I2C needs an extra byte). 36 | self.poweron() 37 | self.init_display() 38 | 39 | def init_display(self): 40 | for cmd in ( 41 | SET_DISP | 0x00, # off 42 | # address setting 43 | SET_MEM_ADDR, 0x00, # horizontal 44 | # resolution and layout 45 | SET_DISP_START_LINE | 0x00, 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 47 | SET_MUX_RATIO, self.height - 1, 48 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 49 | SET_DISP_OFFSET, 0x00, 50 | SET_COM_PIN_CFG, 0x02 if self.height == 32 else 0x12, 51 | # timing and driving scheme 52 | SET_DISP_CLK_DIV, 0x80, 53 | SET_PRECHARGE, 0x22 if self.external_vcc else 0xf1, 54 | SET_VCOM_DESEL, 0x30, # 0.83*Vcc 55 | # display 56 | SET_CONTRAST, 0xff, # maximum 57 | SET_ENTIRE_ON, # output follows RAM contents 58 | SET_NORM_INV, # not inverted 59 | # charge pump 60 | SET_CHARGE_PUMP, 0x10 if self.external_vcc else 0x14, 61 | SET_DISP | 0x01): # on 62 | self.write_cmd(cmd) 63 | self.fill(0) 64 | self.show() 65 | 66 | def poweroff(self): 67 | self.write_cmd(SET_DISP | 0x00) 68 | 69 | def contrast(self, contrast): 70 | self.write_cmd(SET_CONTRAST) 71 | self.write_cmd(contrast) 72 | 73 | def invert(self, invert): 74 | self.write_cmd(SET_NORM_INV | (invert & 1)) 75 | 76 | def show(self): 77 | x0 = 0 78 | x1 = self.width - 1 79 | if self.width == 64: 80 | # displays with width of 64 pixels are shifted by 32 81 | x0 += 32 82 | x1 += 32 83 | self.write_cmd(SET_COL_ADDR) 84 | self.write_cmd(x0) 85 | self.write_cmd(x1) 86 | self.write_cmd(SET_PAGE_ADDR) 87 | self.write_cmd(0) 88 | self.write_cmd(self.pages - 1) 89 | self.write_framebuf() 90 | 91 | def fill(self, col): 92 | self.framebuf.fill(col) 93 | 94 | def pixel(self, x, y, col): 95 | self.framebuf.pixel(x, y, col) 96 | 97 | def scroll(self, dx, dy): 98 | self.framebuf.scroll(dx, dy) 99 | 100 | def text(self, string, x, y, col=1): 101 | self.framebuf.text(string, x, y, col) 102 | 103 | 104 | class SSD1306_I2C(SSD1306): 105 | def __init__(self, width, height, i2c, addr=0x3c, external_vcc=False): 106 | self.i2c = i2c 107 | self.addr = addr 108 | self.temp = bytearray(2) 109 | # Add an extra byte to the data buffer to hold an I2C data/command byte 110 | # to use hardware-compatible I2C transactions. A memoryview of the 111 | # buffer is used to mask this byte from the framebuffer operations 112 | # (without a major memory hit as memoryview doesn't copy to a separate 113 | # buffer). 114 | self.buffer = bytearray(((height // 8) * width) + 1) 115 | self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1 116 | self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], width, height) 117 | super().__init__(width, height, external_vcc) 118 | 119 | def write_cmd(self, cmd): 120 | self.temp[0] = 0x80 # Co=1, D/C#=0 121 | self.temp[1] = cmd 122 | self.i2c.writeto(self.addr, self.temp) 123 | 124 | def write_framebuf(self): 125 | # Blast out the frame buffer using a single I2C transaction to support 126 | # hardware I2C interfaces. 127 | self.i2c.writeto(self.addr, self.buffer) 128 | 129 | def poweron(self): 130 | pass 131 | 132 | 133 | class SSD1306_SPI(SSD1306): 134 | def __init__(self, width, height, spi, dc, res, cs, external_vcc=False): 135 | self.rate = 10 * 1024 * 1024 136 | dc.init(dc.OUT, value=0) 137 | res.init(res.OUT, value=0) 138 | cs.init(cs.OUT, value=1) 139 | self.spi = spi 140 | self.dc = dc 141 | self.res = res 142 | self.cs = cs 143 | self.buffer = bytearray((height // 8) * width) 144 | self.framebuf = framebuf.FrameBuffer1(self.buffer, width, height) 145 | super().__init__(width, height, external_vcc) 146 | 147 | def write_cmd(self, cmd): 148 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 149 | self.cs.high() 150 | self.dc.low() 151 | self.cs.low() 152 | self.spi.write(bytearray([cmd])) 153 | self.cs.high() 154 | 155 | def write_framebuf(self): 156 | self.spi.init(baudrate=self.rate, polarity=0, phase=0) 157 | self.cs.high() 158 | self.dc.high() 159 | self.cs.low() 160 | self.spi.write(self.buffer) 161 | self.cs.high() 162 | 163 | def poweron(self): 164 | self.res.high() 165 | time.sleep_ms(1) 166 | self.res.low() 167 | time.sleep_ms(10) 168 | self.res.high() 169 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-MicroPython 2 | ## 使用MicroPython开发ESP8266 3 | ![ESP8266](https://ftp.bmp.ovh/imgs/2019/12/b2c892de080ce21b.png) 4 | 5 | ## 入门推荐 6 | 1. [教程 in Github](https://github.com/lvidarte/esp8266/wiki) 7 | 2. [ESP8266固件](http://micropython.org/download#esp8266) 8 | 3. [IDE](http://docs.dfrobot.com.cn/upycraft/) 9 | 4. [快速开发指南](https://micropython.nxez.com/2019/01/04/esp8266-quick-development-guide.html) 10 | 11 | -------------------------------------------------------------------------------- /SG90/sg90.py: -------------------------------------------------------------------------------- 1 | import machine 2 | import time 3 | 4 | #设置PWM 引脚G5,频率50Hz 5 | servo = machine.PWM(machine.Pin(5), freq=50) 6 | servo.duty(75) #舵机角度的设定 25 0度 7 | #125 180度 8 | time.sleep_ms(500) 9 | servo.duty(40) 10 | time.sleep_ms(500) 11 | servo.duty(100) 12 | time.sleep_ms(500) 13 | servo.duty(75) 14 | time.sleep_ms(500) 15 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | import socket 2 | byte = 1024 3 | # 两个端口要保持一致 4 | port = 3000 5 | host = "" 6 | addr = (host, port) 7 | 8 | # 创建套接字 9 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 10 | # 绑定 11 | sock.bind(addr) 12 | print("waiting to receive messages...") 13 | 14 | while True: 15 | (data, addr) = sock.recvfrom(byte) 16 | text = data.decode('utf-8') 17 | if text == 'exit': 18 | break 19 | else: 20 | print('From{} says {!r}'.format(addr, text)) 21 | 22 | # 关闭套接字 23 | sock.close() 24 | -------------------------------------------------------------------------------- /siri+led/boot.py: -------------------------------------------------------------------------------- 1 | # This file is executed on every boot (including wake-boot from deepsleep) 2 | 3 | #import esp 4 | 5 | #esp.osdebug(None) 6 | 7 | import uos, machine 8 | 9 | #uos.dupterm(None, 1) # disable REPL on UART(0) 10 | 11 | import gc 12 | 13 | #import webrepl 14 | 15 | #webrepl.start() 16 | 17 | gc.collect() 18 | 19 | 20 | def do_connect(): 21 | import network 22 | sta_if = network.WLAN(network.STA_IF) 23 | ap_if = network.WLAN(network.AP_IF) 24 | if ap_if.active(): 25 | ap_if.active(False) 26 | if not sta_if.isconnected(): 27 | print('connecting to network...') 28 | sta_if.active(True) 29 | sta_if.connect('PandoraBox-2.4G', '104104104') #wifi的SSID和密码 30 | while not sta_if.isconnected(): 31 | pass 32 | print('network config:', sta_if.ifconfig()) 33 | return sta_if 34 | -------------------------------------------------------------------------------- /siri+led/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | LED控制 7 | 28 | 29 | 30 | 31 |
32 | 33 | 34 |
35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /siri+led/siri.py: -------------------------------------------------------------------------------- 1 | import socket, time, re 2 | from machine import Pin 3 | 4 | 5 | def do_connect(): 6 | import network 7 | sta_if = network.WLAN(network.STA_IF) 8 | ap_if = network.WLAN(network.AP_IF) 9 | if ap_if.active(): 10 | ap_if.active(False) 11 | if not sta_if.isconnected(): 12 | print('connecting to network...') 13 | sta_if.active(True) 14 | sta_if.connect('PandoraBox-2.4G', '104104104') #wifi的SSID和密码 15 | while not sta_if.isconnected(): 16 | pass 17 | print('network config:', sta_if.ifconfig()) 18 | return sta_if 19 | 20 | 21 | html = """ 22 | 23 | 24 | 25 | 26 | LED控制 27 | 48 | 49 | 50 |
51 | 52 | 53 | 54 | 55 | 56 | 76 | """ 77 | 78 | wlan = do_connect() 79 | ip = wlan.ifconfig()[0] 80 | port = 80 81 | led = Pin(0, Pin.OUT) 82 | webserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #创建套接字 83 | webserver.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #设置给定套接字选项的值 84 | #webserver.settimeout(2000) 85 | webserver.bind((ip, port)) #绑定IP地址和端口号 86 | webserver.listen(5) #监听套接字 87 | print("服务器地址:%s:%d" % (ip, port)) 88 | 89 | while True: 90 | conn, addr = webserver.accept() #接受一个连接,conn是一个新的socket对象 91 | #print("in %s" % str(addr)) 92 | request = conn.recv(1024) #从套接字接收1024字节的数据 93 | if len(request) > 0: 94 | request = request.decode() 95 | result = re.search("(.*?) (.*?) HTTP/1.1", request) 96 | if result: 97 | method = result.group(1) 98 | url = result.group(2) 99 | print(url) 100 | if method == "POST": 101 | postdata = re.search(".*?\r\n\r\n(.*)", request).group(1) 102 | if postdata: 103 | lists = postdata.split("&") 104 | payload = {} 105 | for list in lists: 106 | k, v = list.split("=") 107 | payload[k] = v 108 | #print(payload) 109 | #conn.sendall("HTTP/1.1 200 OK\nConnection: close\nServer: Esp8266\nContent-Type: text/html;charset=UTF-8\n\n") 110 | conn.send("HTTP/1.1 200 OK\r\n") 111 | conn.send("Server: Esp8266\r\n") 112 | conn.send("Content-Type: text/html;charset=UTF-8\r\n") 113 | conn.send("Connection: close\r\n") 114 | conn.send("\r\n") 115 | if url == "/": 116 | conn.sendall(html) 117 | elif url == "/led": 118 | if method == "POST": 119 | status = payload.get("status") 120 | if status == "on": 121 | led.value(0) 122 | elif status == "off": 123 | led.value(1) 124 | else: 125 | led.value(not led.value()) 126 | conn.sendall("灯亮" if led.value() == 0 else "灯灭") 127 | #conn.send(str(led.value())) 128 | conn.send("\r\n") # 发送结束 129 | else: 130 | print("not found url") 131 | else: 132 | print("no request") 133 | conn.close() 134 | #print("out %s" % str(addr)) 135 | -------------------------------------------------------------------------------- /siri+led改/main.py: -------------------------------------------------------------------------------- 1 | import socket 2 | from machine import Pin 3 | from re import search 4 | from web import do_connect 5 | 6 | html = """ 7 | 8 | 9 | 10 | 11 | LED控制 12 | 13 | 14 |

Hello World!!!

15 | 16 | 17 | """ 18 | 19 | wlan = do_connect() 20 | ip = wlan.ifconfig()[0] 21 | port = 80 22 | led = Pin(2, Pin.OUT) 23 | webserver = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 创建套接字 24 | webserver.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 设置给定套接字选项的值 25 | webserver.bind((ip, port)) # 绑定IP地址和端口号 26 | webserver.listen(5) # 监听套接字 27 | print("服务器地址:{}:{}".format(ip, port)) 28 | 29 | while True: 30 | conn, addr = webserver.accept() 31 | request = conn.recv(1024) 32 | if len(request) > 0: 33 | request = request.decode() 34 | result = search("(.*?) (.*?) HTTP/1.1", request) 35 | if result: 36 | method = result.group(1) 37 | url = result.group(2) 38 | print(url) 39 | conn.send("HTTP/1.1 200 OK\r\n") 40 | conn.send("Server: Esp8266\r\n") 41 | conn.send("Content-Type: text/html;charset=UTF-8\r\n") 42 | conn.send("Connection: close\r\n") 43 | conn.send("\r\n") 44 | if url == "/led1": 45 | led.value(1) 46 | conn.send("灯灭") 47 | elif url == "/led2": 48 | led.value(0) 49 | conn.send("灯亮") 50 | else: 51 | conn.sendall(html) 52 | conn.send("\r\n") # 发送结束 53 | else: 54 | print("not found url") 55 | else: 56 | print("no request") 57 | conn.close() 58 | -------------------------------------------------------------------------------- /siri+led改/web.py: -------------------------------------------------------------------------------- 1 | def do_connect(): 2 | import network 3 | sta_if = network.WLAN(network.STA_IF) 4 | ap_if = network.WLAN(network.AP_IF) 5 | if ap_if.active(): 6 | ap_if.active(False) 7 | if not sta_if.isconnected(): 8 | print('connecting to network...') 9 | sta_if.active(True) 10 | sta_if.connect('wifi名', '密码') # wifi的SSID和密码 11 | while not sta_if.isconnected(): 12 | pass 13 | print('network config:', sta_if.ifconfig()) 14 | return sta_if 15 | -------------------------------------------------------------------------------- /weather/main - 副本.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import re 3 | 4 | url = 'http://i.tianqi.com/index.php?c=code&a=getcode&id=55&py=hongshan' 5 | 6 | r = requests.get(url) # 发起HTTP的GET请求 7 | content = r.text 8 | 9 | print('农历') 10 | nongli = re.search(r'
  • (.*?)
  • ', content) 11 | nongli = nongli.group(1) 12 | print(nongli) 13 | 14 | print('天气') 15 | tianqi = re.search(r'height: 18px;overflow: hidden;">(.*?)', content) 16 | tianqi = tianqi.group(1) 17 | print(tianqi) 18 | 19 | print('温度') 20 | 21 | wendu = re.search( 22 | r'
    (.*?)~(.*?)
    ', 23 | content) 24 | wendua = wendu.group(1) 25 | wendub = wendu.group(2) 26 | print(wendua, wendub) 27 | 28 | print('指数') 29 | zhushui = re.search(r'height:36px">

    (.*?)

    (.*?)

    ', 30 | content) 31 | zhushuia = zhushui.group(1) 32 | zhushuib = zhushui.group(2) 33 | print(zhushuia, zhushuib) 34 | -------------------------------------------------------------------------------- /weather/main.py: -------------------------------------------------------------------------------- 1 | import time 2 | from machine import Pin 3 | 4 | 5 | MY_SSID = "PandoraBox-2.4G" 6 | MY_PASSWORD = "104104104" 7 | 8 | def do_connect(essid,password): 9 | import network 10 | wlan = network.WLAN(network.STA_IF) 11 | wlan.active(True) 12 | if not wlan.isconnected(): 13 | print('connecting to network...') 14 | wlan.connect(essid,password) 15 | time.sleep(10) # 连接有延时,睡眠10秒 16 | print('network config:', wlan.ifconfig()) 17 | return wlan.isconnected() 18 | 19 | def dis_connect(): 20 | import network 21 | wlan = network.WLAN(network.STA_IF) 22 | wlan.active(False) 23 | print('network config:', wlan.ifconfig()) 24 | 25 | do_connect(MY_SSID,MY_PASSWORD) 26 | 27 | import urequests 28 | 29 | url='http://i.tianqi.com/index.php?c=code&a=getcode&id=55&py=hongshan' 30 | 31 | r = urequests.get(url) # 发起HTTP的GET请求 32 | content = r.text 33 | 34 | 35 | import ure as re 36 | print('农历') 37 | nongli = re.search(r'
  • (.*?)
  • ', content) 38 | nongli = nongli.group(1) 39 | print (nongli) 40 | 41 | print('天气') 42 | # r'雷阵雨 这是是原文的串 43 | tianqi = re.search(r'height: 18px;overflow: hidden;">(.*?)', content) 44 | tianqi = tianqi.group(1) 45 | print (tianqi) 46 | 47 | print('温度') 48 | # r'
    27~34
    ' 这是是原文的串 49 | wendu = re.search(r'
    (.*?)~(.*?)
    ', content) 50 | wendua = wendu.group(1) 51 | wendub = wendu.group(2) 52 | print (wendua,wendub) 53 | 54 | print('指数') 55 | zhushui = re.search(r'height:36px">

    (.*?)

    (.*?)

    ', content) 56 | zhushuia = zhushui.group(1) 57 | zhushuib = zhushui.group(2) 58 | print (zhushuia,zhushuib) 59 | 60 | 61 | 62 | 63 | --------------------------------------------------------------------------------