start:arduino:esp32:smart:micropython
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
start:arduino:esp32:smart:micropython [2025/02/26 14:08] – [Test Neopixels : Blanc, Rouge, Vert, Bleu] admin | start:arduino:esp32:smart:micropython [2025/02/26 17:53] (Version actuelle) – [Test LCD] admin | ||
---|---|---|---|
Ligne 258: | Ligne 258: | ||
</ | </ | ||
- | ==== Test Bouton et Neopixels | + | ==== Test Bouton et Neopixels Bp2 + incremente |
<code python exemple010.py> | <code python exemple010.py> | ||
Ligne 328: | Ligne 328: | ||
</ | </ | ||
+ | ==== Test ventilateur dans les 2 sens ==== | ||
+ | <code python exemple011.py> | ||
+ | from machine import Pin,PWM | ||
+ | import time | ||
+ | #Two pins of the motor | ||
+ | p0 = Pin(19, | ||
+ | p1 = Pin(18, | ||
+ | #INA =PWM(po, | ||
+ | #INB =PWM(p1, | ||
+ | INA = PWM(p0, freq=10000, duty_u16=8192) | ||
+ | INB = PWM(p1, freq=10000, duty_u16=8192) | ||
+ | try: | ||
+ | while True: | ||
+ | # | ||
+ | INA.duty(0) #The range of duty cycle is 0-1023 | ||
+ | INB.duty(700) | ||
+ | time.sleep(4) | ||
+ | #stop 2s | ||
+ | INA.duty(0) | ||
+ | INB.duty(0) | ||
+ | time.sleep(2) | ||
+ | #Turn clockwise for 4s | ||
+ | INA.duty(600) | ||
+ | INB.duty(0) | ||
+ | time.sleep(4) | ||
+ | #stop 2s | ||
+ | INA.duty(0) | ||
+ | INB.duty(0) | ||
+ | time.sleep(2) | ||
+ | except: | ||
+ | INA.duty(0) | ||
+ | INB.duty(0) | ||
+ | INA.deinit() | ||
+ | INB.deinit() | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Bouton1 commande ventilateur ==== | ||
+ | |||
+ | <code python exemple012.py> | ||
+ | from machine import Pin,PWM | ||
+ | import time | ||
+ | #Two pins of the motor | ||
+ | p0 = Pin(19, | ||
+ | p1 = Pin(18, | ||
+ | INA =PWM(p0, | ||
+ | INB =PWM(p1, | ||
+ | |||
+ | button1 = Pin(16, Pin.IN, Pin.PULL_UP) | ||
+ | count1 = 0 | ||
+ | |||
+ | |||
+ | try: | ||
+ | while True: | ||
+ | btnVal1 = button1.value() | ||
+ | if(btnVal1 == 0): | ||
+ | time.sleep(0.01) | ||
+ | while(btnVal1 == 0): | ||
+ | btnVal1 = button1.value() | ||
+ | if(btnVal1 == 1): | ||
+ | count1 = count1 + 1 | ||
+ | print(" | ||
+ | val1 = count1 % 2 | ||
+ | if(val1 == 1): | ||
+ | INA.duty(0) #The range of duty cycle is 0-1023 | ||
+ | INB.duty(700) | ||
+ | else: | ||
+ | INA.duty(0) | ||
+ | INB.duty(0) | ||
+ | except: | ||
+ | INA.duty(0) | ||
+ | INB.duty(0) | ||
+ | INA.deinit() | ||
+ | INB.deinit() | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Test capteur Gaz et affichage sur LCD==== | ||
+ | |||
+ | <code python exemple013.py> | ||
+ | from time import sleep_ms, ticks_ms | ||
+ | from machine import I2C, Pin | ||
+ | from i2c_lcd import I2cLcd | ||
+ | |||
+ | DEFAULT_I2C_ADDR = 0x27 | ||
+ | |||
+ | i2c = I2C(scl=Pin(22), | ||
+ | lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, | ||
+ | |||
+ | from machine import Pin | ||
+ | import time | ||
+ | gas = Pin(23, Pin.IN, Pin.PULL_UP) | ||
+ | |||
+ | while True: | ||
+ | gasVal = gas.value() | ||
+ | print(" | ||
+ | lcd.move_to(1, | ||
+ | lcd.putstr(' | ||
+ | if(gasVal == 1): | ||
+ | # | ||
+ | lcd.move_to(1, | ||
+ | lcd.putstr(' | ||
+ | else: | ||
+ | lcd.move_to(1, | ||
+ | lcd.putstr(' | ||
+ | time.sleep(0.1) #delay 0.1s | ||
+ | </ | ||
+ | |||
+ | ==== Test DHT11 et affichage sur LCD ==== | ||
+ | |||
+ | <code python exemple014.py> | ||
+ | # Import machine, time and dht modules. | ||
+ | import machine | ||
+ | import time | ||
+ | import dht | ||
+ | from time import sleep_ms, ticks_ms | ||
+ | from machine import I2C, Pin | ||
+ | from i2c_lcd import I2cLcd | ||
+ | |||
+ | #Associate DHT11 with Pin(17). | ||
+ | DHT = dht.DHT11(machine.Pin(17)) | ||
+ | |||
+ | DEFAULT_I2C_ADDR = 0x27 | ||
+ | |||
+ | i2c = I2C(scl=Pin(22), | ||
+ | lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, | ||
+ | |||
+ | while True: | ||
+ | DHT.measure() # Start DHT11 to measure data once. | ||
+ | # Call the built-in function of DHT to obtain temperature | ||
+ | # and humidity data and print them in “Shell”. | ||
+ | print(' | ||
+ | lcd.move_to(1, | ||
+ | lcd.putstr(' | ||
+ | lcd.move_to(1, | ||
+ | lcd.putstr(' | ||
+ | time.sleep_ms(1000) | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Test ouverture porte servoMoteur avec code -.- sur BP1 ==== | ||
+ | |||
+ | <code python exemple015.py> | ||
+ | # Import machine, time and dht modules. | ||
+ | from machine import Pin, PWM | ||
+ | from time import sleep_ms, ticks_ms | ||
+ | from machine import I2C, Pin | ||
+ | from i2c_lcd import I2cLcd | ||
+ | |||
+ | DEFAULT_I2C_ADDR = 0x27 | ||
+ | |||
+ | i2c = I2C(scl=Pin(22), | ||
+ | lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, | ||
+ | |||
+ | button1 = Pin(16, Pin.IN, Pin.PULL_UP) | ||
+ | button2 = Pin(27, Pin.IN, Pin.PULL_UP) | ||
+ | count = 0 | ||
+ | time_count = 0 | ||
+ | password = "" | ||
+ | correct_password = " | ||
+ | lcd.putstr(" | ||
+ | pwm = PWM(Pin(13)) | ||
+ | pwm.freq(50) | ||
+ | |||
+ | while True: | ||
+ | btnVal1 = button1.value() | ||
+ | if(btnVal1 == 0): | ||
+ | sleep_ms(10) | ||
+ | while(btnVal1 == 0): | ||
+ | time_count = time_count + 1 #Start counting the pressed time of the button | ||
+ | sleep_ms(200) | ||
+ | btnVal1 = button1.value() | ||
+ | if(btnVal1 == 1): | ||
+ | count = count + 1 | ||
+ | print(count) | ||
+ | print(time_count) | ||
+ | if(time_count > 3): #If the pressed time of the button is more than 200*3ms,add" | ||
+ | lcd.clear() | ||
+ | # | ||
+ | password = password + " | ||
+ | else: | ||
+ | lcd.clear() | ||
+ | password = password + " | ||
+ | lcd.putstr(' | ||
+ | time_count = 0 | ||
+ | | ||
+ | btnVal2 = button2.value() | ||
+ | if(btnVal2 == 0): | ||
+ | if(password == correct_password): | ||
+ | lcd.clear() | ||
+ | lcd.putstr(" | ||
+ | pwm.duty(128) | ||
+ | password = "" | ||
+ | sleep_ms(1000) | ||
+ | else: #If the password is wrong | ||
+ | lcd.clear() | ||
+ | lcd.putstr(" | ||
+ | pwm.duty(25) | ||
+ | sleep_ms(2000) | ||
+ | lcd.clear() | ||
+ | lcd.putstr(" | ||
+ | password = "" | ||
+ | |||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Test connection Wifi ==== | ||
+ | |||
+ | <code python exemple016.py> | ||
+ | import time | ||
+ | import network #Import network module | ||
+ | |||
+ | #Enter correct router name and password | ||
+ | ssidRouter | ||
+ | passwordRouter = ' | ||
+ | |||
+ | def STA_Setup(ssidRouter, | ||
+ | print(" | ||
+ | sta_if = network.WLAN(network.STA_IF) #Set ESP32 in Station mode | ||
+ | if not sta_if.isconnected(): | ||
+ | print(' | ||
+ | #Activate ESP32’s Station mode, initiate a connection request to the router | ||
+ | #and enter the password to connect. | ||
+ | sta_if.active(True) | ||
+ | sta_if.connect(ssidRouter, | ||
+ | #Wait for ESP32 to connect to router until they connect to each other successfully. | ||
+ | while not sta_if.isconnected(): | ||
+ | pass | ||
+ | #Print the IP address assigned to ESP32 in “Shell”. | ||
+ | print(' | ||
+ | print(" | ||
+ | |||
+ | try: | ||
+ | STA_Setup(ssidRouter, | ||
+ | except: | ||
+ | sta_if.disconnect() | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Test LCD ==== | ||
+ | |||
+ | <code python exemple017.py> | ||
+ | from time import sleep_ms, ticks_ms | ||
+ | from machine import I2C, Pin | ||
+ | from i2c_lcd import I2cLcd | ||
+ | |||
+ | DEFAULT_I2C_ADDR = 0x27 | ||
+ | |||
+ | i2c = I2C(scl=Pin(22), | ||
+ | lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, | ||
+ | |||
+ | lcd.move_to(1, | ||
+ | lcd.putstr(' | ||
+ | lcd.move_to(1, | ||
+ | lcd.putstr(' | ||
+ | |||
+ | # The following line of code should be tested | ||
+ | # using the REPL: | ||
+ | |||
+ | # 1. To print a string to the LCD: | ||
+ | # lcd.putstr(' | ||
+ | # 2. To clear the display: | ||
+ | # | ||
+ | # 3. To control the cursor position: | ||
+ | # lcd.move_to(2, | ||
+ | # 4. To show the cursor: | ||
+ | # lcd.show_cursor() | ||
+ | # 5. To hide the cursor: | ||
+ | # | ||
+ | # 6. To set the cursor to blink: | ||
+ | # | ||
+ | # 7. To stop the cursor on blinking: | ||
+ | # | ||
+ | # 8. To hide the currently displayed character: | ||
+ | # | ||
+ | # 9. To show the currently hidden character: | ||
+ | # | ||
+ | # 10. To turn off the backlight: | ||
+ | # | ||
+ | # 11. To turn ON the backlight: | ||
+ | # | ||
+ | # 12. To print a single character: | ||
+ | # | ||
+ | # 13. To print a custom character: | ||
+ | #happy_face = bytearray([0x00, | ||
+ | # | ||
+ | # | ||
+ | </ |
/home/chanteri/www/fablab37110/data/attic/start/arduino/esp32/smart/micropython.1740575314.txt.gz · Dernière modification : 2025/02/26 14:08 de admin