Hi i have question...
I have node mcu esp8266, sd card adapter, and i wanna get them work like this:
Save csv file into sd card. Done...
Send that csv file to internet like i send my temperature sensor data
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
http.begin("http://someaddress.com/directory/place/file.php?ipsrc=1&temperature= "+String(temp) );
so question is..
How do i send a file from sd card????
Juraj
June 6, 2018, 11:29am
2
WiFiClient client;
if (client.connect(yourHost, 80)) {
File dataFile = SD.open("filename.csv");
client.println(F("POST /your-url HTTP/1.1"));
client.print(F("Host: "));
client.println(yourHost);
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(dataFile.size());
client.println();
client.write(dataFile);
dataFile.close();
client.stop();
}
Juraj:
while (dataFile.available()) {
client.write(dataFile.read());
}
IIRC, you can use
client.write(dataFile);
It does some proper buffering under the hood:
return 0;
}
return _write_from_source(new BufferDataSource(data, size));
}
size_t write(Stream& stream)
{
if (!_pcb) {
return 0;
}
return _write_from_source(new BufferedStreamDataSource<Stream>(stream, stream.available()));
}
size_t write_P(PGM_P buf, size_t size)
{
if (!_pcb) {
return 0;
}
ProgmemStream stream(buf, size);
return _write_from_source(new BufferedStreamDataSource<ProgmemStream>(stream, size));
}
Juraj
June 6, 2018, 11:42am
4
PieterP:
IIRC, you can use
client.write(dataFile);
It does some proper buffering under the hood:
Arduino/libraries/ESP8266WiFi/src/include/ClientContext.h at b08d282673055b4758cd73d3cd99573f619112a5 · esp8266/Arduino · GitHub
thank you, I edited the snippet
with
client.write(dataFile);
Still don't work, but with
while (dataFile.available()) {
client.write(dataFile.read());
}
works, but is not saving on server.
Post all of your code, both Arduino and server-side code.
Code works, but i need php, what php code i need, so it could save new file in same plase where is php file?
vadims0201:
so it could save new file in same plase where is php file?
That's a terrible idea. I would just send a post request containing a php reverse shell, your script places it into the hosting folder, I send a new request to that file, it executes the reverse shell, and I just logged into your server.
Don't do that.
There are counless examples online on how to save files using php, please do some research.
WiFiClient client;
if (client.connect("server ip", 80)) {
File myFile = SD.open("test.csv");
client.println(F("POST /http://temperaturetest.com/city/place/ "));
client.print(F("Host: "));
client.println("server ip");
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(myFile.size());
while (myFile.available()) {
client.write(myFile.read());
}
myFile.close();
client.stop();
}
and nothing...
just uploads to my esp8266
Juraj:
client.println(F("POST /your-url HTTP/1.1"));
client.print(F("Host: "));
client.println(yourHost);
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(dataFile.size());
client.println();
client.write(dataFile);
}
vadims0201:
client.println(F("POST /http://temperaturetest.com/city/place/ ")); // where's the protocol?
client.print(F("Host: "));
client.println("server ip");
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(myFile.size()); // where's the second new line?
while (myFile.available()) {
client.write(myFile.read());
}
Why did you change the perfectly valid HTTP request Juraj posted?
WiFiClient client;
if (client.connect("server ip", 80)) {
File myFile = SD.open("test.csv");
client.println(F("POST /http://temperaturetest.com/city/place/ HTTP/1.1 "));
client.print(F("Host: "));
client.println("server ip");
client.println(F("Connection: close"));
client.print(F("Content-Length: "));
client.println(myFile.size());
client.println();
while (myFile.available()) {
client.write(myFile.read());
}
myFile.close();
client.stop();
}
like that>?
I just need it to write to csv file on server,
It just connects to server and nothing else...
Juraj
June 7, 2018, 9:33am
12
sending the file by one byte at the time is very ineffective. use the client.write(dataFile); or use buffering
client.write(dataFile);
if i do so
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
Juraj
June 7, 2018, 9:38am
14
vadims0201:
client.write(dataFile);
if i do so
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
write(Stream&) was added to the esp8266 core 2 years ago
Please tell me what should i do, to send from sd card csv file to my server... i did every thing you told me, but still nothing.
You still didn't post your server-side script. What do you want to do? What does the server expect?
Juraj
June 7, 2018, 11:14am
18
you variable for the file is not dataFile like in my snippet so you need client.write(myFile); not client.write(dataFile).
I do not know php
Please, edit your post and use [code] /* your code here */ [/code]
tags, (using the </> button), so your code is readable.
Don't use mysqli, use PDO instead. You are sending an HTTP POST request, so why are you using the php $_GET[] variable? That contains the data of an HTTP GET request.
Do you know what your file looks like? Is your goal to have large CSV texts as database entries? I don't think so.
If someone were to open the url http://temperaturetest.com/city/place/add.php?temperature=1)%3B%20DROP%20TABLE%20`my%20temp`%3B%20( , all of your precious data would be gone forever .
Read up on SQL injection and other security guidelines before you irresponsibly throw your vulnerable PHP code on the internet.
Juraj:
you variable for the file is not dataFile like in my snippet so you need client.write(myFile); not client.write(dataFile).
I do not know php
with client.write(myFile);
nothing changed..