# How send data from UNO R4 to windows (python)

**URL:** <https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401>\
**Category:** UNO R4 WiFi\
**Created:** [April 14, 2024, 12:17am UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401 "2024-04-14T00:17:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronbin](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@ronbin](https://forum.arduino.cc/u/ronbin)\
**Post date:** [April 14, 2024, 12:17am UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/1 "2024-04-14T00:17:22Z")

</div>

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 ?

```arduino
#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;
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [April 14, 2024, 7:06am UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/3 "2024-04-14T07:06:54Z")

</div>

I've written a small tutorial on interfacing with Python. See [Two ways communication between Python3 and Arduino](https://forum.arduino.cc/t/two-ways-communication-between-python3-and-arduino/1219738)

---

<div class="post-metadata">

**Author:** ![Rintin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rintin/32/1013044_2.png) [@Rintin](https://forum.arduino.cc/u/Rintin)\
**Post date:** [April 14, 2024, 7:48am UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/4 "2024-04-14T07:48:21Z")

</div>

You could write a [Flask application](https://flask.palletsprojects.com/en/3.0.x/quickstart/#the-request-object) and send the data over with a HTTP-POST request.

---

<div class="post-metadata">

**Author:** ![MrY](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mry/32/624544_2.png) [@MrY](https://forum.arduino.cc/u/MrY)\
**Post date:** [April 14, 2024, 10:09pm UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/5 "2024-04-14T22:09:48Z")

</div>

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

```arduino
client.read();

```

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

```arduino
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.

---

<div class="post-metadata">

**Author:** ![ronbin](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@ronbin](https://forum.arduino.cc/u/ronbin)\
**Post date:** [April 22, 2024, 1:05pm UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/6 "2024-04-22T13:05:56Z")

</div>

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.

```arduino
#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.");
}

```

---

<div class="post-metadata">

**Author:** ![ronbin](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@ronbin](https://forum.arduino.cc/u/ronbin)\
**Post date:** [April 22, 2024, 1:15pm UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/7 "2024-04-22T13:15:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MrY](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mry/32/624544_2.png) [@MrY](https://forum.arduino.cc/u/MrY)\
**Post date:** [April 23, 2024, 5:27pm UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/8 "2024-04-23T17:27:20Z")

</div>

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**

```arduino
/*
  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**

```arduino
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.

---

<div class="post-metadata">

**Author:** ![ronbin](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@ronbin](https://forum.arduino.cc/u/ronbin)\
**Post date:** [April 26, 2024, 11:17am UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/9 "2024-04-26T11:17:37Z")

</div>

thank you!

---

<div class="post-metadata">

**Author:** ![dpant1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dpant1/32/1029479_2.png) [@dpant1](https://forum.arduino.cc/u/dpant1)\
**Post date:** [August 25, 2024, 8:56pm UTC](https://forum.arduino.cc/t/how-send-data-from-uno-r4-to-windows-python/1247401/10 "2024-08-25T20:56:21Z")

</div>

const char for ssid, pass, remote\_pc\_ip

moderator edit: language
