 /*
  * IRremoteESP8266: IRGCTCPServer - send Global Cache-formatted codes via TCP.
  * An IR emitter must be connected to GPIO pin 4.
- * Version 0.1 1 April, 2016
+ * Version 0.2  May, 2017
  * Copyright 2016 Hisham Khalifa, http://www.hishamkhalifa.com
+ * Copyright 2017 David Conran
  *
  * Example command - Samsung TV power toggle: 38000,1,1,170,170,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,63,20,63,20,63,20,63,20,63,20,63,20,1798\r\n
+ * For more codes, visit: https://irdb.globalcache.com/
+ *
+ * How to use this program:
+ *   1) Update "ssid" and "password" below for your WIFI network.
+ *   2) Compile and upload the sketch to your ESP8266 module.
+ *   3) (Optional) Use the serial connection to confirm it started and get the
+ *      IP address.
+ *   4) From a client machine, connect to port 4998 on the ESP8266, using
+ *      'telnet', 'nc' (netcat), 'putty' or similar command, etc.
+ *      You may need to install one.
+ *   Unix/OSX:
+ *     Start a shell. Then type:
+ *       telnet <esp8266deviceIPaddress> 4998
+ *   Windows:
+ *     Start a new CMD window, then type:
+ *       telnet <esp8266deviceIPaddress> 4998
+ *
+ *   5) Enter a Global Cache-formatted code, starting at the frequency,
+ *      and then a return/enter at the end. No spaces. e.g.:
+ *
+ *   38000,1,1,170,170,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,63,20,63,20,63,20,63,20,63,20,63,20,1798
+ *
+ *   To exit the 'telnet' command:
+ *     press <control> + <]> at the same time, then press 'q', and then <return>.
+ *   or:
+ *     <control> + <d> might work.
+ *
+ * This program will display the ESP's IP address on the serial console, or you
+ * can check your wifi router for it's address.
  */
 
 #include <ESP8266WiFi.h>

#include <WiFiClient.h>
#include <WiFiServer.h>

const char* ssid = "...";
const char* password = "...";

WiFiServer server(4998);  // Uses port 4998.
WiFiClient client;

uint16_t *codeArray;
IRsend irsend(4);  // an IR emitter led is connected to GPIO pin 4

void parseString(String str) {
  int16_t nextIndex;
  uint16_t codeLength = 1;
  uint16_t currentIndex = 0;
  nextIndex = str.indexOf(',');

  // change to do/until and remove superfluous repetition below...
  while (nextIndex != -1) {
    if (codeLength > 1)
      codeArray = reinterpret_cast<uint16_t*>(
          realloc(codeArray, codeLength * sizeof(uint16_t)));
    else
      codeArray = reinterpret_cast<uint16_t*>(
          malloc(codeLength * sizeof(uint16_t)));

    codeArray[codeLength - 1] = (uint16_t) (
        str.substring(currentIndex, nextIndex).toInt());

    codeLength++;
    currentIndex = nextIndex + 1;
    nextIndex = str.indexOf(',', currentIndex);
  }
  codeArray = reinterpret_cast<uint16_t*>(
      realloc(codeArray, codeLength * sizeof(uint16_t)));
  codeArray[codeLength - 1] = (uint16_t) (
      str.substring(currentIndex, nextIndex).toInt());

  irsend.sendGC(codeArray, codeLength);
}

void setup() {
  // initialize serial:
  Serial.begin(115200);
  Serial.println(" ");
  Serial.println("IR TCP Server");


  }

  if (client.available()) {
    String ir_code_str = client.readStringUntil('\r');  // Exclusive of \r
    client.readStringUntil('\n');  // Skip new line as well
    client.flush();
    sendGCString(ir_code_str);
  }
}
