logo elektroda
logo elektroda
X
logo elektroda

How to generate a random IR code in the IRServer sketch on the ESP8266?

seat95 876 0
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18385796
    seat95
    Level 1  
    I would like to make a universal remote control device
    The program would send an IR code randomly e.g. every 2ms.
    The codes would be listened to so that I would know which code is responsible for e.g. switching on the device.
    As a remote control sending the code I have ESP8266 with IR diode transmitting, for listening I have Arduino UNO with IR diode receiving

    I am using the IRServer sketch to control the device after entering the IR code.
    In this sketch I would like the code not to be given but to be sent randomly increasing by 1 from the range 1000000000 to 9999999999 every 2ms.
    Unfortunately I don't know much about programming. I am just starting my adventure with Arduino.

    Please help me how to use the random function to send IR code via text/html

    
    #ifndef UNIT_TEST
    #include <Arduino.h>
    #endif
    #include <ESP8266WiFi.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>
    #include <IRremoteESP8266.h>
    #include <IRsend.h>
    #include <WiFiClient.h>
    
    const char* kSsid = "!!!!!!";
    const char* kPassword = "!!!!!!";
    MDNSResponder mdns;
    
    ESP8266WebServer server(80);
    
    const uint16_t kIrLed = 0;  // ESP8266 GPIO pin to use. Recommended: 4 (D2).
    
    IRsend irsend(kIrLed);  // Set the GPIO to be used to sending the message.
    
    void handleRoot() {[size=9][/size]
      server.send(200, "text/html",
                  "<html>" \
                    "<head><title>ESP8266 Pilot Uniwersalny</title></head>" \
                    "<body>" \
                      "<h1>Sterowanie IR przez ESP8266</h1>" \
                      "<p><a href=\"ir?code=1704605895\">Pioneer POWER</a></p>" \
                      "<p><a href=\"ir?code=3251424817\">NETIA POWER</a></p>" \
                      "<p><a href=\"ir?code=3251399317\">NETIA VOL UP</a></p>" \
                      "<p><a href=\"ir?code=3251431957\">NETIA VOL DOWN</a></p>" \
                      "<p><a href=\"ir?code=2704\">SONY POWER</a></p>" \
                                                         
                    "</body>" \
                  "</html>");
    }
    
    void handleIr() {
      for (uint8_t i = 0; i < server.args(); i++) {
        if (server.argName(i) == "code") {
          uint32_t code = strtoul(server.arg(i).c_str(), NULL, 10);
    #if SEND_NEC
          irsend.sendNEC(code, 32);
    #endif  // SEND_NEC
    #if SEND_SONY
    irsend.sendSony(code, 12);
    #endif 
        }
      }
      handleRoot();
    }
    
    void handleNotFound() {
      String message = "File Not Found\n\n";
      message += "URI: ";
      message += server.uri();
      message += "\nMethod: ";
      message += (server.method() == HTTP_GET)?"GET":"POST";
      message += "\nArguments: ";
      message += server.args();
      message += "\n";
      for (uint8_t i = 0; i < server.args(); i++)
        message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
      server.send(404, "text/plain", message);
    }
    
    void setup(void) {
      irsend.begin();
    
      Serial.begin(115200);
      WiFi.begin(kSsid, kPassword);
      Serial.println("");
    
      // Wait for connection
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(kSsid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP().toString());
    
      if (mdns.begin("esp8266", WiFi.localIP())) {
        Serial.println("MDNS responder started");
      }
    
      server.on("/", handleRoot);
      server.on("/ir", handleIr);
    
      server.on("/inline", [](){
        server.send(200, "text/plain", "this works as well");
      });
    
      server.onNotFound(handleNotFound);
    
      server.begin();
      Serial.println("HTTP server started");
    }
    
    void loop(void) {
      server.handleClient();
    }
    
    .
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
ADVERTISEMENT