Hello
My question is simple
How to read a text file from an internet site ? An example should be great.
With arduino mega and ethernet schield
Thanks
Hello
My question is simple
How to read a text file from an internet site ? An example should be great.
With arduino mega and ethernet schield
Thanks
A simple example.
//zoomkat 5-13-13
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;
String readString, readString1;
int x=0; //for counting line feeds
char lf=10; //line feed character
//////////////////////
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Better client test 5/13/13"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
client.println("Host: web.comporium.net");
client.println("Connection: close"); //close 1.1 persistent connection
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints raw feed for testing
if (c==lf) x=(x+1); //counting line feeds
if (x==9) readString += c; //building readString
}
Serial.println();
Serial.println();
Serial.print("Current data row:" );
Serial.print(readString); //the 10th line captured
Serial.println();
readString1 = (readString.substring(0,8)); //extracting "woohoo!"
Serial.println();
Serial.print("How we feeling?: ");
Serial.println(readString1);
Serial.println();
Serial.println("done");
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
readString = ("");
readString1 = ("");
client.stop(); //stop client
}
hello
i try your example, but it doesn't work
just one question, how can i read what is inside my file.txt on my internet site and load it in a variable ?
client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
client.println --> is for writing, i don't understand how to read into a variable ?
in your example, arduino is a webclient, i would like to create a webserver with arduino and read an external file in order to display it in a html page.
Thanks
regards
Bend94:
i would like to create a webserver with arduino and read an external file in order to display it in a html page.
From the point of view of reading the file, if you're fetching the file via HTTP then the Arduino is acting as a client. (It may also be acting as a server from the point of view of accepting HTTP requests and serving out responses, but that aspect isn't important to this problem.)
You need to look at the examples of web clients. They show you how to submit an HTTP request and get back an HTTP response. The response consists of a stream of text received over a socket connection. You sketch would need to read the response, separate out the part containing the file contents you want from the rest of the HTTP response, and store that part in a string variable. The parsing and separation is not hard to do and I wouldn't expect to have any problem finding examples of web clients that do it. The example that Zoomkat posted might well do it - I haven't checked.
The below discussion has some ways to get data from an internet site.
http://forum.arduino.cc/index.php?topic=214331.msg1570083#msg1570083
Hello,
I have question about how can I import data from arduino Uno
Thank you