Advertisement
RuiViana

CR_ESP_NET

Jan 8th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 KB | None | 0 0
  1. /*
  2.    IRremoteESP8266: IRServer - demonstrates sending IR codes controlled from a webserver
  3.    An IR LED must be connected to ESP8266 pin 0.
  4.    Version 0.1 June, 2015
  5. */
  6. #include <ESP8266WiFi.h>
  7. #include <WiFiClient.h>
  8. #include <ESP8266WebServer.h>
  9. #include <ESP8266mDNS.h>
  10. #include <IRremoteESP8266.h>
  11. const char* ssid = "xxxxxxxxx";
  12. const char* password = "yyyyyyyyyy";
  13. MDNSResponder mdns;
  14. ESP8266WebServer server(80);
  15. IRsend irsend(4);
  16. unsigned long code;
  17. String Teclado = "";
  18. //------------------------------------------------
  19. unsigned long stringToLong(String CodeStr)
  20. {
  21.   char MyArray[12];
  22.   CodeStr.toCharArray(MyArray, sizeof(MyArray));
  23.   return strtoul(MyArray, NULL, 0);
  24. }
  25. //------------------------------------------------
  26. void handleRoot()
  27. {
  28.   server.send(200, "text/html", Teclado);
  29. }
  30. //------------------------------------------------
  31. void handleIr()
  32. {
  33.   for (uint8_t i = 0; i < server.args(); i++)
  34.   {
  35.     if (server.argName(i) == "code")
  36.     {
  37.       String CodeTransl = (server.arg(i));
  38.       code = stringToLong(CodeTransl);
  39.       irsend.sendNEC(code, 32);
  40.     }
  41.   }
  42.   handleRoot();
  43. }
  44. //------------------------------------------------
  45. void handleNotFound()
  46. {
  47.   String message = "File Not Found\n\n";
  48.   message += "URI: ";
  49.   message += server.uri();
  50.   message += "\nMethod: ";
  51.   message += (server.method() == HTTP_GET) ? "GET" : "POST";
  52.   message += "\nArguments: ";
  53.   message += server.args();
  54.   message += "\n";
  55.   for (uint8_t i = 0; i < server.args(); i++)
  56.   {
  57.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  58.   }
  59.   server.send(404, "text/plain", message);
  60. }
  61. //------------------------------------------------
  62. void setup(void)
  63. {
  64.   Teclado += "<html><head> <title>ESP8266 IR</title></head><body><h1>CR Wifi</h1>";
  65.  
  66.   Teclado += "<p><a href=\"ir?code=3782871287\"><button style=\"background-color: rgb(  0, 255,  0);height: 50px; width: 150px;\"><h2>+Canal</h2></button></a>";
  67.   Teclado +=    "<a href=\"ir?code=3782891687\"><button style=\"background-color: rgb(255,   0,  0);height: 50px; width: 150px;\"><h2>-Canal</h2></button></a></p>";
  68.  
  69.   Teclado += "<p><a href=\"ir?code=3782914127\"><button style=\"background-color: rgb(  0, 0, 255);height: 50px; width: 150px;\"><h2>+Vol</h2></button></a>";
  70.   Teclado +=    "<a href=\"ir?code=3782897807\"><button style=\"background-color: rgb(128,128,  0);height: 50px; width: 150px;\"><h2>-Vol</h2></button></a></p>";
  71.  
  72.   Teclado += "<p><a href=\"ir?code=3782932487\"><button style=\"background-color: rgb(128,128,128);height: 25px; width: 70px;\"><h2>Mute</h2></button></a>";
  73.  
  74.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  75.  
  76.   Teclado += "<a href=\"ir?code=3782903927\"><button style=\"background-color: rgb(160, 0,128);height: 25px; width: 70px;\"><h2>Voltar</h2></button></a>";
  77.  
  78.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  79.  
  80.   Teclado += "<a href=\"ir?code=3782879447\"><button style=\"background-color: rgb(160, 0,128);height: 25px; width: 70px;\"><h2>Grade</h2></button></a></p>";
  81.  
  82.   Teclado += "<p><a href=\"ir?code=3782922287\"><button style=\"background-color: rgb(0,255,255);height: 50px; width: 300px;\"><h2>Up</h2></button></a></p>";
  83.  
  84.   Teclado += "<p><a href=\"ir?code=3782924327\"><button style=\"background-color: rgb(128,160,0);height: 50px; width: 100px;\"><h2>Left</h2></button></a>";
  85.   Teclado += "<a href=\"ir?code=3782912087\"><button style=\"background-color: rgb(160, 0,128);height: 50px; width: 100px;\"><h2>Ok</h2></button></a>";
  86.   Teclado += "<a href=\"ir?code=3782883527\"><button style=\"background-color: rgb(160,160, 96);height: 50px; width: 100px;\"><h2>Right</h2></button></a></p>";
  87.  
  88.   Teclado += "<p><a href=\"ir?code=3782881487\"><button style=\"background-color: rgb(0,255,255);height: 50px; width: 300px;\"><h2>Down</h2></button></a></p>";
  89.  
  90.   Teclado += "<p><a href=\"ir?code=3782916167\"><button style=\"background-color: rgb(255,  0,  0);height: 25px; width: 70px;\"><h2>Fav</h2></button></a>";
  91.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  92.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  93.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  94.   Teclado += "<a href=\"ir?code=3782889647\"><button style=\"background-color: rgb(128,128,128);height: 25px; width: 70px;\"><h2>Sair</h2></button></a></p>";
  95.  
  96.   Teclado += "<p><a href=\"ir?code=3782901887\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>1</h2></button></a>";
  97.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  98.   Teclado += "<a href=\"ir?code=3782885567\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>2</h2></button></a>";
  99.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  100.   Teclado += "<a href=\"ir?code=3782918207\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>3</h2></button></a></p>";
  101.  
  102.   Teclado += "<p><a href=\"ir?code=3782877407\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>4</h2></button></a>";
  103.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  104.   Teclado += "<a href=\"ir?code=3782910047\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>5</h2></button></a>";
  105.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  106.   Teclado += "<a href=\"ir?code=3782893727\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>6</h2></button></a></p>";
  107.  
  108.   Teclado += "<p><a href=\"ir?code=3782926367\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>7</h2></button></a>";
  109.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  110.   Teclado += "<a href=\"ir?code=3782873327\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>8</h2></button></a>";
  111.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  112.   Teclado += "<a href=\"ir?code=3782905967\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>9</h2></button></a></p>";
  113.  
  114.   Teclado += "<p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  115.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  116.   Teclado += " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  117.   Teclado += "<a href=\"ir?code=3782869247\"><button style=\"background-color: rgb(255,255,0);height: 25px; width: 70px;\"><h2>0</h2></button></a></p>";
  118.  
  119.   irsend.begin();
  120.   Serial.begin(115200);
  121.   WiFi.begin(ssid, password);
  122.   Serial.println("");
  123.   while (WiFi.status() != WL_CONNECTED)     // Wait for connection
  124.   {
  125.     delay(500);
  126.     Serial.print(".");
  127.   }
  128.   Serial.println("");
  129.   Serial.print("Connected to ");
  130.   Serial.println(ssid);
  131.   Serial.print("IP address: ");
  132.   Serial.println(WiFi.localIP());
  133.  
  134.   if (mdns.begin("esp8266", WiFi.localIP()))
  135.   {
  136.     Serial.println("MDNSponder started");
  137.   }
  138.   server.on("/", handleRoot);
  139.  
  140.   server.on("/ir", handleIr);
  141.   server.on("/inline", []()
  142.   {
  143.     server.send(200, "text/plain", "this works as well");
  144.   });
  145.   server.onNotFound(handleNotFound);
  146.   server.begin();
  147.   Serial.println("HTTP server started");
  148. }
  149. //------------------------------------------------
  150. void loop(void)
  151. {
  152.   server.handleClient();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement