Arduino WebServer for sending data to Python Client (websocket)

Hi everyone,

I am very new to Arduino and I am currently trying to code a webserver using the websocket protocol, such that my Arduino Uno R3 with Ethernet Shield 2 will read the number of pulses coming from a Hall sensor flowmeter ( and will then briefly store that data, such that it could be accessed by a python program, and have this value output every second to a .csv file.

Currently, my arduino code can successfully output the number of pulses coming from the flowmeter using Serial.print(), and the arduino can be successfully pinged from a laptop, but currently, the client does not correctly output this data to a .csv file, despite no errors being produced.

This python code follows a previous example I have used before to interface with other pieces of scientific equipment, so I think the error maybe with the arduino code.

Below is the arduino code

#include <SPI.h>
#include <Ethernet.h>

int Pulses = 3;          //Arduino digital pin 2 is declared as Pulses
volatile int pulsecount; //Variable to count number of pulses
// 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, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
  //Interrupt pin 3 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

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");

  // start the server
  Serial.print("server is at ");

void loop() {

  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    // give the web browser time to receive the data
    // close the connection:
    Serial.println("client disconnected");
  pulsecount = 0;  //Start counting from 0 again
  interrupts();    //Enables interrupt on arduino pin 3
  delay (2000);    //Wait 2 seconds
  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) / (1420.0)); //flow rate in mL/s
  Serial.print("Flow Rate [mL/s] =");
  Serial.print(flowRate); //Print milli liters per second on serial monitor

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

For the sake of completeness, here is the python code too:

import asyncio
import websockets
import socket
import time
import datetime

starttime = time.time() # start value for timed data acquisition

# setup socket 1
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if True:
def acquire():
    data = s1.recv(2048)
    dataline_tmc1 = + "," + str(data) + "," + str(0) + "\n" # format and assemble data line
    file_name_tmc1 = '{:%Y%m%d}'.format( + "_flow-test.csv" # generate file name
    with open(file_name_tmc1, "a") as tmc_file1: # append dataline to file
while True:
   time.sleep(1.0 - ((time.time() - starttime) % 2.0)) # continue every  (2.0 seconds)


Does anyone have any suggestions? Am I missing something simple regarding how I can send the value flowRate to the python client?

Thanks in advance