Hey so i have no idea why my project wont even operate!! I am using an AJAX system for my webpage. The problem is my SD card wont actually initialize
My Code is as Follows:
/*--------------------------------------------------------------
Description: Arduino web server that displays 4 analog inputs,
the state of 3 switches and controls 4 outputs,
2 using checkboxes and 2 using buttons.
The web page is stored on the micro SD card.
Hardware: Arduino Uno and official Arduino Ethernet
shield. Should work with other Arduinos and
compatible Ethernet shields.
2Gb micro SD card formatted FAT16.
Pins 2, 3 and 5 for the switches.
RF Rx
--------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <VirtualWire.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address from Ethernet shield sticker under board
IPAddress ip(192, 168, 1, 100); // 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
int LEDR = 6; //Pin 6
int LEDG = 9; //Pin 9
int LEDR1 = 8; //Pin 8
int LEDG1= 7; //Pin 7
void setup()
{
// disable Ethernet chip 53 for MEGA
pinMode(53, OUTPUT); //Pin 53
digitalWrite(53, HIGH); //Pin 53
// vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_rx_pin(11);
vw_rx_start(); // Start the receiver PLL running
pinMode(LEDG, OUTPUT); //pin 9
pinMode(LEDR, OUTPUT); //pin 6
pinMode(LEDG1, OUTPUT); //pin 7
pinMode(LEDR1, OUTPUT); //pin 8
Serial.begin(9600); // for debugging
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) { // pin 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("HOME_AUTOMATION.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}//End setup
void loop()
{
digitalWrite(LEDG, LOW);
digitalWrite(LEDR, LOW);
digitalWrite(LEDG1, LOW);
digitalWrite(LEDR1, LOW);
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.println("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
if (buf[i] == '1') {
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, LOW);
delay(10000);
}
if (buf[i] == '0') {
digitalWrite(LEDG, HIGH);
digitalWrite(LEDR, LOW);
delay(10000);
}
if (buf[i] == '2') {
digitalWrite(LEDR1, HIGH);
digitalWrite(LEDG1, LOW);
delay(10000);
}
if (buf[i] == '3') {
digitalWrite(LEDG1, HIGH);
digitalWrite(LEDR1, LOW);
delay(10000);
}
Serial.println("");
}
Serial.println("");
digitalWrite(13, false);
}
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();
// 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.println(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)
}//End main loop
// send the XML file with analog values, switch status
// and LED status
void XML_response(EthernetClient cl)
{
int count; // used by 'for' loops
int sw_arr[] = {10,9}; // pins interfaced to switches
cl.print("<?xml version = \"1.0\" ?>");
cl.print("<inputs>");
// read switches
for (count = 0; count < 2; count++) {
cl.print("<switch>");
if (digitalRead(sw_arr[count])==HIGH)
{
cl.print("ON");
}
else
{
cl.print("OFF");
}
cl.println("</switch>");
}
}
// 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;
}