{"id":1915,"date":"2018-12-18T22:53:23","date_gmt":"2018-12-19T03:53:23","guid":{"rendered":"http:\/\/blog.espol.edu.ec\/edelros\/?p=1915"},"modified":"2026-07-23T11:37:56","modified_gmt":"2026-07-23T16:37:56","slug":"iot-mqtt-tls","status":"publish","type":"post","link":"https:\/\/blog.espol.edu.ec\/girni\/iot-mqtt-tls\/","title":{"rendered":"4. IoT MQTT - TLS certificados"},"content":{"rendered":"\n<p>Para a\u00f1adir un nivel adicional de seguridad a los mensajes MQTT en la red, se incorpora SSL\/TLS para un dispositivo ESP8266.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/blog.espol.edu.ec\/girni\/files\/2019\/08\/Mqtt_TLS_Esp8266_01.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1231\" height=\"664\" src=\"http:\/\/blog.espol.edu.ec\/girni\/files\/2019\/08\/Mqtt_TLS_Esp8266_01.png\" alt=\"\" class=\"wp-image-3318\" srcset=\"https:\/\/blog.espol.edu.ec\/girni\/files\/2019\/08\/Mqtt_TLS_Esp8266_01.png 1231w, https:\/\/blog.espol.edu.ec\/girni\/files\/2019\/08\/Mqtt_TLS_Esp8266_01-300x162.png 300w, https:\/\/blog.espol.edu.ec\/girni\/files\/2019\/08\/Mqtt_TLS_Esp8266_01-768x414.png 768w, https:\/\/blog.espol.edu.ec\/girni\/files\/2019\/08\/Mqtt_TLS_Esp8266_01-1024x552.png 1024w\" sizes=\"auto, (max-width: 1231px) 100vw, 1231px\" \/><\/a><\/figure>\n\n\n\n<p>Para simplificar el ejemplo, la explicaci\u00f3n supone que tiene operando el dispositivo con<\/p>\n\n\n\n<p class=\"entry-title\"><a href=\"http:\/\/blog.espol.edu.ec\/girni\/mqtt-mensajes-con-esp8266-parpadeo-led-archivo-ino\/\">Mensajes con ESP8266-Parpadeo LED<\/a><\/p>\n\n\n\n<p>y se ha habilitado un broker SSL\/TLS con certificado \"local\" siguiendo el ejemplo de<\/p>\n\n\n\n<p><a href=\"http:\/\/blog.espol.edu.ec\/girni\/mqtt-mosquitto-tls\/\">MQTT \u2013 Mosquitto TLS<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Librer\u00edas Arduino<\/h2>\n\n\n\n<p>Para manejar TLS en la conexi\u00f3n, se usa la libreria siguiente:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"color: #5e6d03\">#include<\/span> &lt;<b><span style=\"color: #d35400\">WiFiClientSecure<\/span><\/b>.h&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Servidor MQTT puerto<\/h2>\n\n\n\n<p>El primer valor a actualizar para mensajes TLS es el puerto, revise este valor antes de continuar.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ MQTT: Servidor\n<span style=\"color: #00979c\">char<\/span>* MQTT_IP = <span style=\"color: #005c5f\">\"192.168.10.40\"<\/span>;\n<span style=\"color: #00979c\">uint16_t<\/span> MQTT_puerto = 8883;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Certificados TLS<\/h2>\n\n\n\n<p>Los certificados a usar corresponden al certificado de la autoridad y del servidor mqtt.<\/p>\n\n\n\n<p>Para el certificado de la autoridad <code>mqtt-ca.crt<\/code> se usa el contenido del archivo abierto como texto.<\/p>\n\n\n\n<p>El certificado del servidor mqtt-srv se usa en formato reducido de fingerprint, obtenido con la instruccion descrita anteriormente en la secci\u00f3n mosquitto TLS.<\/p>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>\/\/ Certificados TLS: Certificate Authority\n<span style=\"color: #00979c\">const char<\/span> caCert&#091;] <span style=\"color: #00979c\">PROGMEM<\/span> = R\"EOF(-----BEGIN CERTIFICATE-----\nMIIDRzCCAi+gAwIBAgIUMtlZSXzSZfHpBI0vrnxdLrQuNgwwDQYJKoZIhvcNAQEL\nBQAwMzELMAkGA1UEBhMCRUMxDDAKBgNVBAcMA0d5ZTEWMBQGA1UEAwwNMTkyLjE2\n.... \/\/ ....\nOpA6IPNIHmDSSPxgejZq4booviVKro2\/M++iOUuGj1jkjR6XSQ6x1JgK0DVyiBtD\n4od+M4mYk7gz\/CHgl1JEsTVPyUZK4OO\/oxC7\n-----END CERTIFICATE-----\n)EOF\";\n\n\/\/ MQTT: cert SHA1 fingerprint\n<span style=\"color: #00979c\">const uint8_t<\/span> mqttCertFingerprint&#091;] = {0x19,0x50,0x25,0x2C,0xD8,0x6A,0x54,0xE6,0xBC,0x8B,0xA4,0x5F,0x76,0x3F,0xD0,0x7D,0xE0,0x5F,0x7C,0x6C};\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conexiones WiFi Seguras<\/h2>\n\n\n\n<p>Para usar los componentes de WifiClient y PubsubClient se requiere a\u00f1adir los detallas de los certificados, por lo que se sustituyen las l\u00ednea como se muestra:<\/p>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code><span style=\"color: #434f54\">\/\/ Clientes WiFi y MQTT<\/span>\n<span style=\"color: #434f54\">\/\/WiFiClient wificlient;<\/span>\n<span style=\"color: #434f54\">\/\/ PubSubClient mqttclient(wificlient);<\/span>\n\n<span style=\"color: #434f54\">\/\/ X.509 parsed CA Cert <\/span>\n<b><span style=\"color: #d35400\">X509List<\/span><\/b> caCertX509(caCert);\n<span style=\"color: #434f54\">\/\/ Secure client connection class<\/span>\n<b><span style=\"color: #d35400\">WiFiClientSecure<\/span><\/b> wificlient;\n<span style=\"color: #434f54\">\/\/ MQTT: Client connection<\/span>\n<b><span style=\"color: #d35400\">PubSubClient<\/span><\/b> mqttclient(wificlient);\n<\/code><\/pre>\n\n\n\n<p>MQTT - Inicio<\/p>\n\n\n\n<p>En la secci\u00f3n de inicio de MQTT se a\u00f1aden las partes para que tome en cuenta los certificados al momento de realizar la conexi\u00f3n por Wifi<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/ Configura cliente TLS\n  \/\/ a\u00f1ade CA cert en los sitios de confianza\n  wificlient.<span style=\"color: #d35400\">setTrustAnchors<\/span>(&amp;caCertX509);\n  \/\/ Habilita self-signed cert\n  wificlient.<span style=\"color: #d35400\">allowSelfSignedCerts<\/span>(); \n  \/\/ a\u00f1ade fingerprint para validar conexi\u00f3n\n  wificlient.<span style=\"color: #d35400\">setFingerprint<\/span>(mqttCertFingerprint); \n<\/code><\/pre>\n\n\n\n<p>El resto de las instrucciones se mantienen iguales al proceso anterior.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Ejemplo: Parpadea LED<\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code><span style=\"color: #95a5a6\">\/* ESP8266 Sensor Parpadea. edelros@espol.edu.ec<\/span>\n<span style=\"color: #95a5a6\"> *  con SSL\/TLS, Para usar, actualice las secciones de:<\/span>\n<span style=\"color: #95a5a6\"> *  - WIFI:Router, MQTT:Servidor, MQTT:Dispositivo<\/span>\n<span style=\"color: #95a5a6\"> *  ESP-01 al usar GPIO1 y GPIO3,(Tx,Rx), NO USE Serial.print()<\/span>\n<span style=\"color: #95a5a6\">*\/<\/span>\n<span style=\"color: #5e6d03\">#include<\/span> <span style=\"color: #434f54\">&lt;<\/span><span style=\"color: #d35400\">ESP8266WiFi<\/span>.<span style=\"color: #000000\">h<\/span><span style=\"color: #434f54\">&gt;<\/span>\n<span style=\"color: #5e6d03\">#include<\/span> <span style=\"color: #434f54\">&lt;<\/span><b><span style=\"color: #d35400\">WiFiClientSecure<\/span><\/b>.<span style=\"color: #000000\">h<\/span><span style=\"color: #434f54\">&gt;<\/span>\n<span style=\"color: #5e6d03\">#include<\/span> <span style=\"color: #434f54\">&lt;<\/span><b><span style=\"color: #d35400\">PubSubClient<\/span><\/b>.<span style=\"color: #000000\">h<\/span><span style=\"color: #434f54\">&gt;<\/span>\n\n<span style=\"color: #434f54\">\/\/ WIFI: conexi\u00f3n a Router<\/span>\n<span style=\"color: #00979c\">char<\/span>* ssid = <span style=\"color: #005c5f\">\"miRouter\"<\/span>;\n<span style=\"color: #00979c\">char<\/span>* password = <span style=\"color: #005c5f\">\"miRouterclave\"<\/span>;\n\n<span style=\"color: #434f54\">\/\/ MQTT: Servidor<\/span>\n<span style=\"color: #00979c\">char<\/span>* <span style=\"color: #000000\">MQTT_IP<\/span> = <span style=\"color: #005c5f\">\"192.168.10.40\"<\/span>;\n<span style=\"color: #00979c\">uint16_t<\/span> <span style=\"color: #000000\">MQTT_puerto<\/span> = <span style=\"color: #000000\">8883<\/span>;\n<span style=\"color: #00979c\">char<\/span>* <span style=\"color: #000000\">MQTT_usuario<\/span> = <span style=\"color: #005c5f\">\"usuarioprueba\"<\/span>;\n<span style=\"color: #00979c\">char<\/span>* <span style=\"color: #000000\">MQTT_contrasena<\/span> = <span style=\"color: #005c5f\">\"usuarioclave\"<\/span>;\n\n<span style=\"color: #434f54\">\/\/ MQTT: Dispositivo<\/span>\n<span style=\"color: #00979c\">char<\/span>* <span style=\"color: #000000\">MQTT_ID<\/span> = <span style=\"color: #005c5f\">\"sensor01\"<\/span>;\n<span style=\"color: #00979c\">char<\/span>* <span style=\"color: #000000\">MQTT_TOPIC<\/span> = <span style=\"color: #005c5f\">\"oficina\/mensaje\"<\/span>;\n<span style=\"color: #00979c\">char<\/span> <span style=\"color: #000000\">MQTT_SensorEstado<\/span>&#091;<span style=\"color: #000000\">10<\/span>] = <span style=\"color: #005c5f\">\"OFF\"<\/span>;\n\n<span style=\"color: #434f54\">\/\/ Certificados TLS: Certificate Authority<\/span>\n<span style=\"color: #00979c\">const<\/span> <span style=\"color: #00979c\">char<\/span> <span style=\"color: #000000\">caCert<\/span>&#091;] <span style=\"color: #00979c\">PROGMEM<\/span> = <span style=\"color: #000000\">R<\/span><span style=\"color: #000000\">\"EOF(-----BEGIN CERTIFICATE-----<\/span>\n<span style=\"color: #000000\">MIIDRzCCAi<\/span><span style=\"color: #434f54\">+<\/span><span style=\"color: #000000\">gAwIBAgIUMtlZSXzSZfHpBI0vrnxdLrQuNgwwDQYJKoZIhvcNAQEL<\/span>\n.......\/\/ ........\n<span style=\"color: #000000\">OpA6IPNIHmDSSPxgejZq4booviVKro2<\/span><span style=\"color: #434f54\">\/<\/span><span style=\"color: #000000\">M<\/span><span style=\"color: #434f54\">++<\/span><span style=\"color: #000000\">iOUuGj1jkjR6XSQ6x1JgK0DVyiBtD<\/span>\n<span style=\"color: #000000\">4od<\/span><span style=\"color: #434f54\">+<\/span><span style=\"color: #000000\">M4mYk7gz<\/span><span style=\"color: #434f54\">\/<\/span><span style=\"color: #000000\">CHgl1JEsTVPyUZK4OO<\/span><span style=\"color: #434f54\">\/<\/span><span style=\"color: #000000\">oxC7<\/span>\n<span style=\"color: #434f54\">--<\/span><span style=\"color: #434f54\">--<\/span><span style=\"color: #434f54\">-<\/span><span style=\"color: #000000\">END<\/span> <span style=\"color: #000000\">CERTIFICATE<\/span><span style=\"color: #434f54\">--<\/span><span style=\"color: #434f54\">--<\/span><span style=\"color: #434f54\">-<\/span>\n)<span style=\"color: #000000\">EOF<\/span><span style=\"color: #000000\">\";<\/span>\n\n<span style=\"color: #434f54\">\/\/ MQTT: cert SHA1 fingerprint<\/span>\n<span style=\"color: #00979c\">const<\/span> <span style=\"color: #00979c\">uint8_t<\/span> <span style=\"color: #000000\">mqttCertFingerprint<\/span>&#091;] = {0x19,0x50,0x25,0x2C,0xD8,0x6A,0x54,0xE6,0xBC,0x8B,0xA4,0x5F,0x76,0x3F,0xD0,0x7D,0xE0,0x5F,0x7C,0x6C};\n\n<span style=\"color: #434f54\">\/\/ Sensor <\/span>\n<span style=\"color: #00979c\">const<\/span> <span style=\"color: #00979c\">uint8_t<\/span> <span style=\"color: #000000\">sensor_pin<\/span> = <span style=\"color: #000000\">1<\/span>;\n<span style=\"color: #00979c\">volatile<\/span> <span style=\"color: #00979c\">boolean<\/span> <span style=\"color: #000000\">sensor_estado<\/span>  = <span style=\"color: #00979c\">false<\/span>;\n\n<span style=\"color: #434f54\">\/\/ LED monitor interno: ESP01-pin=1, ESP07-pin=2<\/span>\n<span style=\"color: #00979c\">const<\/span> <span style=\"color: #00979c\">uint8_t<\/span> <span style=\"color: #000000\">LED_pin<\/span> = <span style=\"color: #000000\">2<\/span>;\n\n<span style=\"color: #434f54\">\/\/ Mensajes por Puerto Serial<\/span>\n<span style=\"color: #00979c\">volatile<\/span> <span style=\"color: #00979c\">boolean<\/span> <span style=\"color: #000000\">serial_msg<\/span> = <span style=\"color: #00979c\">true<\/span>;\n\n<span style=\"color: #434f54\">\/\/ Clientes WiFi y MQTT<\/span>\n<span style=\"color: #434f54\">\/\/WiFiClient wificlient;<\/span>\n<span style=\"color: #434f54\">\/\/ PubSubClient mqttclient(wificlient);<\/span>\n\n<span style=\"color: #434f54\">\/\/ X.509 parsed CA Cert <\/span>\n<b><span style=\"color: #d35400\">X509List<\/span><\/b> <span style=\"color: #000000\">caCertX509<\/span>(<span style=\"color: #000000\">caCert<\/span>);\n<span style=\"color: #434f54\">\/\/ Secure client connection class<\/span>\n<b><span style=\"color: #d35400\">WiFiClientSecure<\/span><\/b> <span style=\"color: #000000\">wificlient<\/span>;\n<span style=\"color: #434f54\">\/\/ MQTT: Client connection<\/span>\n<b><span style=\"color: #d35400\">PubSubClient<\/span><\/b> mqttclient(<span style=\"color: #000000\">wificlient<\/span>);\n\n<span style=\"color: #00979c\">void<\/span> <span style=\"color: #5e6d03\">setup<\/span>() {\n\n  <span style=\"color: #5e6d03\">if<\/span> (<span style=\"color: #000000\">serial_msg<\/span>){\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">begin<\/span>(<span style=\"color: #000000\">74880<\/span>);<span style=\"color: #434f54\">\/\/74880, 115200<\/span>\n    <span style=\"color: #5e6d03\">while<\/span> (<span style=\"color: #434f54\">!<\/span><b><span style=\"color: #d35400\">Serial<\/span><\/b>){<span style=\"color: #d35400\">delay<\/span>(<span style=\"color: #000000\">50<\/span>);}\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">println<\/span>(<span style=\"color: #005c5f\">\"inicia setup\"<\/span>);\n  }\n  \n  <span style=\"color: #434f54\">\/\/ LED monitor, Enciende en LOW<\/span>\n  <span style=\"color: #d35400\">pinMode<\/span>(<span style=\"color: #000000\">LED_pin<\/span><span style=\"color: #434f54\">,<\/span> <span style=\"color: #00979c\">OUTPUT<\/span>);\n\n  <span style=\"color: #434f54\">\/\/ conexi\u00f3n WIFI y MQTT<\/span>\n  <span style=\"color: #000000\">inicia_wifi<\/span>();\n  <span style=\"color: #5e6d03\">if<\/span> (<b><span style=\"color: #d35400\">WiFi<\/span><\/b>.<span style=\"color: #d35400\">status<\/span>() == WL_CONNECTED){\n    <span style=\"color: #000000\">inicia_mqtt<\/span>();\n  }\n}\n\n<span style=\"color: #00979c\">void<\/span> <span style=\"color: #5e6d03\">loop<\/span>() {\n  \n  <span style=\"color: #434f54\">\/\/ Parpadea estado de sensor<\/span>\n  <span style=\"color: #5e6d03\">if<\/span> (<span style=\"color: #000000\">sensor_estado<\/span>==<span style=\"color: #00979c\">true<\/span>){\n    <span style=\"color: #000000\">sensor_estado<\/span> = <span style=\"color: #00979c\">false<\/span>;\n  }<span style=\"color: #5e6d03\">else<\/span>{\n    <span style=\"color: #000000\">sensor_estado<\/span> = <span style=\"color: #00979c\">true<\/span>;\n  }\n  \n  <span style=\"color: #434f54\">\/\/ LED Monitor Parpadea, enciende en LOW<\/span>\n  <span style=\"color: #5e6d03\">if<\/span> (<span style=\"color: #000000\">sensor_estado<\/span> ==<span style=\"color: #00979c\">true<\/span>) {\n    <span style=\"color: #d35400\">digitalWrite<\/span>(<span style=\"color: #000000\">LED_pin<\/span><span style=\"color: #434f54\">,<\/span> <span style=\"color: #00979c\">LOW<\/span>);\n    <span style=\"color: #d35400\">delay<\/span>(1000); <span style=\"color: #434f54\">\/\/ un segundo<\/span>\n  }\n  <span style=\"color: #5e6d03\">if<\/span> (<span style=\"color: #000000\">sensor_estado<\/span> ==<span style=\"color: #00979c\">false<\/span>) {\n    <span style=\"color: #d35400\">digitalWrite<\/span>(<span style=\"color: #000000\">LED_pin<\/span><span style=\"color: #434f54\">,<\/span> <span style=\"color: #00979c\">HIGH<\/span>);\n    <span style=\"color: #d35400\">delay<\/span>(1000);\n  }\n\n  <span style=\"color: #5e6d03\">if<\/span> (<b><span style=\"color: #d35400\">WiFi<\/span><\/b>.<span style=\"color: #d35400\">status<\/span>()==WL_CONNECTED) {\n    <span style=\"color: #000000\">publica_estado<\/span>();\n  }\n  \n  <span style=\"color: #434f54\">\/\/Revisa estado de Wifi o reintenta conexi\u00f3n  <\/span>\n  <span style=\"color: #5e6d03\">if<\/span> (<b><span style=\"color: #d35400\">WiFi<\/span><\/b>.<span style=\"color: #d35400\">status<\/span>() <span style=\"color: #434f54\">!=<\/span> WL_CONNECTED){\n    <span style=\"color: #000000\">inicia_wifi<\/span>();\n  }<span style=\"color: #5e6d03\">else<\/span>{\n    <span style=\"color: #5e6d03\">if<\/span> (mqttclient.<span style=\"color: #d35400\">connected<\/span>()==<span style=\"color: #00979c\">false<\/span>){\n        <span style=\"color: #000000\">inicia_mqtt<\/span>(); <span style=\"color: #434f54\">\/\/ reintento<\/span>\n    }\n    <span style=\"color: #5e6d03\">if<\/span> (mqttclient.<span style=\"color: #d35400\">connected<\/span>()==<span style=\"color: #00979c\">true<\/span>){\n      mqttclient.<span style=\"color: #5e6d03\">loop<\/span>();\n    }\n  }\n  <span style=\"color: #d35400\">yield<\/span>(); <span style=\"color: #434f54\">\/\/ procesa wifi<\/span>\n} <\/code><\/pre>\n\n\n\n<p>secci\u00f3n de inicio MQTT completa:<\/p>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code><span style=\"color: #00979c\">void<\/span> inicia_mqtt() {\n  <span style=\"color: #00979c\">int<\/span> intentos = 5;\n  <span style=\"color: #00979c\">int<\/span> cuenta = 0;\n\n  \/\/ Configura cliente TLS\n  \/\/ a\u00f1ade CA cert en los sitios de confianza\n  wificlient.<span style=\"color: #d35400\">setTrustAnchors<\/span>(&amp;caCertX509);\n  \/\/ Habilita self-signed cert\n  wificlient.<span style=\"color: #d35400\">allowSelfSignedCerts<\/span>(); \n  \/\/ a\u00f1ade fingerprint para validar conexi\u00f3n\n  wificlient.<span style=\"color: #d35400\">setFingerprint<\/span>(mqttCertFingerprint); \n\n  mqttclient.<span style=\"color: #d35400\">setServer<\/span>(MQTT_IP, MQTT_puerto);\n  mqttclient.<span style=\"color: #d35400\">connect<\/span>(MQTT_ID, MQTT_usuario, MQTT_contrasena);\n  \/\/mqttclient.setCallback(callback);\n\n  <span style=\"color: #5e6d03\">if<\/span> (serial_msg){\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">print<\/span>(<span style=\"color: #005c5f\">\" MQTT Conectando a \"<\/span>);\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">println<\/span>(<span style=\"color: #000000\">MQTT_IP<\/span>);\n  }\n  \n  <span style=\"color: #5e6d03\">while<\/span> (<span style=\"color: #434f54\">!<\/span>mqttclient.<span style=\"color: #d35400\">connected<\/span>()<span style=\"color: #434f54\">&amp;&amp;<\/span>(<span style=\"color: #000000\">cuenta<\/span><span style=\"color: #434f54\">&lt;=<\/span><span style=\"color: #000000\">intentos<\/span>)){\n    <span style=\"color: #000000\">cuenta<\/span> = <span style=\"color: #000000\">cuenta<\/span> <span style=\"color: #434f54\">+<\/span> <span style=\"color: #000000\">1<\/span>;\n    \n    \/\/ LED Monitor parpadeo MQTT, enciende LOW\n    <span style=\"color: #d35400\">digitalWrite<\/span>(<span style=\"color: #000000\">LED_pin<\/span>, <span style=\"color: #00979c\">LOW<\/span>);\n    <span style=\"color: #d35400\">delay<\/span>(<span style=\"color: #000000\">600<\/span>);\n    <span style=\"color: #d35400\">digitalWrite<\/span>(<span style=\"color: #000000\">LED_pin<\/span>, <span style=\"color: #00979c\">HIGH<\/span>);\n    <span style=\"color: #d35400\">delay<\/span>(<span style=\"color: #000000\">400<\/span>);\n    \n    <span style=\"color: #5e6d03\">if<\/span> (serial_msg){\n      <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">print<\/span>(<span style=\"color: #005c5f\">\".\"<\/span>);\n    }\n  }\n  \n  \/\/ Si conectado, inicializa estado\n  <span style=\"color: #5e6d03\">if<\/span> (mqttclient.<span style=\"color: #d35400\">connected<\/span>()){\n      publica_estado();\n  }\n  \n  <span style=\"color: #5e6d03\">if<\/span> (serial_msg){\n    \/\/Fin de \"....\"\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">println<\/span>();\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">print<\/span>(<span style=\"color: #005c5f\">\"; MQTT Conectado: \";<\/span>);\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">println<\/span>(mqttclient.<span style=\"color: #d35400\">connected<\/span>());\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">print<\/span>(<span style=\"color: #005c5f\">\"; MQTT Estado: \";<\/span>);\n    <b><span style=\"color: #d35400\">Serial<\/span><\/b>.<span style=\"color: #d35400\">println<\/span>(mqttclient.state());\n  }\n}<\/code><\/pre>\n\n\n\n<p>secci\u00f3n de publicaci\u00f3n MQTT es igual que en el ejercicio que le precede<\/p>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>\/\/ Publicar el estados del dispositivo\n<span style=\"color: #00979c\">void<\/span> publica_estado() {\n  \n  \/\/ formato para envio (Texto)\n  <span style=\"color: #5e6d03\">if<\/span> (sensor_estado == <span style=\"color: #00979c\">true<\/span> ){\n    snprintf(MQTT_SensorEstado,10, <span style=\"color: #005c5f\">\"ON\"<\/span>);\n  }<span style=\"color: #5e6d03\">else<\/span>{\n    snprintf(MQTT_SensorEstado,10, <span style=\"color: #005c5f\">\"OFF\"<\/span>);\n  }\n\n  \/\/ publicar estados\n  <span style=\"color: #5e6d03\">if<\/span> (mqttclient.<span style=\"color: #d35400\">connected<\/span>()==<span style=\"color: #00979c\">true<\/span>) {\n    mqttclient.<span style=\"color: #d35400\">publish<\/span>(MQTT_TOPIC,MQTT_SensorEstado,<span style=\"color: #00979c\">true<\/span>);\n  }\n\n  <span style=\"color: #5e6d03\">if<\/span> (serial_msg){\n    <span style=\"color: #5e6d03\">if<\/span> (mqttclient.<span style=\"color: #d35400\">connected<\/span>()==<span style=\"color: #00979c\">true<\/span>) {\n      <b><span style=\"color: #d35400\">Serial<\/span><\/b><span style=\"color: #434f54\">.<\/span><span style=\"color: #d35400\">println<\/span>(MQTT_SensorEstado);\n    }\n    <span style=\"color: #5e6d03\">if<\/span> (mqttclient.<span style=\"color: #d35400\">connected<\/span>()==<span style=\"color: #00979c\">false<\/span>) {\n      <b><span style=\"color: #d35400\">Serial<\/span><\/b><span style=\"color: #434f54\">.<\/span><span style=\"color: #d35400\">println<\/span>(<span style=\"color: #005c5f\">\"MQTT desconectado\"<\/span>);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p>secci\u00f3n de inicio de Wifi es igual que en el ejercicio que le precede.<\/p>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code><span style=\"color: #00979c\">void<\/span> inicia_wifi(){\n  <span style=\"color: #00979c\">int<\/span> intentos = 10;\n  <span style=\"color: #00979c\">int<\/span> cuenta = 0;\n\n  <span style=\"color: #5e6d03\">if<\/span> (serial_msg){\n    <b><span style=\"color: #d35400\">Serial.print<\/span><\/b>(<span style=\"color: #005c5f\">\" WIFI Conectando a \"<\/span>);\n    <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>(ssid);\n    }\n  \n  <b><span style=\"color: #d35400\">WiFi.mode<\/span><\/b>(<span style=\"color: #00979c\">WIFI_STA<\/span>);\n  <b><span style=\"color: #d35400\">WiFi.begin<\/span><\/b>(ssid, password);\n  \n  <span style=\"color: #5e6d03\">while<\/span> ((<b><span style=\"color: #d35400\">WiFi.status<\/span><\/b>() != WL_CONNECTED) &amp;&amp; (cuenta &lt; intentos)){\n    <span style=\"color: #5e6d03\">if<\/span> (serial_msg){\n      <b><span style=\"color: #d35400\">Serial.print<\/span><\/b>(<span style=\"color: #005c5f\">\".\"<\/span>);\n      }\n    cuenta = cuenta<span style=\"color: #434f54\">+<\/span>1;\n    \/\/ Parpadeo de Monitor enciende en LOW\n    <span style=\"color: #d35400\">digitalWrite<\/span>(LED_pin, <span style=\"color: #00979c\">LOW<\/span>);\n    <span style=\"color: #d35400\">delay<\/span>(300);\n    <span style=\"color: #d35400\">digitalWrite<\/span>(LED_pin, <span style=\"color: #00979c\">HIGH<\/span>);\n    <span style=\"color: #d35400\">delay<\/span>(200);\n    }\n  \n  <span style=\"color: #5e6d03\">if<\/span> (serial_msg){\n    <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>();  <span style=\"color: #434f54\">\/\/Fin de \"...\"<\/span>\n    <span style=\"color: #5e6d03\">if<\/span> (<b><span style=\"color: #d35400\">WiFi.status<\/span><\/b>() == WL_CONNECTED){\n      <b><span style=\"color: #d35400\">Serial.print<\/span><\/b>(<span style=\"color: #005c5f\">\" Estado: \"<\/span>);\n      <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>(<span style=\"color: #d35400\">WiFi.status<\/span>());\n      <b><span style=\"color: #d35400\">Serial.print<\/span><\/b>(<span style=\"color: #005c5f\">\" MAC: \"<\/span>);\n      <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>(<span style=\"color: #d35400\">WiFi.macAddress<\/span>());\n      <b><span style=\"color: #d35400\">Serial.print<\/span><\/b>(<span style=\"color: #005c5f\">\" IP: \"<\/span>);\n      <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>(<span style=\"color: #d35400\">WiFi.localIP<\/span>());\n      <b><span style=\"color: #d35400\">Serial.print<\/span><\/b>(<span style=\"color: #005c5f\">\" RSSI: \"<\/span>);\n      <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>(<span style=\"color: #d35400\">WiFi.RSSI<\/span>());\n      <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>();\n      }\n    <span style=\"color: #5e6d03\">if<\/span> (<b><span style=\"color: #d35400\">WiFi.status<\/span><\/b>() != WL_CONNECTED){\n      <b><span style=\"color: #d35400\">WiFi.printDiag<\/span><\/b>(<span style=\"color: #d35400\">Serial<\/span>);\n      <b><span style=\"color: #d35400\">Serial.println<\/span><\/b>();\n      }\n    }\n  }\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<p><strong>Referencia<\/strong>: <a href=\"https:\/\/blog.thewalr.us\/2019\/03\/27\/using-esp8266-as-an-iot-endpoint-with-encrypted-mqtt-transport\/\">https:\/\/blog.thewalr.us\/2019\/03\/27\/using-esp8266-as-an-iot-endpoint-with-encrypted-mqtt-transport\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Para a\u00f1adir un nivel adicional de seguridad a los mensajes MQTT en la red, se incorpora SSL\/TLS para un dispositivo ESP8266. Para simplificar el ejemplo, la explicaci\u00f3n supone que tiene operando el dispositivo con Mensajes con ESP8266-Parpadeo LED y se ha habilitado un broker SSL\/TLS con certificado \"local\" siguiendo el ejemplo de MQTT \u2013 Mosquitto [&hellip;]<\/p>\n","protected":false},"author":8043,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1464959],"tags":[],"class_list":["post-1915","post","type-post","status-publish","format-standard","hentry","category-iot-mensajes"],"_links":{"self":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts\/1915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/users\/8043"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/comments?post=1915"}],"version-history":[{"count":14,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts\/1915\/revisions"}],"predecessor-version":[{"id":4386,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/posts\/1915\/revisions\/4386"}],"wp:attachment":[{"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/media?parent=1915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/categories?post=1915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/girni\/wp-json\/wp\/v2\/tags?post=1915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}