title: ESP32 MicroPython SSL WebSocket
body: <p>I successfully managed to connect my ESP32 to a WebSocket server. Now I am trying to make it work with SSL. I tried this simple code to connect to www.google.com. I used this to generate the cert and key.</p>
  
  <pre><code>openssl req -newkey rsa:2048 -nodes -keyout client.key -x509 -days 365 -out client.crt
  
  .
  </code></pre>
  
  <p>Then copy over the key and cert files with adafruit-ampy. Don't forget to change your serial port.</p>
  
  <pre><code>ampy -p /dev/tty.SLAB_USBtoUART put client.crt
  ampy -p /dev/tty.SLAB_USBtoUART put client.key
  </code></pre>
  
  <p>This is the code on the ESP32</p>
  
  <pre><code>import ussl
  import usocket
  import networking
  
  KEY_PATH = "client.key"
  CERT_PATH = "client.crt"
  HOST, PATH, PORT = "www.google.com", "/" 443
  
  with open(KEY_PATH, 'rb') as f:
      key1 = f.read()
  
  with open(CERT_PATH, 'rb') as f:
      cert1 = f.read()
  
  s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
  addr = usocket.getaddrinfo(HOST, PORT)[0][-1]
  s.connect(addr)
  sock = ussl.wrap_socket(s, key = key1, cert = cert1)
  sock.write(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (PATH, HOST), 'utf8'))
  print(sock.read(100))
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>mbedtls_ssl_handshake error: -7280
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  OSError: [Errno 5] EIO
  </code></pre>
  
  <p>Has anyone successfully used ssl socket_wrap on ESP32?</p>
  
  <p>EDIT (23.12.2018):</p>
  
  <p>I managed to finally get something working, fetching HTML from google over HTTPS, yee. Check code above. Hope this helps. I assume the MicropPython port for ESP32 has been getting better and this is the reason this works now.</p>
  
  <p>Next step is to get the SSL WebSocket working ...</p>
  
  <p>EDIT (09.06.2019):</p>
  
  <p>It's working now. This library works great for what I intended to do <a href="https://github.com/danni/uwebsockets" rel="nofollow noreferrer">https://github.com/danni/uwebsockets</a></p>