Creating a WebSocket server on an Arduino Uno w/ Shield 2

Hi everyone,

I am very new to Arduino and currently I am attempting to build a system using Arduino for a small science project. The basic idea is to use the Arduino to record the flow rate of water going through a Hall Effect flowmeter ( RS PRO Flowmeter, and then this value would be accessed by a computer connected by an ethernet cable to the Arduino, with the Arduino acting as a WebSocket server.

I am using an Arduino Uno R3 board with a Ethernet Shield 2 attached to it.

Currently, I have been able to successfully print the flow rate from the flowmeter to the serial monitor, but now I want to setup the WebSocket server to allow it to interface with a computer. I have attempted this using the WebSocketServer.h examples as a base, it compiles fine, but I cannot ping the Arduino when I connect this to a laptop via an Ethernet cable.

#include <SPI.h>
#include <Ethernet.h>
#include <WebSocketServer.h>
using namespace net;

#  define _SERIAL SerialUSB
#  define _SERIAL Serial

constexpr char kSSID[] { "SKYNET" };
constexpr char kPassword[] { "***" };
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x13 };
IPAddress ip(198, 162, 1, 100); //<<< ENTER YOUR IP ADDRESS HERE!!!
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):

int Pulses = 2;          //Arduino digital pin 2 is declared as Pulses
volatile int pulsecount; //Variable to count number of pulses

constexpr uint16_t port = 3000;
WebSocketServer wss(port);

void setup() {
  Ethernet.begin(mac, ip);
  _SERIAL.print(F("Server running at "));

  wss.onConnection([](WebSocket & ws) {
    ws.onMessage([](WebSocket & ws, const WebSocket::DataType & dataType,
    const char *message, uint16_t length) {
      switch (dataType) {
        case WebSocket::DataType::TEXT:
          _SERIAL.print(F("Received: "));
        case WebSocket::DataType::BINARY:
          _SERIAL.println(F("Received binary data"));

      ws.send(dataType, message, length);

      [](WebSocket & ws, const WebSocket::CloseCode & code, const char *reason,
    uint16_t length) {

    _SERIAL.print(F("New client: "));

    const char message[] { "Hello from Arduino server!" };
    ws.send(WebSocket::DataType::TEXT, message, strlen(message));

  //Interrupt pin 2 declared as input and pull up resitor is enabled
  pinMode(Pulses, INPUT);
  //Interrupt is attached to pin 3
  //ISR(interuupt service routine) of interrupt is CountPulses
  //Interrupt is activated on HIGH to LOW transition
  attachInterrupt(digitalPinToInterrupt(Pulses), CountPulses, FALLING);
  Serial.begin(9600);  //Starting serial monitor

void loop() {
  pulsecount = 0;  //Start counting from 0 again
  interrupts();    //Enables interrupt on arduino pin 3
  delay (2000);    //Wait 1 second
  noInterrupts();  //Disable the interrupt

  //One second is over now and we have the number of pulses in variable

  //Calculating the water flow rate in Milli Liters per minute (NEED TO CHANGE)
  float flowRate;
  flowRate = ((pulsecount * 1000.0) / (1420.0 * 2.0)); //flow rate in mL/s
  Serial.print("Flow Rate [mL/s] =");
  Serial.print(flowRate); //Print milli liters per minute on serial monitor

void CountPulses()
  pulsecount++; //Increment the variable on every pulse

Is there a better module I can use to set up a simple WebSocket server on the Arduino? Am I missing something here that prevents me from pinging the Arduino and is there anything special I need to do so that the flow rate value is briefly stored so it can be accessed via an Ethernet connection?

Apologies for the long question. Thank you in advance.

If you create a command prompt on your PC and then run ipconfig what does it tell you the ip addresses are? - might be easier to just post up the entire output of ipconfig.

It is quote common for a laptop to have Wifi as well as Ethernet, each interface has a different ip address. If these IP addresses are in the same subnet problems can occur, similarly if the PC ethernet port is set to DHCP then it will likely have a ip address not in the arduino network range.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.