 }
 
 // ------------------------------------------------------------------
+// call URL, return statuscode and JSON
+reply callUrl(String sUrl)
+{
+  reply r;
+
+  r.iStatusCd = 0;
+  r.sReply = "";
+  
+  Serial.print("Connecting to ");
+  Serial.println(SERVER);
+  
+  // Use WiFiClient class to create TCP connections
+  // todo: change to WiFiClientSecure when ready so as to make sniffing a bit more complex
+  WiFiClient client;
+
+  // todo: we should probably wait here or sleep here until we can connect
+  // so we do not drain the battery too much
+  if (!client.connect(SERVER, PORT)) {
+    Serial.println("Connection failed, trying later...");
+    return r;
+  }
+  
+  // We now create a URI for the request
+  Serial.print("Requesting URL: ");
+  Serial.println(sUrl);
+  
+  // This will send the request to the server
+  client.print(String("GET ") + sUrl + " HTTP/1.1\r\n" + "Host: " + SERVER + "\r\n" + "Connection: close\r\n\r\n");
+               
+  // just a short delay to give other components the chance to do something else
+  delay(100);
+
+  // handling of response
+  Serial.println("");
+  Serial.println("Response:");
+  
+  // Read line by line
+  while(client.available())
+  {
+    String sLine = client.readStringUntil('\r');
+
+    // make sure to not have whitespaces so that the very basic parser will not fail
+    Serial.print(sLine);
+    sLine.trim();
+
+    if (sLine.startsWith("HARDAC-STATUS: ")) 
+    {
+        String sStatus = sLine.substring(14, 18);
+
+        r.iStatusCd = (int)sStatus.toInt();
+    } 
+    // todo: how can we detect the JSON payload?
+    else if (sLine.startsWith("{ \"colour_1\": ")) 
+    {
+      r.sReply = sLine;
+    }
+  }
+
+  Serial.println("EOF.");
+  if (!client.connected()) 
+  {
+    // free up memory
+    client.stop();
+  }
+  
+  return r;
+}
+
+// ------------------------------------------------------------------
+bool registerPJ()
+{
+  // register with backend  
+  String sUrl = "/" + sDeviceId + "/r/public_key/";
+  reply r = callUrl(sUrl);
+
+  if(r.iStatusCd != 201)
+  {
+    Serial.print("Registration of PJ failed with StatusCode: ");
+    Serial.println(r.iStatusCd);
+  }
+  else
+  {
+    // registration successful, make sure, that device knows after reboot
+    EEPROM.write(EEPROM_REGKEY_ADDRESS, REGISTERED);
+    EEPROM.commit();
+    Serial.println("Registration of PJ succeeded.");
+  }
+}
+
+// todo: unregister when asked by backend
+bool unregisterPJ()
+{
+}
+
+// ------------------------------------------------------------------
 // is called once after powering up, sets up the wireless network 
-// and reports status on serial console (for now)
+// checks a few setting, especially if already registered
 void setup() {
 
   // start a serial console for debugging purposes

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  

  // todo: check if registered already (check EEPROM), register with HARDAC otherwise.
}

// ------------------------------------------------------------------
// will be called right after setup and then in a loop after every deep sleep cycle
void loop() {

  // todo: remove me when implementing deep sleep
  //delay(5000);

  // todo: check if button was pressed
  // if so, send ballot to server
  // otherwise just ask for current status

  Serial.print("Connecting to ");
  Serial.println(server);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(server, port)) {
    Serial.println("Connection failed, trying later...");
    return;
  }
  
  // We now create a URI for the request
  String url = "/device_id/s/";
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");
               
  // just a short delay to give other components the chance to do something else
  delay(10);
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available())
  {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Closing connection, going to deep sleep");
  delay(1000);

  // go to deepsleep for one minutes
  system_deep_sleep_set_option(0);
  //system_deep_sleep(1 * 60 * 1000000);
  system_deep_sleep(1 * 10 * 1000000);
  delay(1000);

}


 */

#include <ESP8266WiFi.h>
#include "Esp.h" // deep sleep and system info
#include <ArduinoJson.h> // json parser
#include <EEPROM.h> // to store status/registration in

// ESP8266 SDK headers are in C, they need to be included externally or else 
// they will throw an error compiling/linking.
//
// all SDK .c files required can be added between the { }
//extern "C" {
//}

// change these to your setup
const char* SSID  = "hardac";
const char* PWD   = "hardac";

// where your hardac server is running
const char* SERVER  = "0.0.0.0";
const int PORT      = 8765;

// EEPROM addressing
const int EEPROM_REGKEY_ADDRESS = 0;
// value if registered
const byte REGISTERED = 0x5F;

// http reply struct
struct REPLY 
{
  int iStatusCd;
  String sReply;
};
typedef struct REPLY reply;

// device_id
String sDeviceId = "notinitialised";

// ------------------------------------------------------------------
// used to turn off GPIO Pi
