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 https://uk.rs-online.com/web/p/flow-sensors/5082704/), 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;
#if PLATFORM_ARCH == PLATFORM_ARCHITECTURE_SAMD21
# define _SERIAL SerialUSB
#else
# define _SERIAL Serial
#endif
#if NETWORK_CONTROLLER == NETWORK_CONTROLLER_WIFI
constexpr char kSSID[] { "SKYNET" };
constexpr char kPassword[] { "***" };
#else
// 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!!!
#endif
// 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 "));
_SERIAL.print(Ethernet.localIP());
_SERIAL.print(F(":"));
_SERIAL.println(port);
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: "));
_SERIAL.println(message);
break;
case WebSocket::DataType::BINARY:
_SERIAL.println(F("Received binary data"));
break;
}
ws.send(dataType, message, length);
});
ws.onClose(
[](WebSocket & ws, const WebSocket::CloseCode & code, const char *reason,
uint16_t length) {
_SERIAL.println(F("Disconnected"));
});
_SERIAL.print(F("New client: "));
_SERIAL.println(ws.getRemoteIP());
const char message[] { "Hello from Arduino server!" };
ws.send(WebSocket::DataType::TEXT, message, strlen(message));
});
wss.begin();
//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() {
wss.listen();
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
//'pulsecount'
//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
Serial.println();
}
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.