JS libraries on flash memory

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Semicolonist
Posts: 7
Joined: Sun Mar 08, 2020 12:39 pm

JS libraries on flash memory

Post by Semicolonist » Sun Mar 08, 2020 1:04 pm

My ESP32 works as AP. Devices which are connected to this network can view a website written in html + javascript. But I need to use JS library (Chart.min.js in this case, 173kb) on this website and I don't know how to do that. I tried to send it to the flash and add it to a website by <script src="Chart.min.js"> but I get errors during uploading files to ESP.
So how to use javascript libraries on my website on ESP working as AP without internet connection?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: JS libraries on flash memory

Post by dhylands » Sun Mar 08, 2020 2:28 pm

I’m going to guess that the problem is that there are two dots in your file name. Try renaming it to remove the middle dot.

Semicolonist
Posts: 7
Joined: Sun Mar 08, 2020 12:39 pm

Re: JS libraries on flash memory

Post by Semicolonist » Sun Mar 08, 2020 2:47 pm

I tried, but I don't see any difference:

Code: Select all

[1/1] Writing file Chartmin.js (173kb) with compression
Failed to write file, trying again...
Failed to write file, trying again...
Filecheck failed
Upload done, resetting board...

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: JS libraries on flash memory

Post by Roberthh » Sun Mar 08, 2020 2:56 pm

Which tools do you use for upload?
Is there sufficient space available in the ESP32 file system?
Did you try to specify the full path name for the target?

Semicolonist
Posts: 7
Joined: Sun Mar 08, 2020 12:39 pm

Re: JS libraries on flash memory

Post by Semicolonist » Sun Mar 08, 2020 3:02 pm

I use Visual Studio Code with PyMakr extension. Right now I use 13kb of ESP flash memory for my files. I have 4Mb flash ESP32 edition

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: JS libraries on flash memory

Post by Roberthh » Sun Mar 08, 2020 3:12 pm

Try to use something more basic for file upload, like a dedicated ftp client (ft, filezilla) or tools like ampy, rshell, pyboard.py, ..
VSC + pymakr is IMHO a constant source of trouble. With every version of VSC - like once a week - pymakr may fail again.

Semicolonist
Posts: 7
Joined: Sun Mar 08, 2020 12:39 pm

Re: JS libraries on flash memory

Post by Semicolonist » Thu Mar 12, 2020 1:40 pm

Thank you it really helped. Now I don't have any errors during file transfer which was really annoying. But I still have a problem with usage of this library on my website. Here's part of code I'm using:

Code: Select all

import network
import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 8000))
s.listen(10)
while True:
    conn, addr = s.accept()
    FullRequest = str(conn.recv(1024))
    conn.sendall('HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type:text/html\r\n\r\n')
    conn.sendall(str(PageHTML()))   #PageHTML returns a string with html code
    conn.sendall('\n')
    conn.close()
In html code I have

Code: Select all

<script src="Chartmin.js"></script>
When web browser ask for Chartmin.js it gets PageHTML string text in return. So index page and Chartmin.js in browser sources looks the same. I understand why it happening like that, but I don't know how do it correctly. Should I handle a special GET request and send this library content as string or something? What's the best way to use this library on website?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: JS libraries on flash memory

Post by jimmo » Mon Mar 16, 2020 4:38 am

Semicolonist wrote:
Thu Mar 12, 2020 1:40 pm
When web browser ask for Chartmin.js it gets PageHTML string text in return. So index page and Chartmin.js in browser sources looks the same. I understand why it happening like that, but I don't know how do it correctly. Should I handle a special GET request and send this library content as string or something? What's the best way to use this library on website?
You should use one of the web server libraries rather that rolling your own with sockets. I like microdot, but there are lots of options. https://github.com/miguelgrinberg/microdot

Importantly, it has a way to directly send a file from the flash -- see examples/gpio.py: "response = send_file('gpio.html')".

But yes, you are correct, your program needs to look at the request path (i.e. GET /path/foo.bar HTTP/1.1) and handle that. Using a library will make this much easier.

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: JS libraries on flash memory

Post by mcauser » Fri Mar 20, 2020 8:59 am

To stop the client browser from making subsequent requests, add some cache headers to your large static scripts

Post Reply