Hallo Leute
Ich brauche wieder einmal eure Hilfe.
Im folgenden Code wird eine index.htm Seite von der SD-Karte geladen, und an den Browser gesendet.
nun meine Frage: kann ich in der index.htm Seite einen Link reinschreiben der eine andere Seite von der SD-Karte im Browser zur Anzeige bringt. Ein weiterer Link wäre nicht schlecht damit man wieder zur index.htm Seite wieder zurück kommt.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x80, 0x3F, 0x5D, 0xF8, 0x86, 0xDB };
IPAddress ip(10, 0, 0, 6); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[4] = {0}; // stores the states of the LEDs
int inputWZ=22;
int outputWZ=23;
int inputEZ=24;
int outputEZ=25;
int inputSZ=26;
int outputSZ=27;
int togglestateSZ=0;
int togglestateEZ=0;
int togglestateWZ=0;
float temp=23.6;
void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
// switches
pinMode(inputWZ, INPUT);
pinMode(inputEZ, INPUT);
pinMode(inputSZ, INPUT);
// LEDs
pinMode(outputWZ, OUTPUT);
pinMode(outputEZ, OUTPUT);
pinMode(outputSZ, OUTPUT);
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
In deinem Code fehlt das wesentliche:
Wann/wie wird die Datei index.htm von der SD Karte gelesen und an einen client gesendet, wenn der danach fragt?
Da würde die Erweiterung eingebaut, statt dessen eine andere Datei zu lesen und zu senden, wenn danach gefragt würde. Optimalerweise prüfen, ob es diese auf der SD Karte gibt, und falls nicht, statt dessen eine 404 Antwort zu generieren.
Noch optimalerer wäre natürlich, in solche Seiten auch Echtzeitdaten dazwischenzumischen, damit das Ganze überhaupt einen Sinn ergibt. (Warum einen Arduino mit sowas quälen?)
das ist der gesamte Code...muss noch Überarbeitet werden
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x80, 0x3F, 0x5D, 0xF8, 0x86, 0xDB };
IPAddress ip(10, 0, 0, 6); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[4] = {0}; // stores the states of the LEDs
int inputWZ=22;
int outputWZ=23;
int inputEZ=24;
int outputEZ=25;
int inputSZ=26;
int outputSZ=27;
int togglestateSZ=0;
int togglestateEZ=0;
int togglestateWZ=0;
float temp=23.6;
void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
// switches
pinMode(inputWZ, INPUT);
pinMode(inputEZ, INPUT);
pinMode(inputSZ, INPUT);
// LEDs
pinMode(outputWZ, OUTPUT);
pinMode(outputEZ, OUTPUT);
pinMode(outputSZ, OUTPUT);
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// limit the size of the stored received HTTP request
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
// remainder of header follows below, depending on if
// web page or XML page is requested
// Ajax request - send XML file
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
SetLEDs();
// send XML file containing input states
XML_response(client);
}
else { // web page request
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
}
// display received HTTP request on serial port
Serial.print("------------------------------");
Serial.print(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// checks if received HTTP request is switching on/off LEDs
// also saves the state of the LEDs
void SetLEDs(void)
{
cl.println("</inputs>");
Serial.println("</inputs>");
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
Hallo
Der Grund warum ich einen Arduino (Mega2560) damit quälen möchte ist ganz einfach.
Ich habe vor einiger Zeit meine Heizungssteuerung damit realisiert, und möchte das ganze jetzt auf einem Tablet visualisieren.
lG binderj1
Dann schaue doch hier nach, welche Datei gewünscht wird und liefere diese aus:
...
else { // web page request
// steht "index" im Request öffne index.html
// steht "seite2" im Request öffne seite2.html
...
Gruß Tommy
Hallo Thommy
Danke für deine Hilfe ich hab das wie folgt gelöst.
if (strstr(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
// Serial.println("ajax_inputs");
SetLEDs();
// send XML file containing input states
XML_response(client);
}
if (strstr(HTTP_req, "index.htm")) { // web page request
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
//Serial.println("index.htm");
}
webFile.close();
}
}
if (strstr(HTTP_req, "seite2")) {
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
webFile = SD.open("seite2.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
Serial.println("seite2.htm");
}
webFile.close();
}
}
jetzt muss ich schauen das die "seite2.htm" richtig angezeigt wird, derzeit wird sie an "index.htm" angehängt.
Warum machst Du das so umständlich?
char fname[11];
fname[0] = '\0';
if (strstr(HTTP_req, "index.htm")) { // web page request
strcpy(fname,"index.htm");
}
else if (strstr(HTTP_req, "seite2")) {
strcpy(fname,"seite2.htm");
}
if (fname[0] != '\0') { // Da ist was drin
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
webFile = SD.open(fname); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
//Serial.println("index.htm");
}
webFile.close();
}
}
Gruß Tommy
oh Danke funktioniert super 