Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SD card stuff
- #include <SD.h>
- File myFile;
- //Ethernet stuff
- #include <SPI.h>
- #include <Ethernet.h>
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEB };
- byte ip[] = {
- 192,168,1, 87 };
- Server server(80);
- //EEPROM stuff
- #include <EEPROM.h>
- /*
- 0 = Trigger humidity
- 1 = Minimum run time
- 2 = Air quality
- 3-4 = Schedule Start
- 5-6 = Schedule Stop
- 7 = Schedule Using fan 1
- 8 = Schedule Using fan 2
- 9 = PWM min fan 1
- 10 = PWM max fan 1
- 11 = PWM min fan 2
- 12 = PWM max fan 2
- 13 = Manual fan 1
- 14 = Manual fan 2
- */
- //Stuff used to process the page and url
- String inString = String(150);
- boolean process = false;
- void setup()
- {
- Ethernet.begin(mac, ip);
- server.begin();
- pinMode(10, OUTPUT); //Must be set to output for SD to work
- Serial.begin(9600);
- if (!SD.begin(4))
- {
- /*
- client.println("Please insert SD card.");
- break;
- */
- }
- }
- void loop()
- {
- // listen for incoming clients
- Client client = server.available();
- if (client)
- {
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected())
- {
- if (client.available())
- {
- char c = client.read();
- if (inString.length() < 150)
- {
- inString += c;
- }
- if (c == '\n' && currentLineIsBlank)
- {
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- 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;
- }
- }
- }
- if (inString.indexOf('GET /favicon.ico') == 10) //Do nothing about the .ico request
- {
- client.stop();
- inString = "";
- return;
- }
- char* filename;
- if (inString.indexOf("GET / ") != -1)
- {
- Serial.println("Using default file");
- filename = "index.htm";
- }
- else if (inString.indexOf("GET /dynamic.data") != -1)
- {
- client.print(analogRead(A0));
- client.print(",");
- client.print(analogRead(A1));
- client.print(",");
- client.print(analogRead(A2));
- client.print(",");
- client.print(analogRead(A3));
- client.print(",");
- client.println(analogRead(A4));
- delay(1);
- client.stop();
- inString = "";
- return;
- }
- else if (inString.indexOf("Save=Save") != -1)
- {
- /*
- 0 = Trigger humidity
- 1 = Minimum run time
- 2 = Air quality
- 3-4 = Schedule Start
- 5-6 = Schedule Stop
- 7 = Schedule Using fan 1
- 8 = Schedule Using fan 2
- 9 = PWM min fan 1
- 10 = PWM max fan 1
- 11 = PWM min fan 2
- 12 = PWM max fan 2
- 13 = Manual fan 1
- 14 = Manual fan 2
- */
- eeWrite(0, getFunc("humidityTarget"));
- eeWrite(1, getFunc("minRunTime"));
- eeWrite(2, getFunc("airQuality"));
- }
- if (strlen(filename) <= 1)
- {
- filename = "index.htm";
- }
- myFile = SD.open(filename);
- if (myFile)
- {
- char temp[5];
- String output;
- int16_t c;
- while ((c = myFile.read()) > 0)
- {
- itoa(c, (char*)temp, 10);
- output += temp;
- if (c == '\r' || c == '\n')
- {
- client.print(output);
- output = "";
- }
- //client.print((char)c);
- }
- myFile.close();
- }
- else
- {
- // if the file didn't open, print an error:
- client.println("Could not find file \"");
- client.println(filename);
- client.println("\"");
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- inString = "";
- }
- }
- int getFunc(char* func)
- {
- char* output;
- String temp = String(35);
- if(inString.indexOf(func) != -1)
- {
- int cStart = inString.indexOf("humidityTarget");
- temp = inString.substring(cStart+14);
- int cStop = temp.indexOf("&");
- String getPinDel = temp.substring(1, cStop);
- char tempNum[10];
- tempNum[getPinDel.length() + 1]; // Temp save pin value
- getPinDel.toCharArray(tempNum, 10);
- return atoi(tempNum);
- }
- else
- {
- return -1;
- }
- }
- int eeRead(int id)
- {
- EEPROM.read(id);
- }
- void eeWrite(int id, int value)
- {
- if (EEPROM.read(id) != value && value >= 0) //Check if it is needed to update the current value, if value is less than zero, it means it was not found in the url, and should not be attempted saved
- {
- EEPROM.write(id, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement