Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Support as Http server and the Websockets server on Arduino DUE #1

Closed
jakespeed1311 opened this issue May 23, 2020 · 29 comments
Closed
Labels
enhancement New feature or request

Comments

@jakespeed1311
Copy link

Hello,
Sorry i want so talk about Websockets Server Examples.

i am want wo work with the library with Arduino DUE and PlatformIO last version from Today.
My projectis based on AJAX now i want to switch some functions to Websockets.
I have an working Webserver with html files on SDCard, and i want to bring these Library as websock server on the DUE. Normal Ethernet shiled with the WizNet 5100 controller and SDslot.

Is this posible ?
The PC ist only the client and the DUE is the Server for http and Websock paralel.
The Due sends the data to the Client Browser on the pc.

I havent seen an Examble as Server also not found realy the DUE as suppotert, well only the list for the toolchain.

tanks
Jake

@khoih-prog
Copy link
Owner

khoih-prog commented May 24, 2020

@jakespeed1311

Thanks for your interest in the library.

Currently, SAM DUE is not supported. The WebSockets Server for many other boards is still not ready.

I'm in the process of porting the WebSockets library to Teensy, SAM DUE and STM32 running with W5x00, ENC28J60 and WiFiNINA, as well as make WebSockets Server working in those supported boards.

I think by next week, the new version will have those features added. But I can't promise everything is working as planned.

The library will hopefully provide the WebSocket Server feature for the SAM DUE, but you have to write the application to use the feature for your purpose (SD Card, HTTP Server, etc.).

@jakespeed1311
Copy link
Author

Hello,
thank you for the good news, i hope for good things to come. The Http server and the SDCard output are already running. Now add the websock server, that would be really cool.
I keep my fingers crossed for a smooth adaptation to the new hardware
thanks
Jake

@khoih-prog
Copy link
Owner

Some preliminary good news for SAM DUE, Teensy and STM32F

The WebSocket Server example has just been compiled smoothly, but it needs some important mods in the Arduino hardware package.

Still need some more tests to see if anything is broken. If you're willing to test, I'll post some fixes for you here early next week to have something to start working on.

@khoih-prog khoih-prog added the enhancement New feature or request label May 25, 2020
@khoih-prog
Copy link
Owner

Hi @jakespeed1311

Good news. Just posted the new release v2.2.2

New in v2.2.2

  1. Add support to Teensy boards, such as Teensy 4.1, 4.0. 3.6, 3.5, 3.2/3.1, 3.0, LC..
  2. Add support to STM32 (Nucleo-144, Nucleo-64, Nucleo-32, Discovery, STM32F1, STM32F3, STM32F4, STM32H7, STM32L0, etc.).
  3. Add support to SAM DUE
  4. Add WebSocketServer examples to those supported boards.

Remember to follows instructions in

Important Notes to add support to SAM DUE

The Generic_WebSocketServerAllFunctionsDemo_W5500 example will show you how to run both EthernetWebServer and WebSockets Server for Generic boards (including SAM DUE) with W5x00 Ethernet.

Please post your test results if OK.

Good luck,

@jakespeed1311
Copy link
Author

jakespeed1311 commented May 27, 2020 via email

@jakespeed1311
Copy link
Author

jakespeed1311 commented May 30, 2020 via email

@jakespeed1311
Copy link
Author

jakespeed1311 commented May 30, 2020 via email

@khoih-prog
Copy link
Owner

Hope you're OK now. This is my compile result of the same example for SAM DUE

In file included from /home/kh/Arduino/libraries/WebSockets_Generic-2.2.2/src/WebSocketsServer_Generic.h:43:0,
                 from /home/kh/Arduino/khoih-prog_working/WebSockets_Generic-2.2.2/examples/Generic/W5500/Generic_WebSocketServer_W5500/Generic_WebSocketServer_W5500.ino:26:
/home/kh/Arduino/libraries/WebSockets_Generic-2.2.2/src/WebSockets_Generic.h:59:6: warning: #warning Undef min/max in WebSockets_Generic [-Wcpp]
     #warning Undef min/max in WebSockets_Generic

      ^
In file included from /home/kh/Arduino/libraries/WebSockets_Generic-2.2.2/src/WebSocketsServer_Generic.h:43:0,
                 from /home/kh/Arduino/khoih-prog_working/WebSockets_Generic-2.2.2/examples/Generic/W5500/Generic_WebSocketServer_W5500/Generic_WebSocketServer_W5500.ino:26:
/home/kh/Arduino/libraries/WebSockets_Generic-2.2.2/src/WebSockets_Generic.h:180:4: warning: #warning Use SAM DUE in WebSockets_Generic [-Wcpp]
   #warning Use SAM DUE in WebSockets_Generic

    ^
In file included from /home/kh/Arduino/libraries/WebSockets_Generic-2.2.2/src/WebSockets_Generic.h:527:0,
                 from /home/kh/Arduino/libraries/WebSockets_Generic-2.2.2/src/WebSocketsServer_Generic.h:43,
                 from /home/kh/Arduino/khoih-prog_working/WebSockets_Generic-2.2.2/examples/Generic/W5500/Generic_WebSocketServer_W5500/Generic_WebSocketServer_W5500.ino:26:
/home/kh/Arduino/libraries/WebSockets_Generic-2.2.2/src/WebSockets_Generic-Impl.h:53:6: warning: #warning No CORE_HAS_LIBB64 [-Wcpp]
     #warning No CORE_HAS_LIBB64

      ^
In file included from /home/kh/Arduino/libraries/Ethernet/src/Ethernet.cpp:48:0:
/home/kh/Arduino/libraries/Ethernet/src/utility/w5100.h:68:2: warning: #warning Use 25MHz clock for W5200/W5500. Not for W5100 [-Wcpp]
 #warning Use 25MHz clock for W5200/W5500. Not for W5100
  ^
/home/kh/Arduino/libraries/Ethernet/src/utility/w5100.h:502:2: warning: #warning Use SAM3 architecture [-Wcpp]
 #warning Use SAM3 architecture
  ^
...

/home/kh/Arduino/libraries/Ethernet/src/utility/w5100.cpp:124:2: warning: #warning w5100.cpp Use fallback, change SS_PIN_DEFAULT to 10 [-Wcpp]
 #warning w5100.cpp Use fallback, change SS_PIN_DEFAULT to 10
  ^
Sketch uses 99912 bytes (19%) of program storage space. Maximum is 524288 bytes.

@jakespeed1311
Copy link
Author

jakespeed1311 commented Jun 6, 2020 via email

@khoih-prog
Copy link
Owner

khoih-prog commented Jun 6, 2020

Hi @jakespeed1311

The WebSockets Server currently has severe bug, and currently is not working.

I think you got the similar issue as the other 2 issues

  1. No free space new client / Counting of clients wrong?
  2. Major issue with WiFiNINA - Arduino IoT 33

I'm working on that now. But the outcome and time frame to fix is not determined, and can be quite long. If you can find another quicker solution, I suggest you go with that.

@jakespeed1311
Copy link
Author

jakespeed1311 commented Jun 6, 2020 via email

@jakespeed1311
Copy link
Author

jakespeed1311 commented Jun 8, 2020 via email

@khoih-prog
Copy link
Owner

Hi @jakespeed1311

Thanks for your help. The same issue also happens while using other Ethernet/WiFi such as WiFiNINA, etc.
I'm narrow it down to the same client number issue, but still haven't had a solution yet.
Any more help will be appreciated.

@jakespeed1311
Copy link
Author

jakespeed1311 commented Jun 27, 2020 via email

@khoih-prog
Copy link
Owner

Hi @jakespeed1311

Please try the new WebSockets2_Generic Library I believe there is no WebSocket Server issue as this WebSockets_Generic Library.

This is terminal debug output when running SAMDUE-Ethernet-Server on SAM DUE board with Ethernet W5100 shield using EthernetLarge library, and receiving WebSockets connection from an ESP8266 board.

Starting WebSockets2_Generic SAMDUE-Ethernet-Server on SAM DUE
[SETUP] BOOT WAIT 4
[SETUP] BOOT WAIT 3
[SETUP] BOOT WAIT 2
[SETUP] BOOT WAIT 1

W5100 init, using SS_PIN_DEFAULT = 10, new ss_pin = 10, W5100Class::ss_pin = 10
W5100::init: W5100, SSIZE =4096
WebSockets Server Running and Ready on SAM DUE
IP address: 192.168.2.153, Port: 8080
H[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.153
[WS] WebsocketsServer::recvHandshakeRequest: value = ********
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from ESP8266
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.153
[WS] WebsocketsServer::recvHandshakeRequest: value = ********
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from ESP8266
HHHHHHHHH

Why do we need this WebSockets2_Generic library

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

This WebSockets2_Generic library is based on and modified from Gil Maimon's ArduinoWebSockets Library to provide support to many more boards, such as Arduino SAMD21, Adafruit SAMD21/SAMD51, nRF52, STM32, Teensy, SAM DUE, etc. and enable those boards to use WebSockets services, including voice-control Alexa along with Blynk. Those supported boards can also run WebSockets Server. The WebSockets can be used with ESP’s WiFi, WiFiNINA, W5x00/ENC28J60/LAN8742A Ethernet, ESP8266/ESP32-AT modules/shields.


New in v1.0.1

  1. Add support to SAM DUE boards.
  2. Add support to Ethernet W5x00, using either Ethernet, EthernetLarge or Ethernet2 library.
  3. Restructure code to provide flexibility to support many more WiFi/Ethernet modules/shields in the future. Please delete the *.cpp files, replaced by *.hpp files, in the src directory, if *.cpp files still exist after installing new version.

New in v1.0.0

  1. Add support to nRF52 boards, such as AdaFruit Feather nRF52832, nRF52840 Express, BlueFruit Sense, Itsy-Bitsy nRF52840 Express, Metro nRF52840 Express, NINA_B302_ublox, NINA_B112_ublox, etc..
  2. Add support to SAMD51 (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 Alexa / SINRIC examples and supports to those boards as well as ESP8266/ESP32

@jakespeed1311
Copy link
Author

jakespeed1311 commented Jul 26, 2020 via email

@khoih-prog
Copy link
Owner

You can start from this modified sketch for SAM-DUE and WebSockets2_Generic Library.

You have to spend more time to improve it. Currently the modifications are just to show you how to make it working basically.

I'm sorry I can't spend more time to solve your issue.

  1. File
#include "defines.h"

#include <WebSockets2_Generic.h>

#include <EthernetWebServer.h>

using namespace websockets2_generic;

WebsocketsServer SocketsServer;

#define WEBSOCKETS_PORT     8080

// Change pins according to your boards
#define RED_LED     7
#define GREEN_LED   6
#define BLUE_LED    5

EthernetWebServer server(80);

#if 0
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:
      {
        //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));

      if (payload[0] == '#')
      {
        // we get RGB data

        // decode rgb data
        uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);

        analogWrite(RED_LED, ((rgb >> 16) & 0xFF));
        analogWrite(GREEN_LED, ((rgb >> 8) & 0xFF));
        analogWrite(BLUE_LED, ((rgb >> 0) & 0xFF));
      }
      break;

    default:
      break;
  }
}
#endif

void setup()
{
#if (USE_ETHERNET_LIB || USE_ETHERNET2_LIB || USE_ETHERNET_LARGE_LIB)
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
#endif
  
  Serial.begin(115200);
  while (!Serial);

  Serial.println("\nStart Generic_WebSocketServerAllFunctionsDemo_W5500");

  for (uint8_t t = 4; t > 0; t--)
  {
    Serial.println("[SETUP] BOOT WAIT " + String(t));
    Serial.flush();
    delay(1000);
  }

  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);

  digitalWrite(RED_LED, 1);
  digitalWrite(GREEN_LED, 1);
  digitalWrite(BLUE_LED, 1);

  // start the ethernet connection and the server:
  // Use Static IP
  Ethernet.begin(mac, serverIP);
  //Configure IP address via DHCP
  //Ethernet.begin(mac);

  // server address, port and URL
  Serial.print("WebSockets Server IP address: ");
  Serial.println(Ethernet.localIP());

  SocketsServer.listen(WEBSOCKETS_PORT);
 
  Serial.print(SocketsServer.available() ? "WebSockets Server Running and Ready on " : "Server Not Running on ");
  Serial.println(BOARD_NAME);
  Serial.print("IP address: ");
  Serial.print(Ethernet.localIP());
  Serial.print(", Port: ");
  Serial.println(WEBSOCKETS_PORT);    // Websockets Server Port
 
  // handle index
  server.on("/", []()
  {
    String wsServer = String("ws://192.168.2.95:") + WEBSOCKETS_PORT + "/";
    Serial.print("Request to wsServer: ");
    Serial.println(wsServer);

        
    // send index.html
    //server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':8080/', ['arduino']);connection.onopen = function () {  connection.send('Connect ' + new Date()); }; connection.onerror = function (error) {    console.log('WebSocket Error ', error);};connection.onmessage = function (e) {  console.log('Server: ', e.data);};function sendRGB() {  var r = parseInt(document.getElementById('r').value).toString(16);  var g = parseInt(document.getElementById('g').value).toString(16);  var b = parseInt(document.getElementById('b').value).toString(16);  if(r.length < 2) { r = '0' + r; }   if(g.length < 2) { g = '0' + g; }   if(b.length < 2) { b = '0' + b; }   var rgb = '#'+r+g+b;    console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/></body></html>");
    server.send(200, "text/html", "<html><head><script>var connection = new WebSocket(wsServer, ['arduino']);connection.onopen = function () {  connection.send('Connect ' + new Date()); }; connection.onerror = function (error) {    console.log('WebSocket Error ', error);};connection.onmessage = function (e) {  console.log('Server: ', e.data);};function sendRGB() {  var r = parseInt(document.getElementById('r').value).toString(16);  var g = parseInt(document.getElementById('g').value).toString(16);  var b = parseInt(document.getElementById('b').value).toString(16);  if(r.length < 2) { r = '0' + r; }   if(g.length < 2) { g = '0' + g; }   if(b.length < 2) { b = '0' + b; }   var rgb = '#'+r+g+b;    console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/></body></html>");
  });

  server.begin();
  
  digitalWrite(RED_LED, 0);
  digitalWrite(GREEN_LED, 0);
  digitalWrite(BLUE_LED, 0);
}

WebsocketsClient* client = NULL;

void loop()
{
  unsigned long t = millis();

  server.handleClient();

  client = new WebsocketsClient;

  if (client)
  {
    *client = SocketsServer.accept();
  
    if (client->available())
    {
      WebsocketsMessage msg = client->readBlocking();
  
      // log
      Serial.print("Got Message: ");
      Serial.println(msg.data());
  
      // return echo
      client->send("Echo: " + msg.data());
  
      // close the connection
      client->close();
    }

    delete client;
  }
}
  1. File defines.h
#ifndef defines_h
#define defines_h

#if ( defined(ARDUINO_SAM_DUE) || defined(__SAM3X8E__) )
  #if defined(WEBSOCKETS_ETHERNET_USE_SAMDUE)
    #undef WEBSOCKETS_ETHERNET_USE_SAMDUE
  #endif
  #define WEBSOCKETS_USE_ETHERNET             true
  #define WEBSOCKETS_ETHERNET_USE_SAMDUE      true
#else
  #error This code is intended to run only on the SAM DUE boards ! Please check your Tools->Board setting.
#endif

#if defined(WEBSOCKETS_ETHERNET_USE_SAMDUE)

// For SAM DUE
#if defined(ARDUINO_SAM_DUE)
#define BOARD_TYPE      "SAM DUE"
#elif defined(__SAM3X8E__)
#define BOARD_TYPE      "SAM SAM3X8E"
#else
#define BOARD_TYPE      "SAM Unknown"
#endif

#endif

#ifndef BOARD_NAME
  #define BOARD_NAME    BOARD_TYPE
#endif

// Just select one to be true. If all is false, default is Ethernet. 
// If more than one are true, the priority is USE_ETHERNET_LIB, USE_ETHERNET2_LIB, USE_ETHERNET_LARGE_LIB, USE_UIP_ETHERNET
#define USE_ETHERNET_LIB              false
#define USE_ETHERNET2_LIB             false
#define USE_ETHERNET_LARGE_LIB        false

#define USE_UIP_ETHERNET              false

#if USE_ETHERNET_LIB
  // Also default to Ethernet library
  #include <Ethernet.h>
  #define ETHERNET_TYPE               "W5x00 and Ethernet Library"
#elif USE_ETHERNET2_LIB
  #include <Ethernet2.h>
  #define ETHERNET_TYPE               "W5x00 and Ethernet2 Library"
#elif USE_ETHERNET_LARGE_LIB
  #include <EthernetLarge.h>
  #define ETHERNET_TYPE               "W5x00 and EthernetLarge Library"
#elif USE_UIP_ETHERNET
  #include <UIPEthernet.h>
  #include <utility/logging.h> 
  #define ETHERNET_TYPE               "ENC28J60 and UIPEthernet Library"
#else
  // Default to Ethernet library
  #include <Ethernet.h>
  #define ETHERNET_TYPE               "W5x00 and Ethernet Library"
#endif

#define DEBUG_WEBSOCKETS_PORT     Serial
// Debug Level from 0 to 4
#define _WEBSOCKETS_LOGLEVEL_     3

uint8_t mac[6] =  { 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0x04 };

// Select the IP address according to your local network
IPAddress serverIP(192, 168, 2, 95);

#define SDCARD_CS       4

#endif      //defines_h

This is the Ethernet WebServer screen

image


This is the terminal output when 2 WS clients

are connected to the SAM_DUE WS Server.


Start Generic_WebSocketServerAllFunctionsDemo_W5500
[SETUP] BOOT WAIT 4
[SETUP] BOOT WAIT 3
[SETUP] BOOT WAIT 2
[SETUP] BOOT WAIT 1

W5100 init, using SS_PIN_DEFAULT = 10, new ss_pin = 10, W5100Class::ss_pin = 10
W5100::init: W5100, SSIZE =4096
WebSockets Server IP address: 192.168.2.95
WebSockets Server Running and Ready on SAM DUE
IP address: 192.168.2.95, Port: 8080
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.95
[WS] WebsocketsServer::recvHandshakeRequest: value = MDEyMzQ1Njc4OWFiY2RlZg==
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from NUCLEO_F767ZI
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.95
[WS] WebsocketsServer::recvHandshakeRequest: value = MDEyMzQ1Njc4OWFiY2RlZg==
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from SAMD_NANO_33_IOT
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.95
[WS] WebsocketsServer::recvHandshakeRequest: value = MDEyMzQ1Njc4OWFiY2RlZg==
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from NUCLEO_F767ZI
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.95
[WS] WebsocketsServer::recvHandshakeRequest: value = MDEyMzQ1Njc4OWFiY2RlZg==
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from SAMD_NANO_33_IOT

@jakespeed1311
Copy link
Author

jakespeed1311 commented Aug 4, 2020 via email

@khoih-prog
Copy link
Owner

I'm testing here and seeing both EthernetWebServer and WebSocketsServer working simultaneously.

Try this code and see

#include "defines.h"

#include <WebSockets2_Generic.h>

#include <EthernetWebServer.h>

using namespace websockets2_generic;

WebsocketsServer SocketsServer;

#define WEBSOCKETS_PORT     8080

EthernetWebServer server(80);

int reqCount = 0;                // number of requests received

void handleRoot()
{
#define BUFFER_SIZE     400
  
  char temp[BUFFER_SIZE];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  int day = hr / 24;

  snprintf(temp, BUFFER_SIZE - 1,
           "<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>AdvancedWebServer %s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h2>Hi from EthernetWebServer!</h2>\
<h3>on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);

  server.send(200, "text/html", temp);
}

void handleNotFound()
{
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";

  for (uint8_t i = 0; i < server.args(); i++)
  {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }

  server.send(404, "text/plain", message);
}

void drawGraph()
{
  String out;
  out.reserve(3000);
  char temp[70];
  out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n";
  out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"5\" stroke=\"rgb(0, 0, 0)\" />\n";
  //out += "<g stroke=\"black\">\n";
  out += "<g stroke=\"blue\">\n";
  int y = rand() % 130;

  for (int x = 10; x < 300; x += 10)
  {
    int y2 = rand() % 130;
    sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
    out += temp;
    y = y2;
  }
  out += "</g>\n</svg>\n";

  server.send(200, "image/svg+xml", out);
}

void setup()
{
#if (USE_ETHERNET_LIB || USE_ETHERNET2_LIB || USE_ETHERNET_LARGE_LIB)
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
#endif
  
  Serial.begin(115200);
  while (!Serial);

  Serial.println("\nStarting WebSockets2_Generic SAMDUE-Ethernet_ServerAllFunctionsDemo on " + String(BOARD_NAME));
  Serial.println("Ethernet using " + String(ETHERNET_TYPE));

  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, serverIP);
  //Configure IP address via DHCP
  //Ethernet.begin(mac);

  // server address, port and URL
  Serial.print("WebSockets Server IP address: ");
  Serial.println(Ethernet.localIP());

  SocketsServer.listen(WEBSOCKETS_PORT);
 
  Serial.print(SocketsServer.available() ? "WebSockets Server Running and Ready on " : "Server Not Running on ");
  Serial.println(BOARD_NAME);
  Serial.print("IP address: ");
  Serial.print(Ethernet.localIP());
  Serial.print(", Port: ");
  Serial.println(WEBSOCKETS_PORT);    // Websockets Server Port

  server.on("/", handleRoot);
  server.on("/test.svg", drawGraph);
  server.on("/inline", []()
  {
    server.send(200, "text/plain", "This works as well");
  });

  server.onNotFound(handleNotFound);
  server.begin();
}

void loop()
{
  // ( Times to run EthernetServer / Times to run WebSocketServer ) + 1
  // NUM_TIMES larger => EthernetServer more responsive and WebSocketServer less responsive
  #define NUM_TIMES       100
  
  static uint8_t runEthernetServer  = 0;

  runEthernetServer = (runEthernetServer + 1) % NUM_TIMES;

  if (runEthernetServer != 0)
  {
    //Serial.println("S");
    
    server.handleClient();
    return;
  }
  else
  { 
    //Serial.println("W");
    
    WebsocketsClient client = SocketsServer.accept();
  
    if (client.available())
    {
      WebsocketsMessage msg = client.readBlocking();
  
      // log
      Serial.print("Got Message: ");
      Serial.println(msg.data());
  
      // return echo
      client.send("Echo: " + msg.data());
  
      // close the connection
      client.close();
    }
  }
}

Using the same defines.h


EthernetWebServer @ 192.168.2 93:80

Selection_483


WebSocketServer at the same time @ 192.168.2 93:8080

Starting WebSockets2_Generic SAMDUE-Ethernet_ServerAllFunctionsDemo on SAM DUE
Ethernet using W5x00 and Ethernet Library
[SETUP] BOOT WAIT 4
[SETUP] BOOT WAIT 3
[SETUP] BOOT WAIT 2
[SETUP] BOOT WAIT 1

W5100 init, using SS_PIN_DEFAULT = 10, new ss_pin = 10, W5100Class::ss_pin = 10
W5100::init: W5100, SSIZE =4096
WebSockets Server IP address: 192.168.2.93
WebSockets Server Running and Ready on SAM DUE
IP address: 192.168.2.93, Port: 8080
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.93
[WS] WebsocketsServer::recvHandshakeRequest: value = MDEyMzQ1Njc4OWFiY2RlZg==
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from ESP8266
[WS] WebsocketsServer::recvHandshakeRequest: value = 192.168.2.93
[WS] WebsocketsServer::recvHandshakeRequest: value = MDEyMzQ1Njc4OWFiY2RlZg==
[WS] WebsocketsServer::recvHandshakeRequest: value = websocket
[WS] WebsocketsServer::recvHandshakeRequest: value = Upgrade
[WS] WebsocketsServer::recvHandshakeRequest: value = 13
[WS] WebsocketsServer::recvHandshakeRequest: value = TinyWebsockets Client
[WS] WebsocketsServer::recvHandshakeRequest: value = Basic 
[WS] WebsocketsServer::recvHandshakeRequest: value = https://github.com/khoih-prog/Websockets2_Generic
Got Message: Hello to Server from ESP8266


@khoih-prog
Copy link
Owner

Selection_484

@khoih-prog
Copy link
Owner

Running more than 8hrs continuously and both EthernetServer and WebSocketsServer are still OK

Selection_485

@jakespeed1311
Copy link
Author

jakespeed1311 commented Aug 5, 2020 via email

@jakespeed1311
Copy link
Author

jakespeed1311 commented Aug 5, 2020 via email

@jakespeed1311
Copy link
Author

jakespeed1311 commented Aug 5, 2020 via email

@khoih-prog
Copy link
Owner

You have to run the posted sample code (with the IP address modified) successfully to be sure your board / settings are OK before changing the code gradually to fix your purpose.

If not OK, you have to fix your hardware / settings first.

Do you have to use

IPAddress serverIP(192, 168, 33, 85);

or it's better to use DHCP IP

 // start the ethernet connection and the server:
  // Use Static IP
  //Ethernet.begin(mac, serverIP);
  //Configure IP address via DHCP
  Ethernet.begin(mac);

I can't help any further

@jakespeed1311
Copy link
Author

jakespeed1311 commented Aug 5, 2020 via email

@jakespeed1311
Copy link
Author

jakespeed1311 commented Aug 5, 2020 via email

@khoih-prog
Copy link
Owner

Probably it works because your clients are constantly querying the websocket and can do that ?

If you specify your problem early, it'll be helpful and save time

OK, if I understand correctly now, your problem is that

  1. the WebSocketServer is in blocking mode
  2. you don't have any WebSockets Client
  3. WS Server is blocked, waiting for Client before moving forward
  4. The HTTP WebServer can't continue.

If so, I think I can fix the problem by modifying the WebSockets2_Generic Library.

Please wait until I find out the fix and post the new release.

I'm moving the issue to the correct place of WebSockets2_Generic Library as Running Http and Websocket Servers concurrently

From now on, please post to the new place.

@khoih-prog khoih-prog changed the title Support as Http server and the webscok server on Arduino DUE Support as Http server and the websockets server on Arduino DUE Aug 6, 2020
@khoih-prog khoih-prog changed the title Support as Http server and the websockets server on Arduino DUE Support as Http server and the Websockets server on Arduino DUE Aug 6, 2020
khoih-prog added a commit to khoih-prog/WebSockets2_Generic that referenced this issue Aug 6, 2020
### New in v1.0.6

1. Add ***non-blocking WebSockets Server*** feature to enable WS Server and WebServer running ***concurently***. See [***Support as Http server and the Websockets server on Arduino DUE***](khoih-prog/WebSockets_Generic#1) and [***Running Http and Websocket Servers concurrently***](#1). Thanks to bug report and persistence of [Jake](https://github.com/jakespeed1311).
2. Add non-blocking WebSockets Server and WebServer examples. 
3. Add Ethernet Library Patches
4. Add Arduino SAMD Packages_Patches to fix compiler errors when using [Standard Template Library STL](https://en.wikipedia.org/wiki/Standard_Template_Library)
khoih-prog added a commit to khoih-prog/WebSockets2_Generic that referenced this issue Aug 6, 2020
### New in v1.0.6

1. Add ***non-blocking WebSockets Server*** feature to enable WS Server and WebServer running ***concurently***. See [***Support as Http server and the Websockets server on Arduino DUE***](khoih-prog/WebSockets_Generic#1) and [***Running Http and Websocket Servers concurrently***](#1). Thanks to bug report and persistence of [Jake](https://github.com/jakespeed1311).
2. Add non-blocking WebSockets Server and WebServer examples. 
3. Add Ethernet Library Patches
4. Add Arduino SAMD Packages_Patches to fix compiler errors when using [Standard Template Library STL](https://en.wikipedia.org/wiki/Standard_Template_Library)
khoih-prog added a commit to khoih-prog/WebSockets2_Generic that referenced this issue Aug 6, 2020
### New in v1.0.6

1. Add ***non-blocking WebSockets Server*** feature to enable WS Server and WebServer running ***concurently***. See [***Support as Http server and the Websockets server on Arduino DUE***](khoih-prog/WebSockets_Generic#1) and [***Running Http and Websocket Servers concurrently***](#1). Thanks to bug report and persistence of [Jake](https://github.com/jakespeed1311).
2. Add non-blocking WebSockets Server and WebServer examples. 
3. Add Ethernet Library Patches
4. Add Arduino SAMD Packages_Patches to fix compiler errors when using [Standard Template Library STL](https://en.wikipedia.org/wiki/Standard_Template_Library)
khoih-prog added a commit to khoih-prog/WebSockets2_Generic that referenced this issue Aug 6, 2020
### New in v1.0.6

1. Add ***non-blocking WebSockets Server*** feature to enable WS Server and WebServer running ***concurently***. See [***Support as Http server and the Websockets server on Arduino DUE***](khoih-prog/WebSockets_Generic#1) and [***Running Http and Websocket Servers concurrently***](#1). Thanks to bug report and persistence of [Jake](https://github.com/jakespeed1311).
2. Add non-blocking WebSockets Server and WebServer examples. 
3. Add Ethernet Library Patches
4. Add Arduino SAMD Packages_Patches to fix compiler errors when using [Standard Template Library STL](https://en.wikipedia.org/wiki/Standard_Template_Library)
khoih-prog added a commit to khoih-prog/WebSockets2_Generic that referenced this issue Aug 6, 2020
### New in v1.0.6

1. Add ***non-blocking WebSockets Server*** feature to enable WS Server and WebServer running ***concurently***. See [***Support as Http server and the Websockets server on Arduino DUE***](khoih-prog/WebSockets_Generic#1) and [***Running Http and Websocket Servers concurrently***](#1). Thanks to bug report and persistence of [Jake](https://github.com/jakespeed1311).
2. Add non-blocking WebSockets Server and WebServer examples. 
3. Add Ethernet Library Patches
4. Add Arduino SAMD Packages_Patches to fix compiler errors when using [Standard Template Library STL](https://en.wikipedia.org/wiki/Standard_Template_Library)
khoih-prog added a commit to khoih-prog/WebSockets2_Generic that referenced this issue Aug 6, 2020
### New in v1.0.6

1. Add ***non-blocking WebSockets Server*** feature to enable WS Server and WebServer running ***concurently***. See [***Support as Http server and the Websockets server on Arduino DUE***](khoih-prog/WebSockets_Generic#1) and [***Running Http and Websocket Servers concurrently***](#1). Thanks to bug report and persistence of [Jake](https://github.com/jakespeed1311).
2. Add non-blocking WebSockets Server and WebServer examples. 
3. Add Ethernet Library Patches
4. Add Arduino SAMD Packages_Patches to fix compiler errors when using [Standard Template Library STL](https://en.wikipedia.org/wiki/Standard_Template_Library)
@khoih-prog
Copy link
Owner

Hi @jakespeed1311

The new Version v1.0.6 has been released with these new features to fix the blocking action of WebSockets Server that prevents the concurrent running of WebServer and WebSockets Server you experienced so far.

New in WebSockets2_Generic v1.0.6

  1. Add non-blocking WebSockets Server feature to enable WS Server and WebServer running concurently. See Support as Http server and the Websockets server on Arduino DUE and Running Http and Websocket Servers concurrently. Thanks to bug report and persistence of Jake.
  2. Add non-blocking WebSockets Server and WebServer examples.
  3. Add Ethernet Library Patches
  4. Add Arduino SAMD Packages_Patches to fix compiler errors when using Standard Template Library STL

Please try the example SAMDUE-Ethernet_AdvancedWebServer to have feelings. Then modify it to fix your purpose.

Please be noted that your bug report and persistence, resulting in the new Version v1.0.6, has been noted in Contributions-and-Thanks

khoih-prog added a commit to khoih-prog/WebSockets2_Generic that referenced this issue Sep 9, 2020
### New in v1.0.6

1. Add ***non-blocking WebSockets Server*** feature to enable WS Server and WebServer running ***concurrently***. See [***Support as Http server and the Websockets server on Arduino DUE***](khoih-prog/WebSockets_Generic#1) and [***Running Http and Websocket Servers concurrently***](#1). Thanks to bug report and persistence of [Jake](https://github.com/jakespeed1311).
2. Add non-blocking WebSockets Server and WebServer examples. 
3. Add Ethernet Library Patches
4. Add Arduino SAMD Packages_Patches to fix compiler errors when using [Standard Template Library STL](https://en.wikipedia.org/wiki/Standard_Template_Library)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants