WEB page from SD Card on RP2040

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

WEB page from SD Card on RP2040

Post by blakebr »

Hello,

I am trying to display web pages that are stored on an SD card connected to an RP2040 with WiFi.
I can store data to the SD card just fine.
I can get to a web page in 'http://192.168.1.188'. It displays 'index.html' that is in the '/static folder' just fine
It has a link to 'http://192.168.1.188/SD_CARD/index.html' and 'http://192.168.1.188/SD_CARD/' They both display 'This page isn't working'
If I stick the SD card into my PC the index.html on the SD Card loads fine and links to others on the SD card just fine.

I tried storing data from code.py into '/static/foobar.html', but that is not allowed. When I try I get a 'Write Protected" error.

What magic must I invoke to make this happen?
Must I mount the SD_CARD to the /root/ directory in a way that I can write to it and read from it as a web pages?
Should I be referencing SD_CARD by a different name?

Bruce

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: WEB page from SD Card on RP2040

Post by mikeysklar »

Bruce,

Can you post some of the code in CODE brackets that you are using for on the RP2040?

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Hello Mikey,

Thank you for responding.
This is how I define where to write the files.

Code: Select all

    try:
        SD_CS   = board.D2 # Use any pin that is not taken by SPI
        SD_CARD = "/SD_CARD"
        # Connect to the card and mount the filesystem.
        spi     = busio.SPI(board.SCK, board.MOSI, board.MISO)
        cs      = digitalio.DigitalInOut(SD_CS)
        sdcard  = adafruit_sdcard.SDCard(spi, cs)
        vfs     = storage.VfsFat(sdcard)
        storage.mount(vfs, SD_CARD)
    except:
        print("***************************** Exception Rebbot *****************************")
        print("Failed to configure for SD Card")
        time.sleep(10)
        supervisor.reload()
This is a quick test I ran to write to the /static subdirectory in the root directory of the RP2040.
The two comment lines are what I get on the serial console.
I thought if I could write to /static I would have a solution. Nope, write protected.

Code: Select all

    file = open("/static/test.txt", "w")
    file.write("Hello World!")
    file.close

#    File "code.py", line 133, in <module>
#    OSError: [Errno 30] Read-only filesystem
This is the code that writes the local weather of the ZIP code retrieved from OpenWeatherMap.com.
SD_CARD is set to the path to the SD card. 26 weather forecasts by ZIP code are written to 26 different files.

Code: Select all

def store_to_SD_card():
    file_name = (SD_CARD + "/" + ZIP + ".html")
    print("File Name: " + file_name)
    try:
        with open(file_name, 'w') as file: # a=append, w=write
            file.write("<html>")
            file.write("<br>")
            file.write("System  Time: {}/{}/{} at {}:{}:{} UTC\r\n".format(now.tm_mon,now.tm_mday,now.tm_year,now.tm_hour,now.tm_min,now.tm_sec))
            file.write("<br>")
            file.write(city_name + ", " + ZIP + "\r\n")
            file.write("<br>")
            file.write(main_text + ", " + description + "\r\n")
            file.write("<br>")
            file.write("%.2f\t Kelvin\r\n" % tempK)
            file.write("<br>")
            file.write("%.3f\t°Celsius\r\n" % tempC)
            file.write("<br>")
            file.write("%.3f\t°Celcius    - Dew Point \r\n" % DewPoint)
            file.write("<br>")
            file.write("%.3f\t°Celcius    - Feels Like\r\n" % Feel)
            file.write("<br>")
            file.write("%.3f\t°Fahrenheit\r\n" % tempF)
            file.write("<br>")
            file.write("%.3f\t°Fahrenheit - Dew Point \r\n" % CtoF(DewPoint))
            file.write("<br>")
            file.write("%.3f\t°Fahrenheit - Feels Like\r\n" % CtoF(Feel))
            file.write("<br>")
            file.write("%d\t Millibars\r\n" % Pres)
            file.write("<br>")
            file.write("%d%%\t Humidity\r\n" % Humi)
            file.write("</html>")
            file.close()
    except:
        print("***************************** Exception Rebbot *****************************")
        print("Failed to open file on SD Card")
        supervisor.reload()
This is how I setup the web server.

Code: Select all

wsgiServer = server.WSGIServer(80, application=web_app)
print("open this IP in your browser: ", esp.pretty_ip(esp.ip_address))
This is the 'index.html' file has been written to '/static'. It displays just fine when I open Chrome to 192.168.1.188. When I click either link, disappointment.

Code: Select all

<html>
Hello<br>
Hello<br>
<br> <font size=+2><a href="/SD_CARD/index.html">Weather</a></font><br><br>
<br> <font size=+2><a href="/SD_CARD/">Weather</a></font><br><br>
Hello<br>
Hello<br>
</html>
What am I missing?
Thanks for looking at my code.
Bruce

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: WEB page from SD Card on RP2040

Post by mikeysklar »

Bruce,

I'm hoping this will be as simple as a relative path issue.

Does it help at all if you only reference index.html without a full path from /SDCARD.

Code: Select all

<br> <font size=+2><a href="index.html">Weather</a></font><br><br>

Code: Select all

<br> <font size=+2><a href="./index.html">Weather</a></font><br><br>

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Mike,

Thanks for your reply.
I tried the index.html given below. Only #5 and #7 worked, but they took me back to the original index.html file. None took me to the index.html file that is on the SD Card.

What this tells me is that the index.html is being properly sent to the browser, and it is able to interpret it.
What we don.t know is if the SD Card is mounted to the /root/ or / directory or someplace else. Where that 'else' is, is a mystery.

Code: Select all

<html>
<center>
Hello<br>
<br> <font size=+2><a href="/SD_CARD/index.html">Weather 1</a></font><br><br>
<br> <font size=+2><a href="/SD_CARD/">Weather 2</a></font><br><br>
<br>
<br> <font size=+2><a href="./SD_CARD/index.html">Weather 3</a></font><br><br>
<br> <font size=+2><a href="./SD_CARD/">Weather 4</a></font><br><br>
<br>
<br> <font size=+2><a href="./index.html">Weather 5</a></font><br><br>
<br> <font size=+2><a href="./SD_CARD/">Weather 6</a></font><br><br>
<br>
<br> <font size=+2><a href="../index.html">Weather 7</a></font><br><br>
<br> <font size=+2><a href="../SD_CARD/">Weather 8</a></font><br><br>
Hello<br>
</center>
</html>

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: WEB page from SD Card on RP2040

Post by mikeysklar »

Since #5 worked maybe it does not matter so much about where the index.html is located, but that all you can see is the current folder that the index.html is located in. If you avoid adding a full path names you should be able to access index.html and any sub-folder content without issue.

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

The code below behaves the same as #5 and #7. It shows the index.html in /static.

Code: Select all

<br> <font size=+2><a href="index.html">Weather 9</a></font><br><br>

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Mike,

After I understand what is said; in "CircuitPython Storage" I may have found the answer.

It looks like I must also have a "boot.py" file on CIRCUITPY that looks at a data pin and makes CURCUITPY writable by either the computer or code.py but not both.

I would need to create a boot.py file that with no jumper grounding the data pin I could write and edit code. With the data pin pulled high I could write to the CIRCUITPY file system but not be able to edit code.

The examples do not explicitly mention the RP2040 so I would need to play with a microcontroller that I can afford to corrupt and use the wipe routines on it.

Bruce

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Mike,

After a lot of fits-and-starts I finally got it working. The "CircuitPython Storage page may need to be to be updated.
It does not account for the Raspberry Pi Pico, or the Nano RP2040 Connect. Probably other RP2040 platforms.

There is still one issue. The data written to the system memory is not made available when the file is closed. I have the following at the end of writing the data structure.

Code: Select all

            file.write("<br>")
            file.write("%d%%\t Humidity\r\n" % Humi)
            file.write("</html>")
            file.flush()
            time.sleep(0.05)
            file.close()
It becomes available if I do a button reset, a Ctrl-C, Ctrl-D reboot, or remove power. Is there a way to remove this data block?

Bruce

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Mike,

I let the project run overnight. Without me touching anything the files were updated at 00:57:35 UTC and then again at 09:57:33 UTC. I live in the -4:00 time zone so current weather that is 5 to 6 hours old is not current.

The only solution I have thought of is to have the project reboot after each cycle of 26 locations, or on the hour, or.... That solution sucks.

Bruce

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: WEB page from SD Card on RP2040

Post by mikeysklar »

Bruce,

Good work getting things this far.

Now since the controller seems to be locking up roughly every day maybe we can work on that.

Let's start with making sure you are running CircuitPython 7 with the current libs.

Which model of RP2040 are you using Feather / Pico / QtPy etc.?

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Mike,

I am running 7.0.0, see boot_out.txt below. The 'boot.py Finis' is my add to tell boot.py finished.

Code: Select all

Adafruit CircuitPython 7.0.0 on 2021-09-20; Arduino Nano RP2040 Connect with rp2040
Board ID:arduino_nano_rp2040_connect
boot.py output:
	boot.py Finis
I am running this on a Feather with AirLift, and Nano Connect. As far as my needs they are functionally equivalent.

IMHO, There is an issue between when data is written to a buffer for the flash memory and when it is written to the actual flash memory. I thought file.close() would flush the buffer. When that didn't, I added file.flush() to force a flush. That didn't work.

The fact that the buffer is written to the flash memory at some interval indicates that it is being done. I just need to know how to trigger that magic function without pushing the RESET button or disrupting power.

Bruce

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: WEB page from SD Card on RP2040

Post by mikeysklar »

Bruce,

The Feather board + Airlift you are using. Is that also connected to a PC?

There is some discussion in one of our CircuitPython github repo on issue# 111 about FAT12 filesystems not flushing.

https://github.com/adafruit/circuitpython/issues/111

It also looks like there are some things to try:

1) bootloader update
2) 'sync' from a linux machine
3) autoreset.

add to boot.py

Code: Select all

 import samd
samd.disable_autoreset()
print("*** Disabling auto-reset.")

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Mike,

The #111 issue looks to be an editor corrupting the file system by making partial writes or something. The file system is not corrupted on my machines.

I get the delayed writes to the flash memory from code.py when I am connected to my windows 10 PC as when I am connected to a 60 watt power supply.

I will add the text you suggested tomorrow. It is 11 PM here in Maryland. Time to turn the brain off and go unconscious for a while.

Thank you for all your help.
Bruce

User avatar
blakebr
 
Posts: 956
Joined: Tue Apr 17, 2012 6:23 pm

Re: WEB page from SD Card on RP2040

Post by blakebr »

Mike,

Code: Select all

 1) bootloader update
2) 'sync' from a linux machine
3) autoreset.
1) How do I update the bootloader? Which bootloader are you referencing, the RP2040 or the AirLift?
2) ‘sync’ what from a Linux machine? I only have Raspberry Pi Linux machines.
3) I will test the autoreset code tomorrow.

Bruce

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”