IRServer.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * IRremoteESP8266: IRServer - demonstrates sending IR codes controlled from a webserver
  3. * Version 0.2 June, 2017
  4. * Copyright 2015 Mark Szabo
  5. *
  6. * An IR LED circuit *MUST* be connected to the ESP8266 on a pin
  7. * as specified by kIrLed below.
  8. *
  9. * TL;DR: The IR LED needs to be driven by a transistor for a good result.
  10. *
  11. * Suggested circuit:
  12. * https://github.com/markszabo/IRremoteESP8266/wiki#ir-sending
  13. *
  14. * Common mistakes & tips:
  15. * * Don't just connect the IR LED directly to the pin, it won't
  16. * have enough current to drive the IR LED effectively.
  17. * * Make sure you have the IR LED polarity correct.
  18. * See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity
  19. * * Typical digital camera/phones can be used to see if the IR LED is flashed.
  20. * Replace the IR LED with a normal LED if you don't have a digital camera
  21. * when debugging.
  22. * * Avoid using the following pins unless you really know what you are doing:
  23. * * Pin 0/D3: Can interfere with the boot/program mode & support circuits.
  24. * * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere.
  25. * * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere.
  26. * * ESP-01 modules are tricky. We suggest you use a module with more GPIOs
  27. * for your first time. e.g. ESP-12 etc.
  28. */
  29. #ifndef UNIT_TEST
  30. #include <Arduino.h>
  31. #endif
  32. #include <ESP8266WiFi.h>
  33. #include <ESP8266WebServer.h>
  34. #include <ESP8266mDNS.h>
  35. #include <IRremoteESP8266.h>
  36. #include <IRsend.h>
  37. #include <WiFiClient.h>
  38. const char* kSsid = ".....";
  39. const char* kPassword = ".....";
  40. MDNSResponder mdns;
  41. ESP8266WebServer server(80);
  42. const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2).
  43. IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message.
  44. void handleRoot() {
  45. server.send(200, "text/html",
  46. "<html>" \
  47. "<head><title>ESP8266 Demo</title></head>" \
  48. "<body>" \
  49. "<h1>Hello from ESP8266, you can send NEC encoded IR" \
  50. "signals from here!</h1>" \
  51. "<p><a href=\"ir?code=16769055\">Send 0xFFE01F</a></p>" \
  52. "<p><a href=\"ir?code=16429347\">Send 0xFAB123</a></p>" \
  53. "<p><a href=\"ir?code=16771222\">Send 0xFFE896</a></p>" \
  54. "</body>" \
  55. "</html>");
  56. }
  57. void handleIr() {
  58. for (uint8_t i = 0; i < server.args(); i++) {
  59. if (server.argName(i) == "code") {
  60. uint32_t code = strtoul(server.arg(i).c_str(), NULL, 10);
  61. #if SEND_NEC
  62. irsend.sendNEC(code, 32);
  63. #endif // SEND_NEC
  64. }
  65. }
  66. handleRoot();
  67. }
  68. void handleNotFound() {
  69. String message = "File Not Found\n\n";
  70. message += "URI: ";
  71. message += server.uri();
  72. message += "\nMethod: ";
  73. message += (server.method() == HTTP_GET)?"GET":"POST";
  74. message += "\nArguments: ";
  75. message += server.args();
  76. message += "\n";
  77. for (uint8_t i = 0; i < server.args(); i++)
  78. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  79. server.send(404, "text/plain", message);
  80. }
  81. void setup(void) {
  82. irsend.begin();
  83. Serial.begin(115200);
  84. WiFi.begin(kSsid, kPassword);
  85. Serial.println("");
  86. // Wait for connection
  87. while (WiFi.status() != WL_CONNECTED) {
  88. delay(500);
  89. Serial.print(".");
  90. }
  91. Serial.println("");
  92. Serial.print("Connected to ");
  93. Serial.println(kSsid);
  94. Serial.print("IP address: ");
  95. Serial.println(WiFi.localIP().toString());
  96. if (mdns.begin("esp8266", WiFi.localIP())) {
  97. Serial.println("MDNS responder started");
  98. }
  99. server.on("/", handleRoot);
  100. server.on("/ir", handleIr);
  101. server.on("/inline", [](){
  102. server.send(200, "text/plain", "this works as well");
  103. });
  104. server.onNotFound(handleNotFound);
  105. server.begin();
  106. Serial.println("HTTP server started");
  107. }
  108. void loop(void) {
  109. server.handleClient();
  110. }