Below is the code that print "Hello World from Pico W" on a file in SDCard interfaced with Pico W. Then a server is started to read/download that file from SD card through html page via WiFi. Logically, it should download the complete file present in Sd card but it only show 6.25kB sized file (from starting of readings). I'm unable to handle the error.
There is no code error nor hardware but my assumption is that this is due to memory constraint of Pico W. I don't know completely how this data file transferred to web page (through SRAM or Flash). Need guidance!
There is no code error nor hardware but my assumption is that this is due to memory constraint of Pico W. I don't know completely how this data file transferred to web page (through SRAM or Flash). Need guidance!
Code:
import machinefrom machine import Pin, SPIimport os, _threadimport sdcard, uos, network, socket,utime as timeimport gc, sys#PINS Defined for SD cardtry: sd = sdcard.SDCard(spi, cs) os.mount(sd, "/sd")except Exception as e: print("Error mounting SD card:", e)file_name = "/sd/Hello.txt"def write_to_sd(): count = 0 while True: try: with open(file_name, "a") as file: file.write(f"Hello World from Raspberry Pico W ({count})\n") count += 1 gc.collect() time.sleep(1) except Exception as e: print("Error writing to SD card:", e) sys.exit()# Start a thread to write to the SD card_thread.start_new_thread(write_to_sd, ())# Connect to WiFissid = 'SSID'password = 'Password'wlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(ssid, password)while not wlan.isconnected(): passprint("Connected to WiFi")# Create a web serveraddr = socket.getaddrinfo('192.168.1.115', 80)[0][-1]s = socket.socket()s.bind(addr)s.listen(5)print("Listening on", addr)# HTML for the web pagehtml = """<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Data Logging</title> <style> body { background-color: skyblue; text-align: center; padding: 50px; } .button { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } </style></head><body> <h1>Data Logging</h1> <a href="/download" class="button" download="{file_name}">Download Data</a></body></html>"""while True: try: cl, addr = s.accept() print('Client connected from', addr) request = cl.recv(2048) request = str(request) if "GET /download" in request: cl.send('HTTP/1.0 200 OK\r\nContent-Disposition: attachment; filename="hello.txt"\r\nContent-Type: text/plain\r\n\r\n') with open("/sd/hello.txt", "r") as file: cl.send(file.read()) else: cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n') cl.send(html) cl.close() gc.collect() except Exception as e: print("Error handling request:", e)
Statistics: Posted by leansol1 — Mon Jul 15, 2024 11:20 am — Replies 1 — Views 28