Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1241

Python • too slow in refreshing

$
0
0
I have the following code to measure light intensity using a bh1750 sensor and send it to my laptop by wifi

However it refreshes the reading very slowly somewhere between 7 to 9 seconds.

is there anyway to speed up the time between readings?


Code:

from machine import Pin, I2Cfrom utime import sleepfrom bh1750 import BH1750import networkimport socketi2c0_sda = Pin(16)i2c0_scl = Pin(17)i2c0 = I2C(0, sda=i2c0_sda, scl=i2c0_scl, freq=400000)ssid = '******' #Your network namepassword = '******' #Your WiFi passwordprint("Scanning I2C bus...")device =i2c0.scan()print(device)                                                                                                                  bh1750=BH1750(0x23, i2c0)def connect():    #Connect to WLAN    wlan = network.WLAN(network.STA_IF)    wlan.active(True)    wlan.connect(ssid, password)    while wlan.isconnected() == False:        print('Waiting for connection...')        sleep(1)    ip = wlan.ifconfig()[0]    print(f'Connected on {ip}')    return ipdef open_socket(ip):    # Open a socket    address = (ip, 80)    connection = socket.socket()    connection.bind(address)    connection.listen(1)    return connectiondef webpage(reading):    #Template HTML    html = f"""            <!DOCTYPE html>            <html>            <head>            <title>Pico W BME280 Weather Station</title>            <meta http-equiv="refresh" content="10">            </head>            <body>            <p>{reading}</p>            </body>            </html>            """    return str(html)def serve(connection):    #Start a web server     while True:        print(bh1750.measurement)        reading = (bh1750.measurement)        sleep(.5)     client = connection.accept()[0]        request = client.recv(1024)        request = str(request)               html = webpage(reading)        client.send(html)        client.close()try:    ip = connect()    connection = open_socket(ip)    serve(connection)except KeyboardInterrupt:    machine.reset()    

Statistics: Posted by sandy70 — Fri Feb 28, 2025 2:20 pm



Viewing all articles
Browse latest Browse all 1241

Trending Articles