Save files from the Web page published to ESP32 in Access Point mode

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
mekatech
Posts: 3
Joined: Mon Jul 12, 2021 10:42 pm

Save files from the Web page published to ESP32 in Access Point mode

Post by mekatech » Mon Jul 12, 2021 11:12 pm

Hello to everyone.
I am publishing a web page at 192.168.4.1 using ESP32 in Access Point mode. The user who connects to ESP 32 is selecting a file on this page and press the Save button, I want ESP 32 to save this file.

Web page Code:
<form method="POST" action = '/dosyaYukle' enctype='multipart/form-data'>
<input type="file" id="dosyaSec">
<input name="yukle" type="submit" value="baslat">Dosya Yukle</input>
</form>

Python Code:
import … Required Libraries …
while True:
conn,addr = s.accept()
request=conn.recv(1024)
request2=str(request)
dosyaYukle=request2.find('/dosyaYukle')
if dosyaYukle==7:
with open('deneme.txt', 'wb') as f:
urequests.get('http://192.168.4.1/dosyaYukle', stream=True)
f.write(urequests.content)
f.close()

The file consists of but hollow. urequests.get('http://192.168.4.1/DosyAnayuKle') is returning the line. I haven't managed to have tried a lot of methods. Can you help me with that?

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: Save files from the Web page published to ESP32 in Access Point mode

Post by marcidy » Tue Jul 13, 2021 10:39 pm

Couple hints:

Code: Select all

urequests.get('http://192.168.4.1/dosyaYukle', stream=True)
This is making a new request from the device to the device, not to the client. This doesn't transfer the file, it's a brand new request. HTTP doesn't work this way, all data will be sent from the client to the device via requests from the client to the device. The server sends responses, not new requests.

So your code is accepting a request from the client to the device, then sending a request from the device the device.

Code: Select all

request=conn.recv(1024)
request2=str(request)
dosyaYukle=request2.find('/dosyaYukle')
The request being handled here probably contains the file. You need to process the stream of bytes as headers, which will give you information about what's being sent, and everything after the headers which will probably be the file.

Instead of receiving 1024 bytes, and only 1024 bytes, try something like:

Code: Select all

req = conn.readline()
print(req)
while True:
    header = conn.readline()
    print(header)
    if header == b"" or header == b"\r\n":
       break
You'll see all the headers received. The information in these headers will tell you how big the file is, and you can receive these bytes into a file.

Code: Select all

with open("somefile", 'wb') as f:
    f.write(conn.read(file_size))
Depending how the client sends the file, you might need to do more work than this.

Side note: if you use the code tags (</> button as an example) you'll get nicely formatted code on the forum.

mekatech
Posts: 3
Joined: Mon Jul 12, 2021 10:42 pm

Re: Save files from the Web page published to ESP32 in Access Point mode

Post by mekatech » Wed Jul 14, 2021 2:59 pm

Thank you very much for your interest and answer. I'm new to Python and try to understand the classes. Thanks for your understandable explanation.
As far as I understand "Urequests" class is not suitable for me. ESP 32 answers from sending html codes. It is also in the "Socket" class. "Network" is also creating the WiFi network in AP mode with the class.
Perceive that the user has sent a file with the form in the web browser, "F.Write (Conn.Read (File_Size))" I can save it to the file with the Method?
I have reached the webSocket class and webREPL Class for my project. I was thinking about working with these classes. However, according to the Socket Class statements, it will work on my work. I will work on it.
Thanks again.

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: Save files from the Web page published to ESP32 in Access Point mode

Post by marcidy » Wed Jul 14, 2021 5:54 pm

The socket transfers data. The protocol (http) is what tells you how to interpret the data. The socket just sees 1s and 0s.

When a client sends data over the network to the server, the server has to interpret the data. The socket makes it easy to receive 1s and 0s, and hides the details of TCP/IP or UDP from you, but the socket doesn't know what to do with the data.

Since you expect data to follow http (Hyper-Text Transfer Protocol), you decode and interpret the binary data based on that protocol.

Based on http, you expect "lines", which are groups of binary data separated by the binary representation of "\n". By splitting the binary data into these lines, you receive the first line as the request, and all subsequent lines (until an empty line) as headers. Socket.readline helps here because it will look for "\n" and split the data into groups for you, receiving one complete group at a time. This works because http explicitly uses "\n" as a grouping delimiter. And we call these "lines" for historical reasons, especially as "\n" is the "new line" control character.

You then have to inspect the headers to see how to interpret the "rest" of the data. The goal is to understand some aspects of HTTP so you understand the binary data being sent over the network to your device. Requests are part of the puzzle, since your sever is receiving interpreting requests, but the server doesn't make a new request, it interprets them.

It's like reading vs. writing. You don't need to know how to write to learn how to read, but they are both really similar. A client sends requests, and a server receives them. You have to interpret the data as an http request, but urequests doesn't do that. You need the server-side equivalent. And specifically, you need an http server that will run on micropython that can understand requests that send files.

There may be an http server out there which does what you need, but I'm not familiar with it.

mekatech
Posts: 3
Joined: Mon Jul 12, 2021 10:42 pm

Re: Save files from the Web page published to ESP32 in Access Point mode

Post by mekatech » Thu Jul 15, 2021 8:47 pm

I understand what you mean.
As far as I have seen from the videos I watched on YouTube, there is a "webSocket" class to understand the file from the client. If I'm not wrong, the "webREPL" class is also a class written for direct file transfer. C ++ is used in a class as AsyncWebServerRequest.
Thank you very much for your detailed descriptions.
When I complete my project, I will share codes under this title.

Post Reply