Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <SD.h>
- #define REQ_BUF_SZ 60
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress ip(192, 168, 0, 20);
- EthernetServer server(80);
- File webFile;
- char HTTP_req[REQ_BUF_SZ] = {0};
- char req_index = 0;
- boolean LED_state[5] = {0};
- void setup()
- {
- // disable Ethernet chip
- pinMode(10, OUTPUT);
- digitalWrite(10, HIGH);
- Serial.begin(9600);
- // initialize SD card
- Serial.println("Initializing SD card...");
- if (!SD.begin(4)) {
- Serial.println("ERROR - SD card initialization failed!");
- return;
- }
- Serial.println("SUCCESS - SD card initialized.");
- if (!SD.exists("index.htm")) {
- Serial.println("ERROR - Can't find index.htm file!");
- return;
- }
- Serial.println("SUCCESS - Found index.htm file.");
- // switches
- pinMode(2, INPUT);
- pinMode(3, INPUT);
- // LEDs
- pinMode(9, OUTPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(8, OUTPUT);
- Ethernet.begin(mac, ip);
- server.begin();
- }
- void loop()
- {
- 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
- if (req_index < (REQ_BUF_SZ - 1)) {
- HTTP_req[req_index] = c; // save HTTP request character
- req_index++;
- }
- if (c == '\n' && currentLineIsBlank) {
- client.println("HTTP/1.1 200 OK");
- if (StrContains(HTTP_req, "ajax_inputs")) {
- client.println("Content-Type: text/xml");
- client.println("Connection: keep-alive");
- client.println();
- SetLEDs();
- // send XML file containing input states
- XML_response(client);
- }
- else {
- client.println("Content-Type: text/html");
- client.println("Connection: keep-alive");
- client.println();
- webFile = SD.open("index.htm");
- if (webFile) {
- while (webFile.available()) {
- client.write(webFile.read()); }
- webFile.close();
- }
- }
- Serial.print(HTTP_req);
- req_index = 0;
- StrClear(HTTP_req, REQ_BUF_SZ);
- break;
- }
- if (c == '\n') {
- 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)
- // read buttons and debounce
- ButtonDebounce();
- }
- void ButtonDebounce(void)
- {
- static byte buttonState[2] = {LOW, LOW}; // the current reading from the input pin
- static byte lastButtonState[2] = {LOW, LOW}; // the previous reading from the input pin
- static long lastDebounceTime[2] = {0}; // the last time the output pin was toggled
- long debounceDelay = 50; // the debounce time; increase if the output flickers
- byte reading[2];
- reading[0] = digitalRead(2);
- reading[1] = digitalRead(3);
- for (int i = 0; i < 2; i++) {
- if (reading[i] != lastButtonState[i]) {
- // reset the debouncing timer
- lastDebounceTime[i] = millis();
- }
- if ((millis() - lastDebounceTime[i]) > debounceDelay) {
- // if button state changed
- if (reading[i] != buttonState[i]) {
- buttonState[i] = reading[i];
- // toggle the LED if the new button state is HIGH
- if (buttonState[i] == HIGH) {
- LED_state[i] = !LED_state[i];
- }
- }
- }
- } // end for() loop
- // set the LEDs
- digitalWrite(9, LED_state[0]);
- digitalWrite(5, LED_state[1]);
- digitalWrite(6, LED_state[2]);
- digitalWrite(7, LED_state[3]);
- digitalWrite(8, LED_state[4]);
- lastButtonState[0] = reading[0];
- lastButtonState[1] = reading[1];
- }
- // Setleds
- void SetLEDs(void)
- {
- // Main Verlichting (pin 9)
- if (StrContains(HTTP_req, "LED1=1")) {
- LED_state[0] = 1; // save LED state
- digitalWrite(9, HIGH);
- }
- else if (StrContains(HTTP_req, "LED1=0")) {
- LED_state[0] = 0; // save LED state
- digitalWrite(9, LOW);
- }
- // Sub Verlichting (pin 5)
- if (StrContains(HTTP_req, "LED2=1")) {
- LED_state[1] = 1; // save LED state
- digitalWrite(5, HIGH);
- }
- else if (StrContains(HTTP_req, "LED2=0")) {
- LED_state[1] = 0; // save LED state
- digitalWrite(5, LOW);
- }
- // Alarm (pin 6)
- if (StrContains(HTTP_req, "LED3=1")) {
- LED_state[2] = 1; // save LED state
- digitalWrite(6, HIGH);
- }
- else if (StrContains(HTTP_req, "LED3=0")) {
- LED_state[2] = 0; // save LED state
- digitalWrite(6, LOW);
- }
- // Rolluik 1(pin 7)
- if (StrContains(HTTP_req, "LED4=1")) {
- LED_state[3] = 1; // save LED state
- digitalWrite(7, HIGH);
- }
- else if (StrContains(HTTP_req, "LED4=0")) {
- LED_state[3] = 0; // save LED state
- digitalWrite(7, LOW);
- }
- // Rolluik 2 (pin 8)
- if (StrContains(HTTP_req, "LED5=1")) {
- LED_state[4] = 1; // save LED state
- digitalWrite(8, HIGH);
- }
- else if (StrContains(HTTP_req, "LED5=0")) {
- LED_state[4] = 0; // save LED state
- digitalWrite(8, LOW);
- }
- }
- // send XML file w/ switch status and LED status
- void XML_response(EthernetClient cl)
- {
- int analog_val;
- int count;
- int sw_arr[] = {2, 3};
- cl.print("<?xml version = \"1.0\" ?>");
- cl.print("<inputs>");
- // checkbox LED states
- // LED1
- cl.print("<led>");
- if (LED_state[0]) {
- cl.print("checked");
- }
- else {
- cl.print("unchecked");
- }
- cl.println("</led>");
- // button LED states
- // LED2
- cl.print("<led>");
- if (LED_state[1]) {
- cl.print("on");
- }
- else {
- cl.print("off");
- }
- cl.println("</led>");
- // button LED states
- // LED3
- cl.print("<led>");
- if (LED_state[2]) {
- cl.print("on");
- }
- else {
- cl.print("off");
- }
- cl.println("</led>");
- // button LED states
- // LED4
- cl.print("<led>");
- if (LED_state[3]) {
- cl.print("on");
- }
- else {
- cl.print("off");
- }
- cl.println("</led>");
- // button LED states
- // LED5
- cl.print("<led>");
- if (LED_state[4]) {
- cl.print("on");
- }
- else {
- cl.print("off");
- }
- cl.println("</led>");
- cl.print("</inputs>");
- }
- void StrClear(char *str, char length)
- {
- for (int i = 0; i < length; i++) {
- str[i] = 0;
- }
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement