 
   if ( requestedUri.endsWith("/config") )
   {
-
-    StaticJsonBuffer<1024> jsonBuffer;
-    JsonObject& root = jsonBuffer.createObject();
+    aJsonObject *root;
+    root = aJson.createObject();
     addConfigJson(root);
-
-    root.prettyPrintTo(Serial);
-    WiFiClient client = HTTP.client();
-    root.prettyPrintTo((Print&)client); // Thanks me-no-dev for the "(Print&)"
-
+    sendJson(root);
   }
 
   else if ((requestedUri.startsWith("/api") and (requestedUri.lastIndexOf("/") == 4 )))
   {
     // Serial.println("Respond with complete json as in https://github.com/probonopd/ESP8266HueEmulator/wiki/Hue-API#get-all-information-about-the-bridge");
-
-    StaticJsonBuffer<2048> jsonBuffer;
-    JsonObject& root = jsonBuffer.createObject();
-    JsonObject& groups = root.createNestedObject("groups");
-    JsonObject& scenes = root.createNestedObject("scenes");
-    JsonObject& config = root.createNestedObject("config");
+    aJsonObject *root;
+    root = aJson.createObject();
+    aJsonObject *groups;
+    aJson.addItemToObject(root, "groups", groups = aJson.createObject());
+    aJsonObject *scenes;
+    aJson.addItemToObject(root, "scenes", scenes = aJson.createObject());
+    aJsonObject *config;
+    aJson.addItemToObject(root, "config", config = aJson.createObject());
     addConfigJson(config);
-    JsonObject& lights = root.createNestedObject("lights");
-    addLightJson(lights, 1); // I think this is prone to crashes; see https://github.com/bblanchon/ArduinoJson/issues/87
-//    for (int i = 1; i < 2; i++)
-//    {
-//      addLightJson(root, i); // This crashes when i>1; see https://github.com/bblanchon/ArduinoJson/issues/87
-//    }
-    JsonObject& schedules = root.createNestedObject("schedules");
-    root.prettyPrintTo(Serial);
-    WiFiClient client = HTTP.client();
-    root.prettyPrintTo((Print&)client); // Thanks me-no-dev for the "(Print&)"
+    aJsonObject *lights;
+    aJson.addItemToObject(root, "lights", lights = aJson.createObject());
+    for (int i = 1; i <= 8; i++) // FIXME: Why does this not work for more than 8?
+      addLightJson(lights, i);
+    aJsonObject *schedules;
+    aJson.addItemToObject(root, "schedules", schedules = aJson.createObject());
+    sendJson(root);
+
   }
 
   else if (requestedUri.endsWith("/api"))

// TODO: Change SSDP to get rid of local copies of ESP8266SSDP use the new scheme from the Arduino IDE instead
// https://github.com/esp8266/Arduino/commit/f5ba04d46c0b6df75348f005e68411a856f89e48
// can then get rid of the SSDP.update();

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUDP.h>
#include "ESP8266SSDP.h"
#include <NeoPixelBus.h> // NeoPixelAnimator branch
#include <ArduinoJson.h>

#include "/secrets.h" // Delete this line and populate the following
//const char* ssid = "********";

  return LocalIP;
}

void addConfigJson(aJsonObject *root)
{
  aJson.addStringToObject(root, "name", "Philips hue");
  aJson.addStringToObject(root, "swversion", "01005215");
  aJson.addBooleanToObject(root, "portalservices", false);
  aJson.addStringToObject(root, "zigbeechannel", "0"); // As per spec, 0 is allowed
  aJson.addStringToObject(root, "mac", macString.c_str());
  aJson.addBooleanToObject(root, "dhcp", true);
  aJson.addStringToObject(root, "ipaddress", ipString.c_str());
  aJson.addStringToObject(root, "netmask", "255.255.255.0"); // TODO: FIXME
  aJson.addStringToObject(root, "gateway", "192.168.0.1"); // TODO: FIXME
  aJson.addStringToObject(root, "proxyaddress", "");
  aJson.addNumberToObject(root, "proxyport", 0);
  aJson.addStringToObject(root, "UTC", "2012-10-29T12:05:00");
  aJsonObject *whitelist;
  aJson.addItemToObject(root, "whitelist", whitelist = aJson.createObject());
  aJsonObject *whitelistFirstEntry;
  aJson.addItemToObject(whitelist, "e7x4kuCaC8h885jo", whitelistFirstEntry = aJson.createObject()); // FIXME: Do not hardcode e7x4kuCaC8h885jo
  aJson.addStringToObject(whitelistFirstEntry, "name", "clientname#devicename");
  aJson.addStringToObject(whitelistFirstEntry, "last use date", "2015-07-05T16:48:18");
  aJson.addStringToObject(whitelistFirstEntry, "create date", "2015-07-05T16:48:17");
  aJsonObject *swupdate;
  aJson.addItemToObject(root, "swupdate", swupdate = aJson.createObject());
  aJson.addStringToObject(swupdate, "text", "");
  aJson.addBooleanToObject(swupdate, "notify", false);
  aJson.addNumberToObject(swupdate, "updatestate", 0);
  aJson.addStringToObject(swupdate, "url", "");
}

void addLightJson(aJsonObject* root, int numberOfTheLight)
{
  String lightName = "" + (String) numberOfTheLight;
  aJsonObject *light;
  aJson.addItemToObject(root, lightName.c_str(), light = aJson.createObject());
  aJson.addStringToObject(light, "type", "Extended color light");
  aJson.addStringToObject(light, "name",  ("Hue Lamp " + (String) numberOfTheLight).c_str());
  aJson.addStringToObject(light, "modelid", "LCT001");
  aJsonObject *state;
  aJson.addItemToObject(light, "state", state = aJson.createObject());
  unsigned int brightness = strip.GetPixelColor(numberOfTheLight - 1).CalculateBrightness();
  if (brightness == 0)
  {
    aJson.addBooleanToObject(state, "on", false);
  }
  else
  {
    aJson.addBooleanToObject(state, "on", true);
  }
  aJson.addNumberToObject(state, "bri", 254); // Should be 1-254 according to Philips API
  aJson.addNumberToObject(state, "hue", 0); // Should between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue
  aJson.addNumberToObject(state, "sat", 0);
  double numbers[2] = {0.0, 0.0};
  aJson.addItemToObject(state, "xy", aJson.createFloatArray(numbers, 2));
  aJson.addStringToObject(state, "alert", "none");
  aJson.addStringToObject(state, "effect", "none");
  aJson.addStringToObject(state, "colormode", "hs");
  aJson.addBooleanToObject(state, "reachable", true);

}
