A
A
Acefrog2021-02-21 12:28:21
Python
Acefrog, 2021-02-21 12:28:21

How to connect python tcp server and arduino tcp client?

python:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM,)

server.bind(("192.168.1.11", 8000))

server.listen(1)

while True:
    user_socket, address = server.accept()

    user_socket.send("Hello User!".encode("utf-8"))

arduino:
#include <UIPEthernet.h>

EthernetServer server = EthernetServer(8000);

void setup()
{
  Serial.begin(9600);

  // MAC адрес, должен быть уникальным в сети
  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};

  // IP адрес сервера, измените на адрес в своей подсети
  IPAddress myIP(192,168,1,99);

  Ethernet.begin(mac,myIP);

  server.begin();
}

void loop()
{
  size_t size;

  if (EthernetClient client = server.available())
    {
      while((size = client.available()) > 0)
      client.println("HELLO!!!!");
      client.stop();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kalapanga, 2021-02-21
@kalapanga

I won’t say anything about the python, but in the Arduino sketch, just some kind of meaningless set of letters is written.
The TcpClient example is attached to the library - take it as a basis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question