Skip to content

How can i send/recive Continous TCP socket Get/Request #3518

@alisystem

Description

@alisystem

Basic Infos

Hardware

Hardware: ESP8266-12E

Description

there are some codes available on "https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiWebServer/WiFiWebServer.ino" . I made the code work by modifying it a little but the main problem is that it Exchange data only once ,the server closes the connection when a one data exchanged .i want a continues TCP socket connection (send/receive )

Settings in IDE

Module: Generic ESP8266 Module
Flash Size: 4MB
CPU Frequency: 80Mh?

Flash Frequency: 40Mhz
Upload Using: SERIAL

Sketch

#include <ESP8266WiFi.h>

   const char *ssid = "ESPaliAP";
   const char *password = "12345678hamed";

   WiFiServer server(21);


   char incomingPacket[255];
   char  replyPacekt[] = "Hi there! Got the message :-)";



// the setup function runs once when you press reset or power the board
void setup() {
   pinMode(2, OUTPUT);
     digitalWrite(2, 1);
        delay(1000);
    Serial.begin(115200);
    Serial.println();
    Serial.print("Configuring access point...");
  WiFi.disconnect();
  delay(100);
 WiFi.mode(WIFI_STA);
    // You can remove the password parameter if you want the AP to be open. 
    WiFi.softAP(ssid, password);

    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);

// Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}





void loop() 

 {


   // Check if a client has connected
 WiFiClient client = server.available(); 
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
 client.flush();

  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 1;
  else if (req.indexOf("/gpio/1") != -1)
    val = 0;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(2, val);

  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
// when the function returns and 'client' object is detroyed



}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions