Arduino Christmas Tree Project

I decided that it was essential to give our customers the ability to turn our Christmas tree lights on and off over the web.


Check out our Christmas Tree API here, Arduino code below.  Live stream to follow!



Code Running on Arduino UNO


  * Using Arduino Uno, Arduino Ethernet Shield, Maplin Relay Shield
  * Maplin codes: NO2DH, N33KU, N30KU
  * Optional: USB B cable for programming, stacking headers, free standing PSU box (enclosure), adapters

  * Notes: Christmas tree lights circuit attached to relay sheild pin 4

  created 18 Dec 2014
  by Nick Kewney


#include <SPI.h>
#include <Ethernet.h>

IPAddress ip(10, 100, 1, 177); // give the shield an IP

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // give the shield a MAC 

unsigned int relayPin = 4;

EthernetServer server(80); // init EthernetServer on port 80

String queryString; // for reading target state

void setup() {
   while (!Serial) { // wait!
   // start the connection and web server:
   Ethernet.begin(mac, ip);
   Serial.print("server is at ");
   pinMode(relayPin,OUTPUT); // set up the code to send relay shield

void loop() {
   // listen for requests
   EthernetClient client = server.available();
   if (client) {
     Serial.println("new switcher just connected");
     // an http request ends with a blank line
     boolean isBlankLine = true;
     while (client.connected()) {
       if (client.available()) {
         char c =;
         // get the light/dark querystring parameters from the HTTP header
        if (queryString.length() < 100) {
          queryString += c; 
         if (c == 'n' && isBlankLine) {
           // send a standard http response header as JSON for our consuming service (
           client.println("HTTP/1.1 200 OK");
           client.println("Content-Type: application/json");
           client.println("Access-Control-Allow-Origin: *"); // needed even for subdomains
           client.println("Connection: close");  // the connection will be closed after completion of the response
          if(queryString.indexOf("light") >0)//checks for light on QS
            digitalWrite(relayPin,HIGH);    // set light pin to HIGH
            client.println("{ "State": "Light" }");
          if(queryString.indexOf("dark") >0)//checks for dark on QS
            digitalWrite(relayPin,LOW);    // set light pin to LOW
            client.println("{ "State": "Dark" }");

          queryString=""; // clear this for next request
         if (c == 'n') {
           isBlankLine = true;
         else if (c != 'r') {
           isBlankLine = false;
     client.stop();  // close the connection:
     Serial.println("client disconnected");


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.