Hello guys . I’m trying to make a multiple server. I want to access on 192.168.0.120:80 a page and an another 192.168.0.120:8080 another page that is stocked on my SD card. I don’t want to access them simoultaneous.The code compiles and whent i want to access the browser and enter the ip and port he don’t show me nothing. Can anyone help me?
The first part of the code:
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>
#include <serialGLCD.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x2F, 0xDC };
byte ip[] = { 192,168,0, 120 };
char rootFileName[] = "login.htm";
EthernetServer server(80);
EthernetServer server2(8080);
//pagina contact
// String variables fo each line on the LCD declared globally because I'm lazy, but also
// because it needs to survive loop(), without being cleared from the stack (also to hell with good practice)
String line1 = " ";
String line2 = " ";
// Constant strings for rendering the Webpage are stored in flash
// memory to ration it for other non constant variables
// Strings stored in flash mem for the Html Header
prog_char Header_0[] PROGMEM = "HTTP/1.1 200 OK"; //
prog_char Header_1[] PROGMEM = "Content-Type: text/html"; //
prog_char Header_2[] PROGMEM = ""; //
prog_char Header_3[] PROGMEM = "<HTML>\n<HEAD>"; // The header lines
prog_char Header_4[] PROGMEM = "<TITLE>Contact</TITLE>"; //
prog_char Header_5[] PROGMEM = "</HEAD><BODY>"; //
// A table of pointers to the flash memory strings for the header
PROGMEM const char *Header_table[] =
{
Header_0,
Header_1,
Header_2,
Header_3,
Header_4,
Header_5};
// Strings stored in flash mem for the body of the webpage, including the input forms
// Only the longer lines, not involving variables are stored here
prog_char Body_0[] PROGMEM = "<H2>Ce se afiseaza :</H2>";
prog_char Body_1[] PROGMEM = "<H2>Trimite un mesaj pe LCD!</H2>";
prog_char Body_2[] PROGMEM = "<form action=\"/?\" method=get>";
prog_char Body_3[] PROGMEM = "<b>Nume: </b><input type=\"text\" name=\"L1\" maxlength=\"16\" size=\"16\" />
";
prog_char Body_4[] PROGMEM = "<b>Mesaj: </b><input type=\"text\" name=\"L2\" maxlength=\"16\" size=\"16\" />
";
prog_char Body_5[] PROGMEM = "<input type=\"submit\" value=\"Trimite\" /></form>";
// A table of pointers to the flash memory strings for the body
PROGMEM const char *Body_table[] =
{
Body_0,
Body_1,
Body_2,
Body_3,
Body_4,
Body_5};
// Begin Webserver Specific Code
// Print out MIME and HTML header at top of webpage using the
// strings previously stored in flash memory
void HtmlHeader(EthernetClient client) {
char buffer[30]; //A character array to hold the strings from the flash mem
for (int i = 0; i < 6; i++) {
strcpy_P(buffer, (char*)pgm_read_word(&(Header_table[i]))); // Necessary casts and dereferencing, just copy.
client.println( buffer );
}
}
// Print the footer at the bottom of the webpage
void HtmlFooter(EthernetClient client) {
client.println("</BODY></HTML>");
}
// Parse an HTTP request header one character at a time, seeking string variables (modified from Kevin Haw's code)
void ParseHttpHeader(EthernetClient &client) {
char c;
int i = 0; //An integer use to limit the size of rawUrlText (prevents crashing due to running out of memory)
String rawUrlText = "";
// Skip through until we hit a question mark (first one)
while((c = client.read()) != '?' && client.available()) {
// Debug - print data
Serial.print(c);
}
// Are we here for a question mark or did we run out of data?
if(client.available() > 2) {
// Read the data and add it to our unmodified string rawURLText!
// the incrementer limits the input to about 1 line of plain text and 1/2 line of symbols, more causes line2 to truncate
while((c = client.read()) != ' ' && client.available() && i < 55) {
rawUrlText = rawUrlText + c;
Serial.print(c);
i++;
}
htmlToHuman(rawUrlText); // Make it readable
line1.setCharAt(line1.lastIndexOf('0'), ' '); // the lcd prints the 0's in the null terminator, so we remove them
line2.setCharAt(line2.lastIndexOf('0'), ' '); //This has been revised to prevent remove of legit 0's
}
return;
}
// A function that takes the html formatted string and makes it readable again
void htmlToHuman(String URLstring) {
int indexOfDelim = 0; //This variable stores the location of our delimiter so we can find where line1 ends and line2 begins
// The following array stores a list of ugly html codes, and the special charaters they represent (for changing them back)
const String CHAR_CONVERSIONS[29][2] = {{"+"," "},{"%40","@"},{"%23","#"},{"%24","$"},{"%2B","+"},{"%21","!"},{"%7E","~"},
{"%3A",":"},{"%3B",";"},{"%2C",","},{"%3F","?"},{"%2F","/"},{"%7C","|"},{"%5E","^"},
{"%5C","\\"},{"%7B","{"},{"%7D","}"},{"%5B","["},{"%5D","]"},{"%3C","<"},{"%3E",">"},
{"%28","("},{"%29",")"},{"%27","'"},{"%22","\""},{"%3F","?"},{"%26","&"},{"%3D","="},
{"%25","%"}};
URLstring.replace("L1=",""); // remove the unecessary field variable names
URLstring.replace("&L2=","`"); // and turn this one into our delimiter character ` (The one on the ~ key)
//A for loop that replaces all the html codes with the right symbols
for (int i=0 ; i < 29; i++) {
URLstring.replace(CHAR_CONVERSIONS[i][0],CHAR_CONVERSIONS[i][1]);
}
indexOfDelim = URLstring.indexOf("`"); // find the index of that delimiter
line1 = URLstring.substring(0,indexOfDelim); // set line1 and line 2 using that knowlege
line1 += NULL; // but add a null terminator to each to avoid some odd bugs
line2 = URLstring.substring(indexOfDelim+1,URLstring.length());
line2 += NULL;
serialGLCD lcd;
lcd.clearLCD();
delay(40);
lcd.gotoPosition(5,5);
Serial.print("Ai primit un mesaj de la :");
lcd.gotoPosition(20,15);
Serial.print(line1);
lcd.gotoPosition(5,25);
Serial.print("Continut mesaj");
lcd.gotoPosition(20,35);
Serial.print(line2);
delay(10000);
return;
}
//terminare pagina contact
char* ficheiroLer;
/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();
// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
Serial.println();
PgmPrintln("Done");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
server2.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}