Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: WebSockets_Generic Library.

  1. #1
    Member
    Join Date
    Jan 2020
    Location
    Toronto, Canada
    Posts
    60

    WebSockets_Generic Library.

    https://github.com/khoih-prog/WebSockets_Generic

    How To Install Using Arduino Library Manager

    Current Release

    Why do we need this library

    Many Web services require WebSockets library , which is so far written only for ESP8266/ESP32 boards. The ESP boards rely on this Markus Sattler's WebSockets library to connect to Alexa via Sinric or SinricPro skills.

    This library is based on and modified from WebSockets library to provide support to many boards such as Teensy (4.1, 4.0, 3.x, LC), Arduino SAMD21, Adafruit SAMD21/SAMD51, nRF52, STM32, etc. and enable those boards to use WebSockets services, including voice-control Alexa along with Blynk. The WebSockets can be used with ESP's WiFi, WiFiNINA, W5x00 and ENC28J60 Ethernet.

    Please see illustrating examples.

    New in v2.2.1

    1. Bump up to sync with v2.2.1 of original WebSockets library

    New in v2.1.3

    1. Add support to nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B30_ublox, etc..
    2. Add support to SAM51 (Itsy-Bitsy M4, Metro M4, Grand Central M4, Feather M4 Express, etc.).
    3. Add support to SAMD21 (ZERO, MKR, NANO_33_IOT, M0, M0 Pro, AdaFruit CIRCUITPLAYGROUND_EXPRESS, etc.).
    4. Add support to Teensy (4.1, 4.0, 3.x, LC).
    5. Add support to SAM DUE.


    How to use

    In your code, select one of the currently supported network connection from the following list:

    1. NETWORK_ESP8266_ASYNC for ESP8266 Async
    2. NETWORK_W5100 for W5x00 Ethernet
    3. NETWORK_ENC28J60 for ENC28J60 Ethernet
    4. NETWORK_ESP32 for ESP32 WiFi
    5. NETWORK_ESP32_ETH for ESP32 Ethernet
    6. NETWORK_WIFININA for WiFiNINA

    then add

    Code:
    #define WEBSOCKETS_NETWORK_TYPE
    before

    Code:
    #include <WebSocketsClient_Generic.h>

    Example Generic_WebSocketServer_W5500

    Code:
    #define _WEBSOCKETS_LOGLEVEL_     3
    #define WEBSOCKETS_NETWORK_TYPE   NETWORK_W5100
    
    #include <WebSocketsServer_Generic.h>
    
    WebSocketsServer webSocket = WebSocketsServer(81);
    
    uint8_t mac[6] =  { 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A };
    
    // Select the IP address according to your local network
    IPAddress ip(192, 168, 2, 222);
    
    // Only for W5100
    #define SDCARD_CS       4
    
    void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
    {
      switch (type)
      {
        case WStype_DISCONNECTED:
          //Serial.println( "[" + String(num) + "] Disconnected!");
          break;
        case WStype_CONNECTED:
          {
            Serial.println( "[" + String(num) + "] Connected!");
            //IPAddress ip = webSocket.remoteIP(num);
            //Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
    
            // send message to client
            webSocket.sendTXT(num, "Connected");
          }
          break;
        case WStype_TEXT:
          Serial.println( "[" + String(num) + "] get Text: " + String((char *) payload));
    
          // send message to client
          webSocket.sendTXT(num, "message here");
    
          // send data to all connected clients
           webSocket.broadcastTXT("message here");
          break;
        case WStype_BIN:
          Serial.println( "[" + String(num) + "] get binary length: " + String(length));
          
          //hexdump(payload, length);
    
          // send message to client
          // webSocket.sendBIN(num, payload, length);
          break;
    
          default:
            break;
      }
    }
    
    void setup()
    {
      pinMode(SDCARD_CS, OUTPUT);
      digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
      
      // Serial.begin(921600);
      Serial.begin(115200);
      while (!Serial);
    
      Serial.println("\nStart Generic_WebSocketServer_W5500");
    
      for (uint8_t t = 4; t > 0; t--)
      {
        Serial.println("[SETUP] BOOT WAIT " + String(t));
        Serial.flush();
        delay(1000);
      }
    
      // start the ethernet connection and the server:
      // Use Static IP
      Ethernet.begin(mac, ip);
      //Configure IP address via DHCP
      //Ethernet.begin(mac);
    
      // server address, port and URL
      Serial.print("WebSockets Server IP address: ");
      Serial.println(Ethernet.localIP());
      
      webSocket.begin();
      webSocket.onEvent(webSocketEvent);
    }
    
    void loop()
    {
      webSocket.loop();
    }

    Debug Terminal Output when running nRF52_Blynk_W5500_Alexa

    You can see the Adafruit NRF52840_FEATHER Express board, with W5500 Ethernet shield, connects to Blynk using BlynkEthernet_WM Library

    It also uses WebSockets_Generic library to communicate with SINRIC skills, and Alexa, to control a relay using Alexa voice control via Amazon Alexa phone APP

    Code:
    Start nRF52_Blynk_W5500_Alexa using W5x00_Shield on NRF52840_FEATHER
    LittleFS Flag read = 0xd0d04321
    Flag read = 0xd0d04321
    No doubleResetDetected
    Saving DOUBLERESETDETECTOR_FLAG to DRD file : 0xd0d01234
    Saving DRD file OK
    SetFlag write = 0xd0d01234
    [881] ======= Start Default Config Data =======
    [881] Hdr=NonSSL,BName=Air-Control
    [881] Svr=account.duckdns.org,Tok=token1
    [881] Svr1=blynk-cloud.com,Tok1=<<my real Blynk auth>>
    [882] Prt=8080,SIP=
    [882] LoadCfgFile 
    [883] OK
    [883] ======= Start Stored Config Data =======
    [883] Hdr=W5X00,BName=Air-Control
    [883] Svr=account.duckdns.org,Tok=****
    [884] Svr1=blynk-cloud.com,Tok1=****
    [884] Prt=8080,SIP=
    [884] CCSum=0x262e,RCSum=0x262e
    [886] LoadCredFile 
    [886] ChkCrR: Buffer allocated, sz=37
    [886] ChkCrR:pdata=****,len=36
    [886] ChkCrR:pdata=****,len=24
    [886] OK
    [887] CrCCsum=ea7,CrRCsum=ea7
    [887] Buffer freed
    [887] Valid Stored Dynamic Data
    [889] LoadCredFile 
    [889] CrR:pdata=****,len=36
    [889] CrR:pdata=****,len=24
    [889] OK
    [889] CrCCsum=ea7,CrRCsum=ea7
    [890] Hdr=W5X00,BName=Air-Control
    [890] Svr=account.duckdns.org,Tok=****
    [890] Svr1=blynk-cloud.com,Tok1=****
    [890] Prt=8080,SIP=
    [890] MAC:FE-F8-E0-CB-D0-BD
    _pinCS = 0
    W5100 init, using SS_PIN_DEFAULT = 10, new ss_pin = 10, W5100Class::ss_pin = 10
    W5100::init: W5500, SSIZE =4096
    [2577] IP:192.168.2.89
    [2577] bg: noConfigPortal = true
    [2577] bg: noConfigPortal = true
    [2577] bg:ECon.TryB
    [2577] 
        ___  __          __
       / _ )/ /_ _____  / /__
      / _  / / // / _ \/  '_/
     /____/_/\_, /_//_/_/\_\
            /___/ v0.6.1 on Arduino
    
    [2578] BlynkArduinoClient.connect: Connecting to account.duckdns.org:8080
    [2687] Ready (ping: 3ms).
    [2754] Connected to Blynk Server = account.duckdns.org, Token = ****
    [2754] bg:EBCon
    
    Your stored Credentials :
    SINRIC API Key = ****
    Device_ID1 = ****
    [WSc] Service connected to sinric.com at url: /
    Waiting for commands from sinric.com ...
    Stop doubleResetDetecting
    Saving to DRD file : 0xd0d04321
    Saving DRD file OK
    LittleFS Flag read = 0xd0d04321
    ClearFlag write = 0xd0d04321
    [WSc] get text: {"deviceId":"****","action":"setPowerState","value":"ON"}
    Turn on for unknown device id: ****
    [WSc] get text: {"deviceId":"****","action":"setPowerState","value":"OFF"}
    Turn off for unknown device id: ****
    [WSc] get text: {"action":"test","sender":"web"}
    [WSc] received test command from sinric.com
    [WSc] get text: {"action":"test","sender":"web"}
    [WSc] received test command from sinric.com
    [WSc] get text: {"deviceId":"****","action":"setPowerState","value":"ON"}
    Turn on device id: ****
    [WSc] get text: {"deviceId":"****","action":"setPowerState","value":"OFF"}
    Turn off Device ID: ****

  2. #2
    Junior Member
    Join Date
    Aug 2020
    Posts
    4
    Hi, is the Teensy 4.1 Native Ethernet supported by the WebSockets_Generic Library?

  3. #3
    Member
    Join Date
    Jan 2020
    Location
    Toronto, Canada
    Posts
    60
    Thanks for your interest in the WebSockets_Generic Library

    Currently, the WebSockets_Generic Library doesn't support Teensy 4.1 with NativeEthernet.

    I'll modify and add that support within several days and inform you whenever it's ready.

  4. #4
    Member
    Join Date
    Jan 2020
    Location
    Toronto, Canada
    Posts
    60

    WebSockets_Generic Library release 2.4.0 to add support to Teensy 4.1 NativeEthernet

    The new release to support Teensy 4.1 NativeEthernet has been published.

    The Teensy 4.1 NativeEthernet examples can be found at Teensy NativeEthernet

    Please test and report issue in WebSockets_Generic issues


    Major Release v2.4.0

    1. Add support to Teensy 4.1 using NativeEthernet.
    2. Add support to STM32F/L/H/G/WB/MP1 using built-in LAN8742A.
    3. Sync with v2.3.4 of original WebSockets library
    4. Add Teensy, Teensy 4.1 NativeEthernet and STM32 LAN8742A examples.
    5. Fix bugs in and optimize examples.

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    4
    Thank you for your reactivity! I will test it now!

  6. #6
    Member
    Join Date
    Jan 2020
    Location
    Toronto, Canada
    Posts
    60
    You can also use this WebSockets2_Generic Library, which is also supporting Teensy 4.1 NativeEthernet

    Major Release v1.1.0
    1. Add support to Teensy using Ethernet libraries such as Ethernet, Ethernet2, Ethernet3, EthernetLarge, EthernetENC, UIPEthernet.
    2. Add support to Teensy 4.1 using NativeEthernet library.
    3. Add Version String

  7. #7
    Junior Member
    Join Date
    Aug 2020
    Posts
    4
    For usage with Teensy 4.1 and the NativeEthernet library, which Websocket Generic library would you recommend? the 1st or 2nd version? Sorry if it's a stupid question but I wonder what are the differences (again only for T4_1 with NativeEthernet)?

  8. #8
    Member
    Join Date
    Jan 2020
    Location
    Toronto, Canada
    Posts
    60
    I personally prefer the WebSockets2_Generic for future expansion (WS Server, non-blocking WS Server, etc.).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •