View Single Post
Old 01-03-16, 02:27 PM   #23
Acuario
Apprentice EcoRenovator
 
Join Date: May 2011
Location: Tortosa, Spain
Posts: 221
Thanks: 2
Thanked 81 Times in 46 Posts
Default

9. Ethernet.ino (part 1)
//For ethernet
#include <SPI.h>
#include <Ethernet.h>
#include "HeatpumpController.h"

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAD, 0xBE, 0xEF, 0xFD, 0x13 };
byte ip[] = { 192, 168, 0, 190 }; // ip in lan (that's what you need to use in your browser.
byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
#define ETHPORT 8081
EthernetServer server(ETHPORT); // Initialize the Ethernet server library

void returnSetupPage(EthernetClient cl, bool save);
void saveSettings(char * buf);
void returnWebPage(EthernetClient cl);
void XML_response(EthernetClient cl);
void StrClear(char *str, char length);
char StrContains(char *str, char *sfind);

void setupEthernet(void) {
Ethernet.begin(mac, ip);
server.begin();

DEBUG_PRINT("server is at ");
DEBUG_PRINTLN(Ethernet.localIP());
}

NIL_THREAD(threadEthernet, arg) { // listen for incoming clients
#define REQ_BUF_SZ 120 // size of buffer used to capture HTTP requests
char HTTP_req[REQ_BUF_SZ] = { 0 }; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer

while (TRUE) {
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 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) {
// display received HTTP request on serial port
//DEBUG_PRINTST("Received HTTP:-%s", HTTP_req);

// 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 if (StrContains(HTTP_req, "setup")) {
returnSetupPage(client, FALSE);
}
else if (StrContains(HTTP_req, "savesettings")) {
saveSettings(HTTP_req);
returnSetupPage(client, TRUE);
}
else if (StrContains(HTTP_req, "settime")) {
setTime(HTTP_req);
returnSetupPage(client, TRUE);
}
else if (StrContains(HTTP_req, "defrostOn")) {
Serial.println("START defrost forced");
defrostFlag = TRUE;
returnSetupPage(client, FALSE);
}
else if (StrContains(HTTP_req, "defrostOff")) {
Serial.println("STOP defrost forced");
defrostFlag = FALSE;
returnSetupPage(client, FALSE);
}
else { // web page request send rest of HTTP header
returnWebPage(client);
}
// 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())
nilThdSleepSeconds(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)

// Sleep
nilThdSleepSeconds(10);
}
}
Acuario is offline   Reply With Quote