Advertisement
metalx1000

ESP8266 IR Transmitter with WIFI Manager

Mar 17th, 2022
2,332
0
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.  * Copyright 2015 Mark Szabo
  4.  * Copyright 2019 David Conran
  5.  * https://github.com/tzapu/WiFiManager added by Kris Occhipinti for easy of use 2022
  6.  *
  7.  * An IR LED circuit *MUST* be connected to the ESP on a pin
  8.  * as specified by kIrLed below.
  9.  *
  10.  * TL;DR: The IR LED needs to be driven by a transistor for a good result.
  11.  *
  12.  * Suggested circuit:
  13.  *     https://github.com/crankyoldgit/IRremoteESP8266/wiki#ir-sending
  14.  *
  15.  * Common mistakes & tips:
  16.  *   * Don't just connect the IR LED directly to the pin, it won't
  17.  *     have enough current to drive the IR LED effectively.
  18.  *   * Make sure you have the IR LED polarity correct.
  19.  *     See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity
  20.  *   * Typical digital camera/phones can be used to see if the IR LED is flashed.
  21.  *     Replace the IR LED with a normal LED if you don't have a digital camera
  22.  *     when debugging.
  23.  *   * Avoid using the following pins unless you really know what you are doing:
  24.  *     * Pin 0/D3: Can interfere with the boot/program mode & support circuits.
  25.  *     * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere.
  26.  *     * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere.
  27.  *   * ESP-01 modules are tricky. We suggest you use a module with more GPIOs
  28.  *     for your first time. e.g. ESP-12 etc.
  29.  */
  30. #include <Arduino.h>
  31. #if defined(ESP8266)
  32.  
  33. #include <ESP8266WebServer.h>
  34. #include <ESP8266mDNS.h>
  35. #endif  // ESP8266
  36. #if defined(ESP32)
  37. #include <WiFi.h>
  38. #include <WebServer.h>
  39. #include <ESPmDNS.h>
  40. #endif  // ESP32
  41. #include <IRremoteESP8266.h>
  42. #include <IRsend.h>
  43. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  44. #include <WiFiClient.h>
  45.  
  46.  
  47. MDNSResponder mdns;
  48.  
  49. #if defined(ESP8266)
  50. ESP8266WebServer server(80);
  51. #undef HOSTNAME
  52. #define HOSTNAME "esp8266"
  53. #endif  // ESP8266
  54. #if defined(ESP32)
  55. WebServer server(80);
  56. #undef HOSTNAME
  57. #define HOSTNAME "esp32"
  58. #endif  // ESP32
  59.  
  60. const uint16_t kIrLed = 4;  // ESP GPIO pin to use. Recommended: 4 (D2).
  61.  
  62. IRsend irsend(kIrLed);  // Set the GPIO to be used to sending the message.
  63.  
  64. void handleRoot() {
  65.   server.send(200, "text/html",
  66.               "<html>" \
  67.                 "<head><title>" HOSTNAME " Demo </title>" \
  68.                 "<meta http-equiv=\"Content-Type\" " \
  69.                     "content=\"text/html;charset=utf-8\">" \
  70.                 "<meta name=\"viewport\" content=\"width=device-width," \
  71.                     "initial-scale=1.0,minimum-scale=1.0," \
  72.                     "maximum-scale=5.0\">" \
  73.                 "</head>" \
  74.                 "<body>" \
  75.                   "<h1>Hello from " HOSTNAME ", you can send NEC encoded IR" \
  76.                       "signals from here!</h1>" \
  77.                   "<p><a href=\"ir?code=16769055\">Send 0xFFE01F</a></p>" \
  78.                   "<p><a href=\"ir?code=16429347\">Send 0xFAB123</a></p>" \
  79.                   "<p><a href=\"ir?code=16771222\">Send 0xFFE896</a></p>" \
  80.                 "</body>" \
  81.               "</html>");
  82. }
  83.  
  84. void handleIr() {
  85.   for (uint8_t i = 0; i < server.args(); i++) {
  86.     if (server.argName(i) == "code") {
  87.       uint32_t code = strtoul(server.arg(i).c_str(), NULL, 10);
  88. #if SEND_NEC
  89.       irsend.sendNEC(code, 32);
  90. #endif  // SEND_NEC
  91.     }
  92.   }
  93.   handleRoot();
  94. }
  95.  
  96. void handleNotFound() {
  97.   String message = "File Not Found\n\n";
  98.   message += "URI: ";
  99.   message += server.uri();
  100.   message += "\nMethod: ";
  101.   message += (server.method() == HTTP_GET)?"GET":"POST";
  102.   message += "\nArguments: ";
  103.   message += server.args();
  104.   message += "\n";
  105.   for (uint8_t i = 0; i < server.args(); i++)
  106.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  107.   server.send(404, "text/plain", message);
  108. }
  109.  
  110. void setup(void) {
  111.   irsend.begin();
  112.  
  113.      WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  114.     // it is a good practice to make sure your code sets wifi mode how you want it.
  115.  
  116.     // put your setup code here, to run once:
  117.     Serial.begin(115200);
  118.    
  119.     //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  120.     WiFiManager wm;
  121.  
  122.     // reset settings - wipe stored credentials for testing
  123.     // these are stored by the esp library
  124.     //wm.resetSettings();
  125.  
  126.     // Automatically connect using saved credentials,
  127.     // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
  128.     // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
  129.     // then goes into a blocking loop awaiting configuration and will return success result
  130.  
  131.     bool res;
  132.     // res = wm.autoConnect(); // auto generated AP name from chipid
  133.      res = wm.autoConnect("ESP_IR_Transmitter"); // anonymous ap
  134.     //res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
  135.  
  136.     if(!res) {
  137.         Serial.println("Failed to connect");
  138.         // ESP.restart();
  139.     }
  140.     else {
  141.         //if you get here you have connected to the WiFi    
  142.         Serial.println("connected...yeey :)");
  143.     }
  144.  
  145.  
  146.  
  147. #if defined(ESP8266)
  148.   if (mdns.begin(HOSTNAME, WiFi.localIP())) {
  149. #else  // ESP8266
  150.   if (mdns.begin(HOSTNAME)) {
  151. #endif  // ESP8266
  152.     Serial.println("MDNS responder started");
  153.     // Announce http tcp service on port 80
  154.     mdns.addService("http", "tcp", 80);
  155.   }
  156.  
  157.   server.on("/", handleRoot);
  158.   server.on("/ir", handleIr);
  159.  
  160.   server.on("/inline", [](){
  161.     server.send(200, "text/plain", "this works as well");
  162.   });
  163.  
  164.   server.onNotFound(handleNotFound);
  165.  
  166.   server.begin();
  167.   Serial.println("HTTP server started");
  168. }
  169.  
  170. void loop(void) {
  171. #if defined(ESP8266)
  172.   mdns.update();
  173. #endif
  174.   server.handleClient();
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement