How send data from UNO R4 to windows (python)

Hello all, many day can not figure out, how I can send data from UNO R4 to python script. I just need for test send one value. I created the next script and can send data, but I can not send this data in loop. Can you give me some advice how I can do it ?

#include "WiFiS3.h"
char ssid[] = "123451";
char pass[] = "12341";
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);
  while (status != WL_CONNECTED) {
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  server.begin();
}
 
void loop() {
  int tests = 1;
  WiFiClient client = server.available();
  if (client) {    
    String currentLine = "";
    while (client.connected()) {  
      char c = client.read();
      if (c == '\n') {    
        if (currentLine.length() == 0) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-type:text/html");
          client.println();
          
          while (1) {
            tests += 1;
            client.print(tests);
            client.println();
          }
          client.println();
        } else {
          currentLine = "";
        }
      } else if (c != '\r') {
        currentLine += c;
      }
    }
  }
}

I've written a small tutorial on interfacing with Python. See Two ways communication between Python3 and Arduino

You could write a Flask application and send the data over with a HTTP-POST request.

If I understand your code, you wish to receive data over the network. Before doing a

client.read();

You should check to see if any data is waiting to be read. To do this, you might do something like this:

while (client.connected()) {
      if (not client.available()) {
        continue;
      }
      char c = client.read();

Perhaps you could provide a better description of how your code is supposed to work. It may be easier to forget about HTTP initially, use a Python socket to interact with the Arduino, and then convert to HTTP when everything works.

Thank you! Sorry for late response, I didn't receive notification.
Actually, I need oppositely, send data from Arduino to Python. It works via HTML (web server) but very slowly.

#include "WiFiS3.h"

char ssid[] = "";
char pass[] = "";
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);
  while (status != WL_CONNECTED) 
    {
    status = WiFi.begin(ssid, pass);
    }
  server.begin();
}
  int a = 0;
void loop() {

  WiFiClient client = server.available();
    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
  //  int c = 8;
  //  client.print(c);

    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n') {
          if (currentLine.length() == 0) {
            client.println();
            a += 1;
            client.print(a);
            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
      }
    }
    client.stop();
    Serial.println("Client disconnected.");
}

Thank you! looks like your tutorial for Serial, I need wifi

Below is both a sketch and Python script to implement a simple UDP server residing on your PC and a client residing on the Arduino R4 WIFI.

Client Sketch

/*
  WiFi UDP Send data

  

*/
#include <WiFiS3.h>

int status = WL_IDLE_STATUS;
char ssid[] = "";      // your network SSID (name)
char pass[] = "";  // your network password (use for WPA, or use as key for WEP)
char remote_pc_ip[] = "192.168.1.231";
int keyIndex = 0;           // your network key index number (needed only for WEP)



char SendBuffer[] = "1";  // a string to send back

WiFiUDP Udp;


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true)
      ;
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to WiFi");

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(50008);
}

void loop() {

  // if there's data available, read a packet

  // send a reply, to the IP address and port that sent us the packet we received
  Udp.beginPacket(remote_pc_ip, 50007);
  Udp.write(SendBuffer);
  Udp.endPacket();
  SendBuffer[0]++;
  delay(1000);
}

Before compiling and uploading, you must enter your SSID, password, and PC IP address at the top of the sketch.

Python Server

import socket

ServerSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = '0.0.0.0'
port = 50007

try:
    ServerSocket.bind((host, port))
except socket.error as e:
    print(str(e))


while True:
    data, address = ServerSocket.recvfrom(2048)
    data_str = data.decode('utf-8')
    try:

        # print(f"data : {data_str}, address : {address}")
        print(f'data received: {data_str}')

    except:
        print(f"invalid value, data : {data_str}, address : {address}")

    if not data:
        print("no data")
        break

connection.close()

No modifications are necessary for the server.

Start the server and then power up the Uno R4. The value should increase by one every second.

thank you!

const char for ssid, pass, remote_pc_ip

moderator edit: language