לימוד מיקרופייתון , esp32
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from machine import Pin import time led = Pin(2, Pin.OUT) # LED connected to pin 2 button = Pin(4, Pin.IN, Pin.PULL_UP) # button connected to pin 4 while True: if not button.value(): led.on() # Turn on LED else: led.off() # Turn off LED time.sleep_ms(50) # Sleep 50 milliseconds |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
1.importing libraries for use with ESP32 import machine, time, network 2.Connecting to Wifi sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('SSID', 'PASSWORD') 3.Reading ADC values adc = machine.ADC(0) adc_value = adc.read() 4. Controlling GPIO pins pin = machine.Pin(0, machine.Pin.OUT) pin.value(1) 5. Setting up a timer timer = machine.Timer(-1) timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=lambda t:print("Timer fired")) 6. Creating a web server import usocket def web_page(): html = """<html><head><title>ESP32 Web Server</title></head><body><h1>Hello World!</h1></body></html>""" return html s = usocket.socket(usocket.AF_INET, usocket.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)) 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() 7. Setting up I2C communication import machine, time i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4)) devices = i2c.scan() 8. Setting up UART communication import machine # setup UART uart = machine.UART(2, baudrate=115200, rx=16, tx=17, timeout=10) uart.write("Hello World!\n") data = uart.read() print(data) 9. Sending emails import smtplib sender_email = 'sender@mail.com' receiver_email = 'receiver@mail.com' password = 'password' server = smtplib.SMTP('smtp.mail.com', 587) server.starttls() server.login(sender_email, password) message = 'Subject: This is a test\n\nThis is a test email sent from ESP32 board.' server.sendmail(sender_email, receiver_email, message) server.quit() 10. Using the RTC import time, machine # set RTC rtc = machine.RTC() rtc.datetime((2020, 5, 12, 10, 30, 0, 0, 0)) # get RTC time_now = rtc.datetime() print(time_now) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
micropython esp32 lcd oled i2c exmaple code # Import required libraries from machine import I2C import time import ssd1306 # Define I2C ports, parameters # 0 - scl, 2 - sda i2c = I2C(-1, scl=22, sda=21, freq=100000) # Create the OLED display object oled_width = 128 oled_height = 32 oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c) # Clear display oled.fill(0) oled.show() # Display text oled.text('Hello, World', 10, 10) oled.show() # Delay for 3 seconds time.sleep(3) # Clear display oled.fill(0) oled.show() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
micropython esp32 uart0 , uart1 echo show code example # UART0 from machine import UART uart = UART(0, 9600) # init with given baudrate uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters # receive data while True: data = uart.read() # read a single byte if data is not None: uart.write(data) # echo back # UART1 from machine import UART uart = UART(1, 9600) # init with given baudrate uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters # receive data while True: data = uart.read() # read a single byte if data is not None: uart.write(data) # echo back |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
micropython esp32 read from uart0 , write to uart1 read from uart1 write to uart9 , show code example import machine import time # Create UART object for uart0 and uart1 uart0 = machine.UART(0, 9600) uart1 = machine.UART(1, 9600) # Create UART object for uart9 uart9 = machine.UART(9, 9600) # infinite loop while True: # If new data is available on UART0 if uart0.any(): # Read data and convert it to string data = uart0.read().decode('utf-8') # Print data print(data) # Write data to UART1 uart1.write(data) # If new data is available on UART1 if uart1.any(): # Read data and convert it to string data = uart1.read().decode('utf-8') # Write data to UART9 uart9.write(data) |