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.