A Proof of Concept of integration of Arduino as simple IO for openHAB home automation

The design criteria of the module was

  1. Cheap IO for lab and cost efficient home automation
  2. Simple IO with multiple digital input and analog input
  3. IO accessible via lan/wan ethernet
  4. IO mapped to openHAB Contact items and Number items via HTTP binding
  5. Minimise roundtrip for efficient lan usage

openHAB.cfg
To be able to minimise the round trips to the IO the current status is cached in the openHAB http cache using the following rows

    1: http:ArduinoIOCache.url=http://192.168.1.46
    2: http:ArduinoIOCache.updateInterval=1000



item.cfg
Mapping of the current status of the IO to a Contact item is done like this, where the REGEX is used to search for the current status of a single input in this case ”digitalChannel=0”

    1: Contact HA_CONTACT_TEST_0               "Test 0"                (Indoor) {http="<[ArduinoIOCache:1000:REGEX(.*?<digitalChannel=0>(.*?)</digitalChannel=0>.*)]"}



Arduino HTTP output
Arduino reads and returns input status in form of a semi-XML document

    1: <!DOCTYPE HTML>
    2: <html>
    3: <analogChannel=0>1023</analogChannel=0><br />
    4: <analogChannel=1>1023</analogChannel=1><br />
    5: <analogChannel=2>922</analogChannel=2><br />
    6: <analogChannel=3>786</analogChannel=3><br />
    7: <analogChannel=4>638</analogChannel=4><br />
    8: <analogChannel=5>527</analogChannel=5><br />
    9: <digitalChannel=0>OPEN</digitalChannel=0><br />
   10: <digitalChannel=1>CLOSED</digitalChannel=1><br />
   11: <digitalChannel=2>CLOSED</digitalChannel=2><br />
   12: <digitalChannel=3>CLOSED</digitalChannel=3><br />
   13: <digitalChannel=4>CLOSED</digitalChannel=4><br />
   14: <digitalChannel=5>CLOSED</digitalChannel=5><br />
   15: <digitalChannel=6>CLOSED</digitalChannel=6><br />
   16: <digitalChannel=7>OPEN</digitalChannel=7><br />
   17: <digitalChannel=8>CLOSED</digitalChannel=8><br />
   18: </html>



Arduino Program

    1: /*
    2:   Web Server
    3:
    4: A simple web server that shows the value of the analog inputs pins and digital inputs as semi-XML.
    5: using an Arduino Wiznet Ethernet shield.
    6:
    7: Circuit:
    8: * Ethernet shield attached to pins 10, 11, 12, 13
    9: * Analog inputs attached to pins A0 through A5 (optional)
   10:
   11: created 18 Dec 2009
   12: by David A. Mellis
   13: modified 9 Apr 2012
   14: by Tom Igoe
   15:
   16: modified again 23 feb 2015 by Martin Pålsson
   17:   */
   18:
   19: #include <SPI.h>
   20: #include <Ethernet.h>
   21:
   22: // Enter a MAC address and IP address for your controller below.
   23: // The IP address will be dependent on your local network:
   24: byte mac[] = {
   25:   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
   26: IPAddress ip(192,168,1,177);
   27:
   28: // Initialize the Ethernet server library
   29: // with the IP address and port you want to use
   30: // (port 80 is default for HTTP):
   31: EthernetServer server(80);
   32:
   33: void setup() {
   34:  // Open serial communications and wait for port to open:
   35:   Serial.begin(9600);
   36:    while (!Serial) {
   37:     ; // wait for serial port to connect. Needed for Leonardo only
   38:   }
   39:
   40:
   41:   // start the Ethernet connection and the server:
   42:   Ethernet.begin(mac);
   43:   server.begin();
   44:   Serial.print("server is at ");
   45:   Serial.println(Ethernet.localIP());
   46: }
   47:
   48:
   49: void loop() {
   50:   // listen for incoming clients
   51:   EthernetClient client = server.available();
   52:   if (client) {
   53:     Serial.println("new client");
   54:     // an http request ends with a blank line
   55:     boolean currentLineIsBlank = true;
   56:     while (client.connected()) {
   57:       if (client.available()) {
   58:         char c = client.read();
   59:         Serial.write(c);
   60:         // if you've gotten to the end of the line (received a newline
   61:         // character) and the line is blank, the http request has ended,
   62:         // so you can send a reply
   63:         if (c == '\n' && currentLineIsBlank) {
   64:           // send a standard http response header
   65:           client.println("HTTP/1.1 200 OK");
   66:           client.println("Content-Type: text/html");
   67:           client.println("Connection: close");  // the connection will be closed after completion of the response
   68:           client.println();
   69:           client.println("<!DOCTYPE HTML>");
   70:           client.println("<html>");
   71:           // output the value of each analog input pin
   72:           for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
   73:             int sensorReading = analogRead(analogChannel);
   74:             client.print("<analogChannel=");
   75:             client.print(analogChannel);
   76:             client.print(">");
   77:             client.print(sensorReading);
   78:             client.print("</analogChannel=");
   79:             client.print(analogChannel);
   80:             client.println("><br />");      
   81:           }
   82:           for (int digitalChannel = 0; digitalChannel < 9; digitalChannel++) {
   83:             boolean sensorReading = digitalRead(digitalChannel);
   84:             client.print("<digitalChannel=");
   85:             client.print(digitalChannel);
   86:             client.print(">");
   87:             if((digitalRead(digitalChannel) == true)){
   88:               client.print("OPEN");
   89:             }
   90:             else{
   91:               client.print("CLOSED");
   92:             }
   93:             client.print("</digitalChannel=");
   94:             client.print(digitalChannel);
   95:             client.println("><br />");      
   96:           }
   97:           client.println("</html>");
   98:           break;
   99:         }
  100:         if (c == '\n') {
  101:           // you're starting a new line
  102:           currentLineIsBlank = true;
  103:         }
  104:         else if (c != '\r') {
  105:           // you've gotten a character on the current line
  106:           currentLineIsBlank = false;
  107:         }
  108:       }
  109:     }
  110:     // give the web browser time to receive the data
  111:     delay(1);
  112:     // close the connection:
  113:     client.stop();
  114:     Serial.println("client disonnected");
  115:   }
  116: }