WebSocket AT Examples

[English]

This document shows how to use WebSocket AT Commands on ESP32 with detailed examples.

Important

The default AT firmware does not support the WebSocket functionality. If you need ESP32 to support WebSocket functionality, please refer to introduction.

WebSocket Connection over TCP

  1. Set the Wi-Fi mode to station.

    Command:

    AT+CWMODE=1
    

    Response:

    OK
    
  2. Connect to the router.

    Command:

    AT+CWJAP="espressif","1234567890"
    

    Response:

    WIFI CONNECTED
    WIFI GOT IP
    
    OK
    

    Note:

    • The SSID and password you entered may be different from those in the above command. Please replace the SSID and password with those of your router settings.

  3. Connect the PC to the same router which ESP32 is connected to.

  4. Create a WebSocket server over TCP on PC using python. The example code is as follows:

    #!/usr/bin/env python
    
    import asyncio
    from websockets.server import serve
    
    host = "192.168.200.249"
    port = 8765
    
    async def echo(websocket):
        async for message in websocket:
            await websocket.send(message)
    
    async def main():
        async with serve(echo, host, port):
            print(f"Server started at ws://{host}:{port}")
            await asyncio.get_running_loop().create_future()  # run forever
    
    asyncio.run(main())
    

    Please modify the code above by replacing the host with the IP address of your PC, and save it as ws-server.py file. Then run the program.

    python ws-server.py
    

    Note:

    • If you do not have the websockets library installed on your PC, please install it using pip install websockets.

    • The WebSocket server port is set to 8765 by default, but you can also set it to a different port.

  5. Connect ESP32 device as a client to the WebSocket server.

    Command:

    AT+WSOPEN=0,"ws://192.168.200.249:8765"
    

    Response:

    +WS_CONNECTED:0
    
    OK
    
  6. Send 4 bytes of data.

    Command:

    AT+WSSEND=0,4
    

    Response:

    OK
    
    >
    

    Input 4 bytes (e.g., test), and then AT will respond with the following message.

    SEND OK
    

    Note:

    • If the input exceeds the length (n) specified by AT+CIPSEND, the system will respond with busy p..., and send the first n bytes. Once those bytes are sent, the system will respond with SEND OK.

  7. Receive 4 bytes of data.

    Since the above WebSocket server is an echo server, after the data is sent, the server will also return the data as it is, so AT will output:

    +WS_DATA:0,4,test
    

WebSocket Connection over TLS (Mutual Authentication)

  1. Set the Wi-Fi mode to station.

    Command:

    AT+CWMODE=1
    

    Response:

    OK
    
  2. Connect to the router.

    Command:

    AT+CWJAP="espressif","1234567890"
    

    Response:

    WIFI CONNECTED
    WIFI GOT IP
    
    OK
    

    Note:

    • The SSID and password you entered may be different from those in the above command. Please replace the SSID and password with those of your router settings.

  3. Set the SNTP server.

    Command:

    AT+CIPSNTPCFG=1,8,"cn.ntp.org.cn","ntp.sjtu.edu.cn"
    

    Response:

    OK
    

    Note:

    • You can set the SNTP server according to your country’s time zone.

  4. Query the SNTP time.

    Command:

    AT+CIPSNTPTIME?
    

    Response:

    +CIPSNTPTIME:Mon Oct 18 20:12:27 2021
    OK
    

    Note:

    • You can verify if the SNTP server is functioning correctly by checking whether the SNTP-synchronized time matches the actual current time.

  5. Connect the PC to the same router which ESP32 is connected to.

  6. Get the CA, certificate, and private key of the WebSocket server.

    You can use the openssl tool to generate the CA, certificate, and private key. If you encounter any difficulties, consider using the following configuration for testing:

    server_ca.crt

    -----BEGIN CERTIFICATE-----
    MIIDNjCCAh6gAwIBAgIUDe6T+Pu0BqmmTjw3s4snVRNFiCMwDQYJKoZIhvcNAQEL
    BQAwNjELMAkGA1UEBhMCQ04xFTATBgNVBAoMDEVTUFJFU1NJRiBBVDEQMA4GA1UE
    AwwHUm9vdCBDQTAeFw0yNDA5MTkwOTMzNDBaFw0zNDA5MTcwOTMzNDBaMDYxCzAJ
    BgNVBAYTAkNOMRUwEwYDVQQKDAxFU1BSRVNTSUYgQVQxEDAOBgNVBAMMB1Jvb3Qg
    Q0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCylQstJGNFHLf7F+gG
    oMSZSrWV4+/5Qxw1Cw3y5N1OVkMSxppYHVxj6MbOwWoCqQcx/WMtqnhg9ATDiZOE
    bXFVB0aZd6EEBz24k7UvQ1ilfIw5tzmjl8SSbe7CiFq302WVokBVFhSeY2jrG+sI
    6PWg0WsvxzierDL9hef708IJERlX0ScBIsLZnVU4UBPtrbG2bgl3L6T5iQ95tSEX
    LsmfZ4l+/Q1xSC7VGH1K1WGBnUzGpv9vLc9uFGZVcAEKx1V9Y7DsyJvTosfOOMmb
    yUIcBk5nVCHRfhmtrfRAWWD9YaNc0gMPVpO6cHzGd/Fgw6vO6QshMYUOE6wxpVjb
    8JYlAgMBAAGjPDA6MAwGA1UdEwQFMAMBAf8wCwYDVR0PBAQDAgGmMB0GA1UdDgQW
    BBRdPJ7Nq+WXSiLN4ZLWJlQQfjrm7zANBgkqhkiG9w0BAQsFAAOCAQEANYpErE2L
    IpISbzJTvG6A67YmYMyadWSNGQ2VjdLK2s3zkggF96bIZziygOa9mgAKD/yTw10t
    V0xF1GUDz43DtJZ1wxo8FPrxH41cP63vjtp28+ZNkylv9Hrx8De2JjiYwRpZmlQY
    EHwDEFpK26LdwPPalwdMZSyMzCtRNJ+o8Bk5kSc3V9QJmh9lLe4PdfCJcfzwPskC
    dNgUMECRa4k3VFOYVUumyFofG8/kINcRogfPzZuUE2Oj1wCHqSpOaP2CJN9QTk0q
    Fn0Itrjikv6z1aTp+SJPOzRPbympL9KhhhhcYJQdPaXHWWYfdhEU/abnnJQpd+1Y
    HPnBCb4tK/pS9w==
    -----END CERTIFICATE-----
    

    server.crt

    -----BEGIN CERTIFICATE-----
    MIIDVTCCAj2gAwIBAgIUPLhviJh1UJDQ5Md6tHibK11jP24wDQYJKoZIhvcNAQEL
    BQAwNjELMAkGA1UEBhMCQ04xFTATBgNVBAoMDEVTUFJFU1NJRiBBVDEQMA4GA1UE
    AwwHUm9vdCBDQTAeFw0yNDA5MTkwOTMzNDBaFw0zNDA5MTcwOTMzNDBaMDQxCzAJ
    BgNVBAYTAkNOMRUwEwYDVQQKDAxFU1BSRVNTSUYgQVQxDjAMBgNVBAMMBUwxIENB
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtKALI+zRbUaMjukAi2ai
    dzPdNdy+TUv2K+GyB15yomIr0e1c1pfztk68zdHKcFFt0RQfz1GYtxrZzqvO7o4t
    K9ijFAw+k498SCTmTqHwlUKc7B9mK6UZfZSkXUnufXKE5+N96u+49e3wbk7yoNfQ
    kzdtwwXUtM6ee5w7HvjwdKQcJXYWv/c/zVLWmAUG3AEaUknS8r4LdG/X+L/bxN+9
    zycLL5tGgZg22KENW4QWsSUY6ntuKQDlohiNxi+wXyM3mVNOc94umICj7a1OhPst
    UmuLYD/gUCnM4JRvQYAVmPQCi88KoLj7aIwJedQ7TJwhJGDS6WDVEMeRfJVXswUk
    4QIDAQABo10wWzAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBpjAdBgNVHQ4EFgQU
    e1wZbnvJnZr8js60EyIWiP6hRFAwHwYDVR0jBBgwFoAUNrQtNxD0tapUczc8Mwln
    hPZE84YwDQYJKoZIhvcNAQELBQADggEBAICnosdYBc6enaKB77AWXFzMWyDDLvd5
    JSJa1IRJ+Rhs1gBxjIH66/+6QyZcJu5C/RZ+RZ04Mky3nMndc3kSCFqauBCK9xoG
    /fZ5wrAfH54o4P+3ZliGoK8EltKu1HuQYJTW8JKbLWCRJ8PnGkrDZBgSXgn8tyL4
    U3hu1MdmkH//fiV0z6itfyJcZDu387l8bGBhrAD3Z7gWcdIJbXxmu6m7FJadUe/N
    0vsLtgkOpOyUa2rThOnJpSyXB5QnKifRGYjuPrnOIWtmADYQRUeD2zdgqRUUuYJR
    ee4Vz2BFXhpZdGeD3bVAop+/YEbTa0iDxXSLWkPLQfCyIkdTPXmKQPQ=
    -----END CERTIFICATE-----
    

    server.key

    -----BEGIN PRIVATE KEY-----
    MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC0oAsj7NFtRoyO
    6QCLZqJ3M9013L5NS/Yr4bIHXnKiYivR7VzWl/O2TrzN0cpwUW3RFB/PUZi3GtnO
    q87uji0r2KMUDD6Tj3xIJOZOofCVQpzsH2YrpRl9lKRdSe59coTn433q77j17fBu
    TvKg19CTN23DBdS0zp57nDse+PB0pBwldha/9z/NUtaYBQbcARpSSdLyvgt0b9f4
    v9vE373PJwsvm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mwBgY3cX9NRt6qZBSifIqnlbNiU17p
    rl8a6qLWeTqVyp5vPMroHQyPVp+2xS0C1VlJcpSOu6cKLxLZDQQZlmg1bNghmFeV
    JpPQbBxdujBYT9peON5RQ2IpBNI/9SHPZwx5I2cCgYBdCmby0loBi5lCbCXKV5c3
    mzlPpM6zR3aLYsFyknWRtLv3UPMGzOLsRDkwfQEMZWt/VFjXehajyqd8X7r3V0lX
    cz5LwmH0MSKDYgdzrJjvE3lPxDdKtmyPr5nnF/8/SF6Lawj7v6eGnpiZNqHC/wkK
    9EmCxWXJc/9GVWSZtEOz/Q==
    -----END PRIVATE KEY-----
    
  7. Create a WebSocket server over TLS on PC using python. The example code is as follows:

    #!/usr/bin/env python
    
    import asyncio
    import ssl
    import websockets
    
    host = '192.168.200.249'
    port = 8766
    server_ca = '/your_path/server_ca.crt'
    server_cert = '/your_path/server.crt'
    server_key = '/your_path/server.key'
    
    async def echo(websocket, path):
        async for message in websocket:
            await websocket.send(message)
    
    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    ssl_context.load_cert_chain(certfile=server_cert, keyfile=server_key)
    ssl_context.verify_mode = ssl.CERT_REQUIRED
    ssl_context.load_verify_locations(server_ca)
    
    start_server = websockets.serve(echo, host, port, ssl=ssl_context)
    
    asyncio.get_event_loop().run_until_complete(start_server)
    print(f"Server started at wss://{host}:{port}")
    
    asyncio.get_event_loop().run_forever()
    

    Please modify the code above by replacing the host with the IP address of your PC, and the server_ca, server_cert, and server_key with the paths to the CA, certificate, and private key of the server. Save the modified code as wss-server.py and run the program.

    python wss-server.py
    

    Note:

    • If you do not have the websockets library installed on your PC, please install it using pip install websockets.

    • The WebSocket server port is set to 8766, but you can also set it to a different port.

  8. Configure the WebSocket connection for mutual authentication.

    Command:

    AT+WSCFG=0,30,60,4096,3,0,0
    

    Response:

    OK
    

    Note:

  9. Connect ESP32 device as a client to the WebSocket server.

    Command:

    AT+WSOPEN=0,"wss://192.168.200.249:8766"
    

    Response:

    +WS_CONNECTED:0
    
    OK
    
  10. Send 4 bytes of data.

    Command:

    AT+WSSEND=0,4
    

    Response:

    OK
    
    >
    

    Input 4 bytes, for example, test, then AT will respond the following message.

    SEND OK
    

    Note:

    • If the number of bytes inputted are more than the length (n) set by AT+CIPSEND, the system will reply busy p..., and send the first n bytes. And after sending the first n bytes, the system will reply SEND OK.

  11. Receive 4 bytes of data.

    Since the above WebSocket server is an echo server, after the data is sent, the server will also return the data as it is, so AT will output:

    +WS_DATA:0,4,test