Cannot send the data from Python server to the arduino's serial monitor through socket communication

I feel very dumb to be asking this question, and since I am new to the Arduino, I am trying to learn socket communication and trying to establish the connection between Python as a server and Arduino as a client while testing my ESP32, I managed to send the data from Serial Monitor to Python server although cannot seem to do the same from the other end. i.e. Typing anything in python is not resulting or appearing in Serial monitor in arduino.

I have looked through extensive google search results (about 15-16 pages, it stopped being relevant after that sadly), read through and tried various blogs in between, tried to use commands like client.send, client.sendto, client.sendall, but sadly to no result. It feels this forum may be my last hope. I want to type in python, "World is new everyday you open your eyes." and serial monitor to read the same, through socket communication only and not using Pyserial. I have a feeling it is possible.

Anyway, here are the codes for Python script:

import socket

s = socket.socket()

s.bind(('192.168.0.108', 1224))
s.listen(0)

while True:

        client, addr = s.accept()

        while True:
                content = client.recv(32)

                if len(content) == 0:
                        break

                else:
                        print('Arduino says ', content, 'to ya!')

        print('Closing connection')
        
        input1=input(('Enter thy message: '))
        client.sendall(input1.encode('utf-8'))
        client.close()

and Arduino code:

#include <WiFi.h>
 
const char* ssid = "chacha";
const char* password =  "hamstring";
 
const uint16_t port = 1224;
const char* host = "192.168.0.108";

WiFiServer Server(1224);
 
void setup()
{
 
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("...");
  }
 
  Serial.print("WiFi connected with IP: ");
  Serial.println(WiFi.localIP());
  Server.begin();
 
}
 
void loop()
{
    if(Serial.available()>=0){
      char s = Serial.read();
      Serial.println(s);
      
      WiFiClient client;

      if (!client.connect(host, port)){

        Serial.println("Connected to the host failed!");

        delay(1000);
        return;
        
      }

      Serial.println("Connected to the server successfully.");
      client.println("Hello from ESP32!"); client.print(s); client.print("Yay?");
      if (client.available()){
        char s = client.read();
        client.print(s);
      }
      //String t = client.readString();
      //Server.println(t);
      
      Serial.println("Disconnecting...");
      client.stop();

      delay(10000);
    }

}

Apologies if I may seem like too much of a dumb or stupid person to not get this, I am very new and I hope I get this thing done. If it will be just one integer even, that Python sends to the Arduino's serial monitor, I will be super happy.

Is there a reason why the whole of the code in your loop() function depends on receiving characters from the Serial interface ?

Yeah, that is because I hope to send the data for Python to read again and again, and Serial monitor takes in input and delivers just that. It is the Python end that I am hoping to figure out.

Also thanks for a quick response and for taking interest in this! :grinning:

Your reply has confused me even more

Noting the title that you gave the topic, it sounds like you want to send data from Python on the PC to the Arduino using a socket connection. So where does the Serial interface come into it beyond using it to display the data on the Serial monitor once it has been received ?

I apologize, I followed this tutorial and edited here and there from other resources to establish a two-way communication without using pyserial. I was trying from all ends, from arduino code trying to create a server object for the WiFiServer class that maybe can read the data but such is not possible which I found out from the arduino guide. I decided then to look for other resources and even different study blogs but none ever try to even show the Python side data transfer to arduino serial monitor which it can then read.

Let me try to make it even more clearer. Let's say, I want to use Python as a Server and Arduino as a Client. Then as Python's data maybe 0 or 1 let the LED on my ESP32 blink but that is through socket communication in between them.

I am truly sorry if I am unable to explain it clearly.

Hallo naidardevil: did you solve this communication. I have the same project. THX!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.