IRGCTCPServer.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * IRremoteESP8266: IRGCTCPServer - send Global Cache-formatted codes via TCP.
  3. * An IR emitter must be connected to GPIO pin 4.
  4. * Version 0.2 May, 2017
  5. * Copyright 2016 Hisham Khalifa, http://www.hishamkhalifa.com
  6. * Copyright 2017 David Conran
  7. *
  8. * 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
  9. * For more codes, visit: https://irdb.globalcache.com/
  10. *
  11. * How to use this program:
  12. * 1) Update "kSsid" and "kPassword" below for your WIFI network.
  13. * 2) Compile and upload the sketch to your ESP8266 module.
  14. * 3) (Optional) Use the serial connection to confirm it started and get the
  15. * IP address.
  16. * 4) From a client machine, connect to port 4998 on the ESP8266, using
  17. * 'telnet', 'nc' (netcat), 'putty' or similar command, etc.
  18. * You may need to install one.
  19. * Unix/OSX:
  20. * Start a shell. Then type:
  21. * telnet <esp8266deviceIPaddress> 4998
  22. * Windows:
  23. * Start a new CMD window, then type:
  24. * telnet <esp8266deviceIPaddress> 4998
  25. *
  26. * 5) Enter a Global Cache-formatted code, starting at the frequency,
  27. * and then a return/enter at the end. No spaces. e.g.:
  28. *
  29. * 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
  30. *
  31. * To exit the 'telnet' command:
  32. * press <control> + <]> at the same time, then press 'q', and then <return>.
  33. * or:
  34. * <control> + <d> might work.
  35. *
  36. * This program will display the ESP's IP address on the serial console, or you
  37. * can check your wifi router for it's address.
  38. */
  39. #ifndef UNIT_TEST
  40. #include <Arduino.h>
  41. #endif
  42. #include <ESP8266WiFi.h>
  43. #include <IRremoteESP8266.h>
  44. #include <IRsend.h>
  45. #include <WiFiClient.h>
  46. #include <WiFiServer.h>
  47. const char* kSsid = "..."; // Put your WIFI SSID here.
  48. const char* kPassword = "..."; // Put your WIFI Password here.
  49. WiFiServer server(4998); // Uses port 4998.
  50. WiFiClient client;
  51. uint16_t *code_array;
  52. #define IR_LED 4 // ESP8266 GPIO pin to use. Recommended: 4 (D2).
  53. IRsend irsend(IR_LED); // Set the GPIO to be used to sending the message.
  54. void sendGCString(String str) {
  55. int16_t index;
  56. uint16_t count;
  57. // Find out how many items there are in the string.
  58. index = -1;
  59. count = 1;
  60. do {
  61. index = str.indexOf(',', index + 1);
  62. count++;
  63. } while (index != -1);
  64. // Now we know how many there are, allocate the memory to store them all.
  65. code_array = reinterpret_cast<uint16_t*>(malloc(count * sizeof(uint16_t)));
  66. // Check we malloc'ed successfully.
  67. if (code_array == NULL) { // malloc failed, so give up.
  68. Serial.printf("\nCan't allocate %d bytes. (%d bytes free)\n",
  69. count * sizeof(uint16_t), ESP.getFreeHeap());
  70. Serial.println("Giving up & forcing a reboot.");
  71. ESP.restart(); // Reboot.
  72. delay(500); // Wait for the restart to happen.
  73. return; // Should never get here, but just in case.
  74. }
  75. // Now convert the strings to integers and place them in code_array.
  76. count = 0;
  77. uint16_t start_from = 0;
  78. do {
  79. index = str.indexOf(',', start_from);
  80. code_array[count] = str.substring(start_from, index).toInt();
  81. start_from = index + 1;
  82. count++;
  83. } while (index != -1);
  84. #if SEND_GLOBALCACHE
  85. irsend.sendGC(code_array, count); // All done. Send it.
  86. #endif // SEND_GLOBALCACHE
  87. free(code_array); // Free up the memory allocated.
  88. }
  89. void setup() {
  90. // initialize serial:
  91. Serial.begin(115200);
  92. delay(100);
  93. Serial.println(" ");
  94. Serial.println("IR TCP Server");
  95. WiFi.begin(kSsid, kPassword);
  96. while (WiFi.status() != WL_CONNECTED) {
  97. delay(900);
  98. Serial.print(".");
  99. }
  100. server.begin();
  101. IPAddress myAddress = WiFi.localIP();
  102. Serial.println(myAddress.toString());
  103. irsend.begin();
  104. }
  105. void loop() {
  106. while (!client)
  107. client = server.available();
  108. while (!client.connected()) {
  109. delay(900);
  110. client = server.available();
  111. }
  112. if (client.available()) {
  113. String ir_code_str = client.readStringUntil('\r'); // Exclusive of \r
  114. client.readStringUntil('\n'); // Skip new line as well
  115. client.flush();
  116. sendGCString(ir_code_str);
  117. }
  118. }