Resolved!
Here is my Working Code for TCP Client
/*
Arduino-ESP8266 Client , Received Ultrasonic Value from Web Server to Control Water Pump
Date: OCT 26, 2022
*/
#define DEBUG true //make a constant named "DEBUG" and it's value true. we will use it later.
#define Motor 22
String AP = "AI-THINKER_157C71"; // WiFi Access Point NAME
String PASS = ""; // WiFi Access Point PASSWORD
String HOST = "192.168.4.1"; // Your Web Server Host IP or Domain
String PORT = "80"; // Port of IP or Domain
String response = ""; //initialize a String variable named "response". we will use it later.
void setup() {
pinMode(Motor, OUTPUT); //Motor - HIGH LEVEL ACTIVE
// digitalWrite(Motor, HIGH);
Serial.begin(9600); //begin the Hardware serial communication (0, 1) at speed 9600.
Serial1.begin(115200); //begin the software serial communication (2, 3) at speed 9600.
Serial.println("Starting TCP Client");
InitWifiModule(); //call this user-defined function "InitWifiModule()" to initialize a communication between the Serial1 and your access point (Home Router or even your mobile hotspot).
}
void InitWifiModule() {
sendData("AT+RST\r\n", 2000, DEBUG); //reset the Serial1 module.
delay(3000);
sendData("AT+CWMODE=1\r\n", 1500, DEBUG); // 1=Station Mode
delay(1000);
sendData("AT+CWLAP\r\n", 1500, DEBUG); //Check availabe WiFi APs
delay(1000);
sendData("AT+CIFSR\r\n", 1500, DEBUG); //Show IP Address, and the MAC Address.
delay(1000);
sendData("AT+CIPSTA?\r\n", 1500, DEBUG); //Show IP Address, and the MAC Address.
delay(1000);
if (response.indexOf("192.168.") >= 0) {
Serial.println("IP Assigned\r\n");
}
else {
Serial.println("No IP, Please check AP\r\n");
WiFi_Connect();
}
sendData("AT+CIPMUX=1\r\n", 1500, DEBUG); //Multiple conections.
delay(1000);
}
int WiFi_Connect() {
String msg = String("");
label:
// msg = sendData("AT+CWJAP=\"AI-THINKER_157C71\",\"\"", 2000, DEBUG); //AT+CWJAP="AI-THINKER_157C71",""
msg = sendData("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 2000, DEBUG); //Connect to the WiFi network.
delay(3000);
if (msg.indexOf("CONNECTED") >= 0) {
Serial.println("GET OK");
Serial.println();
return 1;
} else {
Serial.println("NOT GET OK");
Serial.println();
sendData("AT+CWLAP\r\n", 1500, DEBUG); //Check availabe WiFi APs
goto label; //If NOT GET OK GOTO label
return 0;
}
}
void loop() //our main program, some fun are about to start
{
// sendData("AT+CIPSTART=0,\"TCP\",\"192.168.4.1\",80\r\n", 1000, DEBUG);
sendData("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT + "\r\n", 500, DEBUG); // AT+CIPSTART=0,"TCP","192.168.4.1",80
sendData("AT+CIPSEND=0,16\r\n", 500, DEBUG);
Serial.println("\r\nReceiving Data…\r\n");
sendData("GET / HTTP/1.1\r\n\r\n", 200, DEBUG); //GET /test.html HTTP/1.1
Serial.println();
delay(1500);
if (Serial1.available()) //if there's any data received and stored in the serial receive buffer, go and excute the if-condition body. If not, dont excute the if-condition body at all.
{
Serial.println("Received Data:");
if (Serial1.find("+IPD,")) //search for the "+IPD," string in the incoming data. if it exists the ".find()" returns true and if not it returns false.
{
Serial.println("+IPD Data");
delay(10); //wait 10ms second to fill up the buffer with the data.
String inData = Serial1.readString();
Serial.println(inData);
int index1 = inData.indexOf("Level:");
int from = index1 + 6; //Start Index of Value Received from Web-Server
int index2 = inData.indexOf("0,CLOSED"); //End Index of Value Received from Web-Server
int to = index2;
String data = inData.substring(from, to);
Serial.print("Extracted Data: ");
Serial.println(data);
Serial.print("\r\nTank-1 Level: ");
int Level = data.toFloat();
Serial.print(Level);
Serial.println(" CM\r\n");
if (Level < 5) {
Serial.println("Turning ON Water Pump!\r\n");
digitalWrite(Motor, HIGH);
}
else {
Serial.println("Turning OFF Water Pump!\r\n");
digitalWrite(Motor, LOW);
}
int connectionId = Serial1.read() - 48; //Subtract 48 because the read() function returns the ASCII decimal value. And 0 (the first decimal number) starts at 48. We use it to convert from ASCI decimal value to a character value.
String closeCommand = "AT+CIPCLOSE="; //close the TCP/IP connection.
closeCommand += connectionId; //append the "connectionId" value to the string.
closeCommand += "\r\n"; //append the "\r\n" to the string. it simulates the keyboard enter press.
sendData(closeCommand, 1000, DEBUG); //then send this command to the Serial1 module to excute it.
Serial.println("\r\n******END TCP Connection*******\r\n");
}
}
}
String sendData(String command, const int timeout, boolean debug) {
response = ""; //initialize a String variable named "response". we will use it later.
Serial1.print(command); //send the AT command to the Serial1 (from ARDUINO to Serial1).
long int time = millis(); //get the operating time at this specific moment and save it inside the "time" variable.
while ((time + timeout) > millis()) //excute only whitin 1 second.
{
while (Serial1.available()) //is there any response came from the Serial1 and saved in the Arduino input buffer?
{
char c = Serial1.read(); //if yes, read the next character from the input buffer and save it in the "response" String variable.
response += c; //append the next character to the response variabl. at the end we will get a string(array of characters) contains the response.
}
}
if (debug) //if the "debug" variable value is TRUE, print the response on the Serial monitor.
{
Serial.print(response);
}
return response; //return the String response.
}