Guest User

Untitled

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