Outils pour utilisateurs

Outils du site


start:arduino:esp32:smart:micropython

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
start:arduino:esp32:smart:micropython [2025/02/26 17:38] – [Bouton1 commande ventilateur] adminstart:arduino:esp32:smart:micropython [2025/02/26 17:53] (Version actuelle) – [Test LCD] admin
Ligne 440: Ligne 440:
 </code> </code>
  
 +==== 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), sda=Pin(21), freq=400000) 
 +lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
 +
 +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('temperature:',DHT.temperature(),'℃','humidity:',DHT.humidity(),'%')
 +    lcd.move_to(1, 0)
 +    lcd.putstr('T= {}'.format(DHT.temperature()))
 +    lcd.move_to(1, 1)
 +    lcd.putstr('H= {}'.format(DHT.humidity()))
 +    time.sleep_ms(1000)
 +
 +
 +</code>
 +
 +==== 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), sda=Pin(21), freq=400000) 
 +lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
 +
 +button1 = Pin(16, Pin.IN, Pin.PULL_UP)
 +button2 = Pin(27, Pin.IN, Pin.PULL_UP)
 +count = 0
 +time_count = 0
 +password = ""   #Enter password
 +correct_password = "-.-"  #Correct password
 +lcd.putstr("Enter password")
 +pwm = PWM(Pin(13))  
 +pwm.freq(50)
 +
 +while True:
 +    btnVal1 = button1.value()  # Read the value of button 1
 +    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)                #The time is 200ms cumulative
 +            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"-" to  password
 +                    lcd.clear()
 +                    #lcd.move_to(1, 1)
 +                    password = password + "-"
 +                else:
 +                    lcd.clear()
 +                    password = password + "."  #Otherwise add "."
 +                lcd.putstr('{}'.format(password)) 
 +                time_count = 0
 +                
 +    btnVal2 = button2.value()
 +    if(btnVal2 == 0):
 +        if(password == correct_password):  #If the password is correct
 +            lcd.clear()
 +            lcd.putstr("open")
 +            pwm.duty(128)  #Open the door
 +            password = ""  #Remove the password
 +            sleep_ms(1000)
 +        else:              #If the password is wrong
 +            lcd.clear()
 +            lcd.putstr("error")
 +            pwm.duty(25)  #Close the door
 +            sleep_ms(2000)
 +            lcd.clear()
 +            lcd.putstr("enter again")
 +            password = ""  #Remove the password
 +
 +
 +
 +</code>
 +
 +==== Test connection Wifi ====
 +
 +<code python exemple016.py>
 +import time
 +import network #Import network module
 +
 +#Enter correct router name and password
 +ssidRouter     = 'XXXXXXXXXXXXX' #Enter the router name
 +passwordRouter = 'xxxxxxxxxxxxx' #Enter the router password
 +
 +def STA_Setup(ssidRouter,passwordRouter):
 +    print("Setup start")
 +    sta_if = network.WLAN(network.STA_IF) #Set ESP32 in Station mode
 +    if not sta_if.isconnected():
 +        print('connecting to',ssidRouter)
 +#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,passwordRouter)
 +#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('Connected, IP address:', sta_if.ifconfig())
 +    print("Setup End")
 +
 +try:
 +    STA_Setup(ssidRouter,passwordRouter)
 +except:
 +    sta_if.disconnect()
 +
 +</code>
 +
 +==== 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), sda=Pin(21), freq=400000) 
 +lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
 +
 +lcd.move_to(1, 0)
 +lcd.putstr('Bonjour')
 +lcd.move_to(1, 1)
 +lcd.putstr('Au Castellab')
 +
 +# The following line of code should be tested
 +# using the REPL:
 +
 +# 1. To print a string to the LCD:
 +#    lcd.putstr('Hello world')
 +# 2. To clear the display:
 +#lcd.clear()
 +# 3. To control the cursor position:
 +# lcd.move_to(2, 1)
 +# 4. To show the cursor:
 +# lcd.show_cursor()
 +# 5. To hide the cursor:
 +#lcd.hide_cursor()
 +# 6. To set the cursor to blink:
 +#lcd.blink_cursor_on()
 +# 7. To stop the cursor on blinking:
 +#lcd.blink_cursor_off()
 +# 8. To hide the currently displayed character:
 +#lcd.display_off()
 +# 9. To show the currently hidden character:
 +#lcd.display_on()
 +# 10. To turn off the backlight:
 +#lcd.backlight_off()
 +# 11. To turn ON the backlight:
 +#lcd.backlight_on()
 +# 12. To print a single character:
 +#lcd.putchar('x')
 +# 13. To print a custom character:
 +#happy_face = bytearray([0x00, 0x0A, 0x00, 0x04, 0x00, 0x11, 0x0E, 0x00])
 +#lcd.custom_char(0, happy_face)
 +#lcd.putchar(chr(0))
 +</code>
/home/chanteri/www/fablab37110/data/attic/start/arduino/esp32/smart/micropython.1740587891.txt.gz · Dernière modification : 2025/02/26 17:38 de admin