Hi all,
Im trying to do a udemy class on creating a webpage with the arduino uno and ethernet sheild. Ive followed the instructions and cannot get my arduino to see the "index.htm" file. has anyone one had this problem before? How do i fix it? below is the code.
#include <SD.h> // The SD library allows for reading from and writing to SD cards
#include <SPI.h>
#include <Ethernet.h>
#define REQ_BUF_SZ 50 // size of buffer used to capture HTTP requests
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address from Ethernet shield sticker under board (if present)
IPAddress ip(192, 168, 0, 106); // Enter your IP address found using IP address finder in a previous lecture
EthernetServer miniServer(80);
File SdCardFile; // the web page file on the SD card
boolean SWITCH_state[4] = {0}; // stores the states of the Switches
char HTTP_request[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char request_index = 0; // index into HTTP_req buffer
void setup()
{
// For the Ethernet shield to work properly, you must make pin 10 an output and high before initializing the SD card as
// both the SD card and Ethernet controller cannot use the SPI bus simultaneously.
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(5, OUTPUT); // Switch #1
pinMode(6, OUTPUT); // Switch #2
pinMode(7, OUTPUT); // Switch #3
pinMode(8, OUTPUT); // Switch #4
// Initialize Switches
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
Serial.begin(9600); // for debugging only
Ethernet.begin(mac, ip); // initialize Ethernet device
miniServer.begin();
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - Bad or missing SD card");
return;
}
Serial.println("SUCCESS - SD card found");
if (!SD.exists("fts")) { // check the file system to see if their is a file named index.htm
Serial.println("ERROR - Can't find fts file!");
return;
}
Serial.println("SUCCESS - Found fts file.");
}
void loop()
{
// listen for incoming clients
EthernetClient client = miniServer.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // Returns the number of bytes available for reading
char c = client.read(); // read 1 byte (character) from client
if (request_index < (REQ_BUF_SZ - 1)) { // buffer first part of HTTP request in HTTP_req array
HTTP_request[request_index] = c; // save HTTP request character
request_index++;
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
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_request, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
// send XML file containing input states
SetSWITCHES();
XML_response(client);
}
else {
// web page request
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
SdCardFile = SD.open("index.htm"); // open web page file
if (SdCardFile) {
while(SdCardFile.available()) {
client.write(SdCardFile.read()); // send web page to client
}
SdCardFile.close();
}
}
// display received HTTP request on serial port
Serial.println(HTTP_request);
// reset buffer index and all buffer elements to 0
request_index = 0;
StrClear(HTTP_request, REQ_BUF_SZ);
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
} // end of client.available
} // end of client.connected
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
}
}
char StrContains(char *str, char *sfind)
// searches for the string sfind in the string str, returns 1 if string found or returns 0 if string not found.
{
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;
}
void XML_response(EthernetClient cl)
{
// Sending Gauge Values to webclient
cl.print("<?xml version = \"1.0\" ?>");
cl.print("<inputs>");
// Sending SWITCH states to dashboard
// SWITCH #1
cl.print("<SW>");
if (SWITCH_state[0]) {
cl.print("checked");
}
else {
cl.print("unchecked");
}
cl.println("</SW>");
// SWITCH #2
cl.print("<SW>");
if (SWITCH_state[1]) {
cl.print("checked");
}
else {
cl.print("unchecked");
}
cl.println("</SW>");
// SWITCH #3
cl.print("<SW>");
if (SWITCH_state[2]) {
cl.print("checked");
}
else {
cl.print("unchecked");
}
cl.println("</SW>");
// SWITCH #4
cl.print("<SW>");
if (SWITCH_state[3]) {
cl.print("checked");
}
else {
cl.print("unchecked");
}
cl.println("</SW>");
cl.print("</inputs>");
}
void SetSWITCHES(void)
// checks if received HTTP request is switching any of our switches
{
// SWITCH #1
if (StrContains(HTTP_request, "SW1=1")) {
SWITCH_state[0] = 1; // save SWITCH state
digitalWrite(5, LOW);
}
else if (StrContains(HTTP_request, "SW1=0")) {
SWITCH_state[0] = 0; // save SWITCH state
digitalWrite(5, HIGH);
}
// SWITCH #2
if (StrContains(HTTP_request, "SW2=1")) {
SWITCH_state[1] = 1; // save SWITCH state
digitalWrite(6, LOW);
}
else if (StrContains(HTTP_request, "SW2=0")) {
SWITCH_state[1] = 0; // save SWITCH state
digitalWrite(6, HIGH);
}
// SWITCH #3 (
if (StrContains(HTTP_request, "SW3=1")) {
SWITCH_state[2] = 1; // save SWITCH state
digitalWrite(7, LOW);
}
else if (StrContains(HTTP_request, "SW3=0")) {
SWITCH_state[2] = 0; // save SWITCH state
digitalWrite(7, HIGH);
}
// SWITCH #4
if (StrContains(HTTP_request, "SW4=1")) {
SWITCH_state[3] = 1; // save SWITCH state
digitalWrite(8, LOW);
}
else if (StrContains(HTTP_request, "SW4=0")) {
SWITCH_state[3] = 0; // save SWITCH state
digitalWrite(8, HIGH);
}
}
void StrClear(char *str, char length)
// sets every element of str to 0 (clears array)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}