Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*--------------------------------------------------------------
- Ajax server sketch that serves web page from SD card and sends serial commands to another Arduino
- running a thermostat and recieves variable values to send to web client.
- steve.a.mccluskey@gmail.com
- Modified from sketch found at
- http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-AJAX-XML-web-server/
- Original Author: W.A. Smith, http://startingelectronics.com
- References: - WebServer example by David A. Mellis and
- modified by Tom Igoe
- - SD card examples by David A. Mellis and
- Tom Igoe
- - Ethernet library documentation:
- http://arduino.cc/en/Reference/Ethernet
- - SD Card library documentation:
- http://arduino.cc/en/Reference/SD
- Pin layout:
- 0 : Hardware serial RX.
- 1 : Hardware serial TX.
- 2 :
- 3 ~:
- 4 : SD CS.
- 5 ~:
- 6 ~:
- 7 :
- 8 :
- 9 ~:
- 10~: Ethernet shield CS.
- 11~: Ethernet shield MOSI.
- 12 : Ethernet shield MISO.
- 13 : Ethernet shield CLK.
- Revisions:
- 1.2: Added code to handle if sensor is unplugged on the web page, 02/18/14.
- 1.2: Added support for temps down to -40F. 02/20/14.
- --------------------------------------------------------------*/
- #include <SPI.h>
- #include <Ethernet.h>
- #include <SD.h>
- #define REQ_BUF_SZ 60 // size of buffer used to capture HTTP requests.
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress ip(192, 168, 1, 179); // IP address, may need to change depending on network.
- EthernetServer server(80); // create a server at port 80.
- File webFile;
- char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string.
- char req_index = 0; // index into HTTP_req buffer.
- //variables to serve:
- uint8_t tempHold = 0;
- int8_t tempAvg = 0;
- uint8_t hour = 0;
- uint8_t eveningHour = 0;
- uint8_t morningHour = 0;
- uint8_t tempReduction = 0;
- uint8_t door = 0;
- uint8_t heater = 0;
- int8_t temp1 = 0;
- int8_t temp2 = 0;
- int8_t temp3 = 0;
- int8_t tempOut = 0;
- unsigned long timeNow = 0, lastUpdate = 0;
- String readString;
- void setup() {
- pinMode(10, OUTPUT);
- digitalWrite(10, HIGH); // disable Ethernet chip.
- Serial.begin(19200);
- if (!SD.begin(4)) { // initialize SD card.
- return; // init failed.
- } //end if.
- if (!SD.exists("index.htm")) { // check for index.htm file.
- return; // can't find index file.
- } //end if.
- Ethernet.begin(mac, ip);
- server.begin();
- } //end setup().
- void loop() {
- timeNow = millis();
- EthernetClient client = server.available(); // try to get client.
- if (timeNow - lastUpdate > 10000) { //keeps data fresh when no client connected.
- Serial.println(F("*"));
- lastUpdate = millis();
- } //end if.
- if (client) { // got client?
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- 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++;
- } //end if.
- // 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(F("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.
- Serial.println(F("*")); //request all values from thermostat.
- client.println(F("Content-Type: text/xml"));
- client.println(F("Connection: keep-alive"));
- client.println();
- sendCommands();
- XML_response(client); //send XML file containing input states.
- } //end if.
- else { // web page request.
- // send rest of HTTP header:
- client.println(F("Content-Type: text/html"));
- client.println(F("Connection: keep-alive"));
- client.println();
- // send web page:
- webFile = SD.open("index.htm");
- if (webFile) {
- while(webFile.available()) {
- client.write(webFile.read()); //send web page to client.
- } //end while.
- webFile.close();
- } //end if.
- } //end else.
- //Serial.print(HTTP_req); // display received HTTP request on serial port.
- // reset buffer index and all buffer elements to 0:
- req_index = 0;
- StrClear(HTTP_req, REQ_BUF_SZ);
- break;
- } //end if.
- // 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;
- } //end if.
- else if (c != '\r') { // a text character was received from client.
- currentLineIsBlank = false;
- } //end if.
- } // 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 loop().
- // checks if received HTTP request contains certain strings then issues serial commands accordingly.
- void sendCommands(void) {
- if (StrContains(HTTP_req, "tempUp")) {
- Serial.println(F("h")); // increase hold temp.
- } //end if.
- else if (StrContains(HTTP_req, "tempDn")) {
- Serial.println(F("n")); // decrease hold temp.
- } //end else if
- else if (StrContains(HTTP_req, "nightUp")) {
- Serial.println(F("e")); // night hour increase.
- } //end else if
- else if (StrContains(HTTP_req, "nightDn")) {
- Serial.println(F("d")); // night hour decrease.
- } //end else if
- else if (StrContains(HTTP_req, "dayUp")) {
- Serial.println(F("m")); // day hour increase.
- } //end else if
- else if (StrContains(HTTP_req, "dayDn")) {
- Serial.println(F("j")); //day hour decrease.
- } //end else if
- else if (StrContains(HTTP_req, "tempRUp")) {
- Serial.println(F("t")); // increase temp reduction.
- } //end else if
- else if (StrContains(HTTP_req, "tempRDn")) {
- Serial.println(F("g")); //decrease temp reduction.
- } //end else if
- } //end sendCommands().
- // send the XML file with current variable values
- void XML_response(EthernetClient cl) {
- cl.print(F("<?xml version = \"1.0\" ?>"));
- cl.print(F("<inputs>"));
- cl.print(F("<tempAvg>"));
- cl.print(tempAvg);
- cl.print(F("</tempAvg>"));
- cl.print(F("<tempHold>"));
- cl.print(tempHold);
- cl.print(F("</tempHold>"));
- cl.print(F("<hour>"));
- cl.print(hour);
- cl.print(F("</hour>"));
- cl.print(F("<eveningHour>"));
- cl.print(eveningHour - 12);
- cl.print(F("</eveningHour>"));
- cl.print(F("<morningHour>"));
- cl.print(morningHour);
- cl.print(F("</morningHour>"));
- cl.print(F("<tempReduction>"));
- cl.print(tempReduction);
- cl.print(F("</tempReduction>"));
- cl.print(F("<doorState>"));
- cl.print(door);
- cl.print(F("</doorState>"));
- cl.print(F("<heaterState>"));
- cl.print(heater);
- cl.print(F("</heaterState>"));
- cl.print(F("<sensors>"));
- if (temp1 >= -40) {
- cl.print(temp1);
- } //end if.
- else {
- cl.print(F("--"));
- } //end else.
- cl.print(F("</sensors>"));
- cl.print(F("<sensors>"));
- if (temp2 >= -40) {
- cl.print(temp2);
- } //end if.
- else {
- cl.print(F("--"));
- } //end else.
- cl.print(F("</sensors>"));
- cl.print(F("<sensors>"));
- if (temp3 >= -40) {
- cl.print(temp3);
- } //end if.
- else {
- cl.print(F("--"));
- } //end else.
- cl.print(F("</sensors>"));
- cl.print(F("<sensors>"));
- if (tempOut >= -40) {
- cl.print(tempOut);
- } //end if.
- else {
- cl.print(F("--"));
- } //end else.
- cl.print(F("</sensors>"));
- cl.print(F("</inputs>"));
- } //end XML_response().
- // sets every element of str to 0 (clears array)
- void StrClear(char *str, char length) {
- for (uint8_t i = 0; i < length; i++) {
- str[i] = 0;
- } //end for.
- } //end StrClear().
- // 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;
- } //end if.
- while (index < len) {
- if (str[index] == sfind[found]) {
- found++;
- if (strlen(sfind) == found) {
- return 1;
- } //end if.
- } //end if.
- else {
- found = 0;
- } //end else.
- index++;
- } //end while.
- return 0;
- } //end StrContains().
- void serialEvent() { //recieves all values from thermostat and saves to global values.
- while (Serial.available()) {
- char c = Serial.read();
- readString += c;
- if (c == '*') { //end of packet.
- uint8_t holdEnd = readString.indexOf('h');
- uint8_t avgEnd = readString.indexOf('a');
- uint8_t hourEnd = readString.indexOf('j');
- uint8_t eveningEnd = readString.indexOf('e');
- uint8_t morningEnd = readString.indexOf('m');
- uint8_t reductionEnd = readString.indexOf('t');
- uint8_t doorEnd = readString.indexOf('d');
- uint8_t heaterEnd = readString.indexOf('f');
- uint8_t temp1End = readString.indexOf('x');
- uint8_t temp2End = readString.indexOf('y');
- uint8_t temp3End = readString.indexOf('z');
- uint8_t tempOutEnd = readString.indexOf('w');
- String hold = readString.substring(0, holdEnd);
- tempHold = hold.toInt();
- String avg = readString.substring(holdEnd + 1, avgEnd);
- tempAvg = avg.toInt();
- String hour1 = readString.substring(avgEnd + 1, hourEnd);
- hour = hour1.toInt();
- String evening = readString.substring(hourEnd + 1, eveningEnd);
- eveningHour = evening.toInt();
- String morning = readString.substring(eveningEnd + 1, morningEnd);
- morningHour = morning.toInt();
- String reduction = readString.substring(morningEnd + 1, reductionEnd);
- tempReduction = reduction.toInt();
- String doorCheck = readString.substring(reductionEnd + 1, doorEnd);
- door = doorCheck.toInt();
- String heaterCheck = readString.substring(doorEnd + 1, heaterEnd);
- heater = heaterCheck.toInt();
- String temp1Check = readString.substring(heaterEnd + 1, temp1End);
- if (temp1Check == "--") {
- temp1 = -41;
- } //end if.
- else {
- temp1 = temp1Check.toInt();
- } //end else.
- String temp2Check = readString.substring(temp1End + 1, temp2End);
- if (temp2Check == "--") {
- temp2 = -41;
- } //end if.
- else {
- temp2 = temp2Check.toInt();
- } //end else.
- String temp3Check = readString.substring(temp2End + 1, temp3End);
- if (temp3Check == "--") {
- temp3 = -41;
- } //end if.
- else {
- temp3 = temp3Check.toInt();
- } //end else.
- String tempOutCheck = readString.substring(temp3End + 1, tempOutEnd);
- if (tempOutCheck == "--") {
- tempOut = -41;
- } //end if.
- else {
- tempOut = tempOutCheck.toInt();
- } //end else.
- readString = "";
- } //end if.
- } //end while.
- } //end serialEvent().
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement