A
A
Acefrog2021-02-27 09:21:01
Python
Acefrog, 2021-02-27 09:21:01

Why won't arduino connect to python?

I want to connect an arduino client to a python server, but I don't understand what the problem is, why the arduino can't connect to the server. When connecting, it writes Client connected with IP: 0.0.0.0 and that's it. Please help me figure it out.

#include <UIPEthernet.h>

const uint16_t port = 8090;
const char * host = "192.168.0.103";

void setup() {
 Serial.begin(9600);
 Serial.print("Client connected with IP: ");
 Serial.println(Ethernet.localIP());

}

void loop() {
  EthernetClient client;
  if(!client.connect(host, port))
  {
    Serial.println("Connection to host failed");
    delay(1000);
    return;
  }
  Serial.println("Connected to server successful!");

  client.print("Hello server");
  Serial.println("Disconnecting...");
  client.stop();
  delay(10000);
}

mport socket
 
s = socket.socket()         
 
s.bind(('0.0.0.0', 8090 ))
s.listen(0)                 
 
while True:
 
    client, addr = s.accept()
 
    while True:
        content = client.recv(1)
 
        if len(content) ==0:
           break
 
        else:
            print(content)
 
    print("Closing connection")
    client.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vanyamba-electronics, 2021-03-02
@vanyamba-electronics

Well, firstly, EthernetClient is defined in your sketch as a local variable inside the loop function. Whereas it should be a global variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question