En este tutorial crearemos un web server para el control de cualquier dispositivo, que se maneje por infrarrojos, como televisores, equipos de audio, DVD etc. Para ello vamos a usar el esp32 con dos sensores infrarrojos uno receptor, para capturar los comandos de mandos infrarrojos, y otro sensor emisor, para enviar el código obtenido, y de esa forma controlar el dispositivo electrónico hogareño. Analizaremos el código fuente, creado para tal fin, también hablaremos como hacer la aplicación que va a interactuar con el web server interno del esp32, y finalmente haremos una prueba del funcionamiento de todo este sistema.
Tal vez pueda interesarte proyectos en arduino, pic, robótica, telecomunicaciones, suscribete en http://www.youtube.com/user/carlosvolt?sub_confirmation=1 mucho videos con código fuentes completos y diagramas
Componentes electrónicos
Un Esp32
Características del módulo ESP32-T
Conectividad
El módulo ESP32 dispone de todas las variantes del WiFi:
- 802.11 b/g/n/e/i/n
- Wi-Fi Direct (P2P), P2P Discovery, P2P Group Owner mode and P2P Power Management
Esta versión nueva incluye la conectividad mediante Bluethoot de bajo consumo
- Bluetooth v4.2 BR/EDR and BLE
- BLE Beacon
Además, puede comunicarse mediante los protocoles SPI, I2C, UART, MAC Ethernet, Host SD
Prestaciones del microcontrolador
La CPU está formado por un SoC modelo Tensilica LX6 con las siguientes características y memoria
- Doble núcleo de 32 bits con velocidad de 160MHz
- Memoria ROM de 448 kBytes
- Memoria SRAM de 520kBytes
Dispne de 48 Pines
- 18 ADC de 12 bits
- 2 DAC de 8 bits
- 10 pines sensores de contacto
- 16 PWM
- 20 Entradas/salidas digitales
Alimentación y modos de consumo
Para un correcto funcionamiento del ESP32 es necesario subministrar un voltaje de entre 2,8V y 3,6V. La energía que consume depende del modo de funcionamiento. Contiene un modo, el Ultra Low Power Solution (ULP), en que se continúan realizando tareas básicas (ADC, RTC…) en el modo Sleep.
Pines hembra
Módulo ky-005 (emisor infrarrojo)
El módulo transmisor de infrarrojos KY-005 consiste en solo un LED emisor IR de 5 mm.
ESPECIFICACIONES TÉCNICAS
Emite un haz de luz infrarroja a una frecuencia de 38 KHz.
Voltaje de funcionamiento: 5 Volts
Corriente alimentación: 30 a 60 mA CD
Consumo de energía: 90 mW
Temperatura de funcionamiento: -25 °C a 80 °C [-13 °F a 176 °F]
Dimensiones: 18.5 mm x 15 mm [0.728 in x 0.5905 in]
Peso: 2 gr
Módulo receptor infrarrojo ky-022
Tamaño: 6.4 * 7.4 * 5.1MM, ángulo de aceptación 90 °, voltaje de trabajo 2.7-5.5V.
Frecuencia 37.9KHZ, recibiendo la distancia 18 m.
Rechazo de luz diurna hasta 500LUX, capacidad de interferencia electromagnética, IC dedicado de infrarrojos incorporado.
Ampliamente utilizado: estéreo, TV, VCR, CD, decodificadores, marco de fotos digital, audio para el automóvil, juguetes de control remoto, receptores de satélite, disco duro, aire acondicionado, calefacción, ventiladores, iluminación y otros electrodomésticos.
Pinout:
1 …. GND (-)
2 …. + 5V
3 …. Salida (S)
PCB
Código Fuente Receptor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
//------------------------------------------------------------------------------ // Include the IRremote library header // #include <IRremote.h> //------------------------------------------------------------------------------ // Tell IRremote which Arduino pin is connected to the IR Receiver (TSOP4838) // int recvPin = 15; IRrecv irrecv(recvPin); //+============================================================================= // Configure the Arduino // void setup ( ) { Serial.begin(115200); // Status message will be sent to PC at 115200 baud irrecv.enableIRIn(); // Start the receiver } //+============================================================================= // Display IR code // void ircode (decode_results *results) { // Panasonic has an Address if (results->decode_type == PANASONIC) { Serial.print(results->address, HEX); Serial.print(":"); } // Print Code Serial.print(results->value, HEX); } //+============================================================================= // Display encoding type // void encoding (decode_results *results) { switch (results->decode_type) { default: case UNKNOWN: Serial.print("UNKNOWN"); break ; case NEC: Serial.print("NEC"); break ; case SONY: Serial.print("SONY"); break ; case RC5: Serial.print("RC5"); break ; case RC6: Serial.print("RC6"); break ; case DISH: Serial.print("DISH"); break ; case SHARP: Serial.print("SHARP"); break ; case JVC: Serial.print("JVC"); break ; // case SANYO: Serial.print("SANYO"); break ; // case MITSUBISHI: Serial.print("MITSUBISHI"); break ; case SAMSUNG: Serial.print("SAMSUNG"); break ; case LG: Serial.print("LG"); break ; case WHYNTER: Serial.print("WHYNTER"); break ; // case AIWA_RC_T501: Serial.print("AIWA_RC_T501"); break ; case PANASONIC: Serial.print("PANASONIC"); break ; case DENON: Serial.print("Denon"); break ; } } //+============================================================================= // Dump out the decode_results structure. // void dumpInfo (decode_results *results) { // Check if the buffer overflowed if (results->overflow) { Serial.println("IR code too long. Edit IRremoteInt.h and increase RAWBUF"); return; } // Show Encoding standard Serial.print("Encoding : "); encoding(results); Serial.println(""); // Show Code & length Serial.print("Code : "); ircode(results); Serial.print(" ("); Serial.print(results->bits, DEC); Serial.println(" bits)"); } //+============================================================================= // Dump out the decode_results structure. // void dumpRaw (decode_results *results) { // Print Raw data Serial.print("Timing["); Serial.print(results->rawlen-1, DEC); Serial.println("]: "); for (int i = 1; i < results->rawlen; i++) { unsigned long x = results->rawbuf[i] * USECPERTICK; if (!(i & 1)) { // even Serial.print("-"); if (x < 1000) Serial.print(" ") ; if (x < 100) Serial.print(" ") ; Serial.print(x, DEC); } else { // odd Serial.print(" "); Serial.print("+"); if (x < 1000) Serial.print(" ") ; if (x < 100) Serial.print(" ") ; Serial.print(x, DEC); if (i < results->rawlen-1) Serial.print(", "); //',' not needed for last one } if (!(i % 8)) Serial.println(""); } Serial.println(""); // Newline } //+============================================================================= // Dump out the decode_results structure. // void dumpCode (decode_results *results) { // Start declaration Serial.print("unsigned int "); // variable type Serial.print("rawData["); // array name Serial.print(results->rawlen - 1, DEC); // array size Serial.print("] = {"); // Start declaration // Dump data for (int i = 1; i < results->rawlen; i++) { Serial.print(results->rawbuf[i] * USECPERTICK, DEC); if ( i < results->rawlen-1 ) Serial.print(","); // ',' not needed on last one if (!(i & 1)) Serial.print(" "); } // End declaration Serial.print("};"); // // Comment Serial.print(" // "); encoding(results); Serial.print(" "); ircode(results); // Newline Serial.println(""); // Now dump "known" codes if (results->decode_type != UNKNOWN) { // Some protocols have an address if (results->decode_type == PANASONIC) { Serial.print("unsigned int addr = 0x"); Serial.print(results->address, HEX); Serial.println(";"); } // All protocols have data Serial.print("unsigned int data = 0x"); Serial.print(results->value, HEX); Serial.println(";"); } } //+============================================================================= // The repeating section of the code // void loop ( ) { decode_results results; // Somewhere to store the results if (irrecv.decode(&results)) { // Grab an IR code dumpInfo(&results); // Output the results dumpRaw(&results); // Output the results in RAW format dumpCode(&results); // Output the results as source code Serial.println(""); // Blank line between entries irrecv.resume(); // Prepare for the next value } } |
Archivo con definición de pines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
/* * PinDefinitionsAndMore.h * * Contains pin definitions for IRremote examples for various platforms * as well as definitions for feedback LED and tone() and includes * * Copyright (C) 2021-2022 Armin Joachimsmeyer * armin.joachimsmeyer@gmail.com * * This file is part of IRremote https://github.com/Arduino-IRremote/Arduino-IRremote. * * Arduino-IRremote is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>. * */ /* * Pin mapping table for different platforms * * Platform IR input IR output Tone Core/Pin schema * -------------------------------------------------------------- * DEFAULT/AVR 2 3 4 * ATtinyX5 0|PB0 4|PB4 3|PB3 * ATtiny167 3|PA3 2|PA2 7|PA7 ATTinyCore * ATtiny167 9|PA3 8|PA2 5|PA7 Digispark pro * ATtiny3217 18|PA1 19|PA2 20|PA3 MegaTinyCore * ATtiny1604 2 3|PA5 % * SAMD21 3 4 5 * ESP8266 14|D5 12|D6 % * ESP32 15 4 27 * BluePill PA6 PA7 PA3 * APOLLO3 11 12 5 * RP2040 3|GPIO15 4|GPIO16 5|GPIO17 */ //#define _IR_MEASURE_TIMING // For debugging purposes. /* * We do not have pin restrictions for this CPU's, so lets use the hardware PWM for send carrier signal generation */ #if defined(ESP32) || defined(ARDUINO_ARCH_RP2040) || defined(PARTICLE) #define SEND_PWM_BY_TIMER #endif #if defined(ESP8266) #define FEEDBACK_LED_IS_ACTIVE_LOW // The LED on my board (D4) is active LOW #define IR_RECEIVE_PIN 14 // D5 #define IR_RECEIVE_PIN_STRING "D5" #define IR_SEND_PIN 12 // D6 - D4/pin 2 is internal LED #define IR_SEND_PIN_STRING "D6" #define _IR_TIMING_TEST_PIN 13 // D7 #define APPLICATION_PIN 0 // D3 #define tone(...) void() // tone() inhibits receive timer #define noTone(a) void() #define TONE_PIN 42 // Dummy for examples using it #elif defined(ESP32) #include <Arduino.h> #define TONE_LEDC_CHANNEL 1 // Using channel 1 makes tone() independent of receiving timer -> No need to stop receiving timer. void tone(uint8_t _pin, unsigned int frequency){ ledcAttachPin(_pin, TONE_LEDC_CHANNEL); ledcWriteTone(TONE_LEDC_CHANNEL, frequency); } void tone(uint8_t _pin, unsigned int frequency, unsigned long duration){ ledcAttachPin(_pin, TONE_LEDC_CHANNEL); ledcWriteTone(TONE_LEDC_CHANNEL, frequency); delay(duration); ledcWriteTone(TONE_LEDC_CHANNEL, 0); } void noTone(uint8_t _pin){ ledcWriteTone(TONE_LEDC_CHANNEL, 0); } #define IR_RECEIVE_PIN 15 // D15 #define IR_SEND_PIN 4 // D4 #define TONE_PIN 27 // D27 25 & 26 are DAC0 and 1 #define APPLICATION_PIN 16 // RX2 pin #elif defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_STM32F1) // BluePill // Timer 3 blocks PA6, PA7, PB0, PB1 for use by Servo or tone() #define IR_RECEIVE_PIN PA6 #define IR_RECEIVE_PIN_STRING "PA6" #define IR_SEND_PIN PA7 #define IR_SEND_PIN_STRING "PA7" #define TONE_PIN PA3 #define _IR_TIMING_TEST_PIN PA5 #define APPLICATION_PIN PA2 #elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) // Digispark board #include "ATtinySerialOut.hpp" // Available as Arduino library "ATtinySerialOut". saves 370 bytes program space and 38 bytes RAM for digistump core #define IR_RECEIVE_PIN 0 #define IR_SEND_PIN 4 // Pin 2 is serial output with ATtinySerialOut. Pin 1 is internal LED and Pin3 is USB+ with pullup on Digispark board. #define TONE_PIN 3 #define _IR_TIMING_TEST_PIN 3 #elif defined(__AVR_ATtiny87__) || defined(__AVR_ATtiny167__) // Digispark pro board #include "ATtinySerialOut.hpp" // Available as Arduino library "ATtinySerialOut" // For ATtiny167 Pins PB6 and PA3 are usable as interrupt source. # if defined(ARDUINO_AVR_DIGISPARKPRO) #define IR_RECEIVE_PIN 9 // PA3 - on Digispark board labeled as pin 9 //#define IR_RECEIVE_PIN 14 // PB6 / INT0 is connected to USB+ on DigisparkPro boards #define IR_SEND_PIN 8 // PA2 - on Digispark board labeled as pin 8 #define TONE_PIN 5 // PA7 #define _IR_TIMING_TEST_PIN 10 // PA4 # else #define IR_RECEIVE_PIN 3 #define IR_SEND_PIN 2 #define TONE_PIN 7 # endif #elif defined(__AVR_ATtiny88__) // MH-ET Tiny88 board #include "ATtinySerialOut.hpp" // Available as Arduino library "ATtinySerialOut". Saves 128 bytes program space // Pin 6 is TX pin 7 is RX #define IR_RECEIVE_PIN 3 // INT1 #define IR_SEND_PIN 4 #define TONE_PIN 9 #define _IR_TIMING_TEST_PIN 8 #elif defined(__AVR_ATtiny1616__) || defined(__AVR_ATtiny3216__) || defined(__AVR_ATtiny3217__) // Tiny Core Dev board #define IR_RECEIVE_PIN 18 #define IR_SEND_PIN 19 #define TONE_PIN 20 #define APPLICATION_PIN 0 // PA4 #undef LED_BUILTIN // No LED available on the TinyCore 32 board, take the one on the programming board which is connected to the DAC output #define LED_BUILTIN 2 // PA6 #elif defined(__AVR_ATtiny1604__) #define IR_RECEIVE_PIN 2 // To be compatible with interrupt example, pin 2 is chosen here. #define IR_SEND_PIN 3 #define APPLICATION_PIN 5 #define tone(...) void() // Define as void, since TCB0_INT_vect is also used by tone() #define noTone(a) void() #define TONE_PIN 42 // Dummy for examples using it # elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \ || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \ || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \ || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \ || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega32__) \ || defined(__AVR_ATmega16__) || defined(__AVR_ATmega8535__) \ || defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) \ || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__) \ || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega162__) #define IR_RECEIVE_PIN 2 #define IR_SEND_PIN 13 #define TONE_PIN 4 #define APPLICATION_PIN 5 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 6 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 7 #elif defined(ARDUINO_ARCH_APOLLO3) // Sparkfun Apollo boards #define IR_RECEIVE_PIN 11 #define IR_SEND_PIN 12 #define TONE_PIN 5 #elif defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_MBED_NANO) // Arduino Nano 33 BLE #define IR_RECEIVE_PIN 3 // GPIO15 Start with pin 3 since pin 2|GPIO25 is connected to LED on Pi pico #define IR_SEND_PIN 4 // GPIO16 #define TONE_PIN 5 #define APPLICATION_PIN 6 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 7 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 8 #elif defined(ARDUINO_ARCH_RP2040) // Arduino Nano Connect, Pi Pico with arduino-pico core https://github.com/earlephilhower/arduino-pico #define IR_RECEIVE_PIN 15 // to be compatible with the Arduino Nano RP2040 Connect (pin3) #define IR_SEND_PIN 16 #define TONE_PIN 17 #define APPLICATION_PIN 18 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 19 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 20 // If you program the Nano RP2040 Connect with this core, then you must redefine LED_BUILTIN // and use the external reset with 1 kOhm to ground to enter UF2 mode //#undef LED_BUILTIN //#define LED_BUILTIN 6 #elif defined(PARTICLE) // !!!UNTESTED!!! #define IR_RECEIVE_PIN A4 #define IR_SEND_PIN A5 // Particle supports multiple pins #define LED_BUILTIN D7 /* * 4 times the same (default) layout for easy adaption in the future */ #elif defined(TEENSYDUINO) #define IR_RECEIVE_PIN 2 #define IR_SEND_PIN 3 #define TONE_PIN 4 #define APPLICATION_PIN 5 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 6 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 7 #elif defined(__AVR__) // Default as for ATmega328 like on Uno, Nano etc. #define IR_RECEIVE_PIN 2 // To be compatible with interrupt example, pin 2 is chosen here. #define IR_SEND_PIN 3 #define TONE_PIN 4 #define APPLICATION_PIN 5 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 6 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 7 # if defined(ARDUINO_AVR_PROMICRO) // Sparkfun Pro Micro is __AVR_ATmega32U4__ but has different external circuit // We have no built in LED at pin 13 -> reuse RX LED #undef LED_BUILTIN #define LED_BUILTIN LED_BUILTIN_RX # endif #elif defined(ARDUINO_ARCH_MBED) // Arduino Nano 33 BLE #define IR_RECEIVE_PIN 2 #define IR_SEND_PIN 3 #define TONE_PIN 4 #define APPLICATION_PIN 5 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 6 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 7 #elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) #define IR_RECEIVE_PIN 2 #define IR_SEND_PIN 3 #define TONE_PIN 4 #define APPLICATION_PIN 5 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 6 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 7 // On the Zero and others we switch explicitly to SerialUSB #define Serial SerialUSB // Definitions for the Chinese SAMD21 M0-Mini clone, which has no led connected to D13/PA17. // Attention!!! D2 and D4 are switched on these boards!!! // If you connect the LED, it is on pin 24/PB11. In this case activate the next two lines. //#undef LED_BUILTIN //#define LED_BUILTIN 24 // PB11 // As an alternative you can choose pin 25, it is the RX-LED pin (PB03), but active low.In this case activate the next 3 lines. //#undef LED_BUILTIN //#define LED_BUILTIN 25 // PB03 //#define FEEDBACK_LED_IS_ACTIVE_LOW // The RX LED on the M0-Mini is active LOW #elif defined (NRF51) // BBC micro:bit #define IR_RECEIVE_PIN 2 #define IR_SEND_PIN 3 #define APPLICATION_PIN 1 #define _IR_TIMING_TEST_PIN 4 #define tone(...) void() // no tone() available #define noTone(a) void() #define TONE_PIN 42 // Dummy for examples using it #else #warning Board / CPU is not detected using pre-processor symbols -> using default values, which may not fit. Please extend PinDefinitionsAndMore.h. // Default valued for unidentified boards #define IR_RECEIVE_PIN 2 #define IR_SEND_PIN 3 #define TONE_PIN 4 #define APPLICATION_PIN 5 #define ALTERNATIVE_IR_FEEDBACK_LED_PIN 6 // E.g. used for examples which use LED_BUILDIN for example output. #define _IR_TIMING_TEST_PIN 7 #endif // defined(ESP8266) #if !defined (FLASHEND) #define FLASHEND 0xFFFF // Dummy value for platforms where FLASHEND is not defined #endif /* * Helper macro for getting a macro definition as string */ #if !defined(STR_HELPER) #define STR_HELPER(x) #x #define STR(x) STR_HELPER(x) #endif |
Código Fuente Emisor
La versión de la librería IRremote es la 3.6.1 que se puede descar del mismo ide de arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
#include <Arduino.h> #include "PinDefinitionsAndMore.h"//Configuración y definición de pines #include <IRremote.hpp> #include <WiFi.h> const char* ssid = "Tu_red_wifi"; const char* password = "Tu_clave"; //configuración ip estática para esp32 IPAddress ip(192,168,1,78); IPAddress gateway(192,168,1,1); IPAddress subnet(255,255,255,0); WiFiServer server(80);//puesto 80 void setup() { Serial.begin(115200); IrSender.begin(); // Inicializamos el emisor infrarrojo Serial.print(F("Listo para enviar señales IR en el pin")); Serial.println(IR_SEND_PIN);//Muestra el número del pin configurado en PinDefinitionsAndMore.h // Comenzamos conectándonos a una red WiFi Serial.println(); Serial.println(); Serial.print("Conectando a "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.config(ip, gateway, subnet);//Configuración ip estática WiFi.begin(ssid, password);//Inicilizamos con los datos de nuestra red wifi while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("Conectado a red Wifi."); Serial.println("Dirección IP "); Serial.println(WiFi.localIP()); server.begin();//Inicializamos el servidor web en el puerto 80 } void loop() { WiFiClient client = server.available(); // Escuchando a los clientes entrantes if (client) { // Si hay un cliente, Serial.println("Nuevo cliente"); // Imprime un mensaje en el puerto serie String currentLine = ""; // String para contener datos entrantes del cliente while (client.connected()) { // Bucle mientras el cliente está conectado if (client.available()) { // Si hay bytes para leer del cliente, char c = client.read(); // Lee un caracter Serial.write(c); // Lo imprimimos en el monitor serial if (c == '\n') { // Si el byte es un carácter de nueva línea if (currentLine.length() == 0) { client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); // Contenido HTML client.print("Click <a href=\"/POWER\">POWER</a> Tecla POWER.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/VOL+\">VOLT+</a> Tecla subir volumen.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/VOLT-\">VOL-</a> Tecla bajar el volumen.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/MENU\">MENU</a> Tecla MENU.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/EXIT\">EXIT</a> Tecla Salir.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/INFO\">INFO</a> Tecla INFO.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/TOOLS\">TOOLS</a> Tecla TOOLS.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/SOURCE\">SOURCE</a> Tecla elegir fuente de video.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/ARRIBA\">ARRIBA</a> Tecla ARRIBA.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/DERECHA\">DERECHA</a> Tecla DERECHA.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/ABAJO\">ABAJO</a> Tecla ABAJO.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/IZQUIERDA\">IZQUIERDA</a> Tecla IZQUIERDA.<br>"); //------------------------------------------------------------------------------------------- client.print("Click <a href=\"/OK\">OK</a> Tecla OK confirmar.<br>"); //------------------------------------------------------------------------------------------- client.println(); // Salir del ciclo while: break; } else { // si tienes una nueva línea, borra currentLine: currentLine = ""; } } else if (c != '\r') { currentLine += c; } //---------------------------------- if (currentLine.endsWith("GET /POWER")) { IrSender.sendSAMSUNG(0xE0E040BF, 32); //apagar encender } //----------------------- if (currentLine.endsWith("GET /VOL+")) { IrSender.sendSAMSUNG(0xE0E0E01F, 32); //Volumen + } //----------------------- if (currentLine.endsWith("GET /VOL-")) { IrSender.sendSAMSUNG(0xE0E0D02F, 32); //volumen - } //----------------------- if (currentLine.endsWith("GET /MENU")) { IrSender.sendSAMSUNG(0xE0E058A7, 32); //menu } //----------------------- if (currentLine.endsWith("GET /EXIT")) { IrSender.sendSAMSUNG(0xE0E0B44B, 32); //Exit/salir } //----------------------- if (currentLine.endsWith("GET /INFO")) { IrSender.sendSAMSUNG(0xE0E0F807, 32);//Info } //----------------------- if (currentLine.endsWith("GET /TOOLS")) { IrSender.sendSAMSUNG(0xE0E0D22D, 32);// Tools } //----------------------- if (currentLine.endsWith("GET /SOURCE")) { IrSender.sendSAMSUNG(0xE0E0807F, 32);//Source } //----------------------- if (currentLine.endsWith("GET /ARRIBA")) { IrSender.sendSAMSUNG(0xE0E006F9, 32);//ARRIBA } //----------------------- if (currentLine.endsWith("GET /DERECHA")) { IrSender.sendSAMSUNG(0xE0E046B9, 32);//DERECHA } //----------------------- if (currentLine.endsWith("GET /ABAJO")) { IrSender.sendSAMSUNG(0xE0E08679, 32);//ABAJO } //----------------------- if (currentLine.endsWith("GET /IZQUIERDA")) { IrSender.sendSAMSUNG(0xE0E0A659, 32);//IZQUIERDA } //----------------------- if (currentLine.endsWith("GET /OK")) { IrSender.sendSAMSUNG(0xE0E016E9, 32);//Source } } } // Cierra la conexión client.stop(); Serial.println("Cliente desconectado"); } } |
PROYECTO RECOMENDADO