1

I'm setting webserver with toggle led. I want to create global webserver. I`m mean hat use server with global ip. For it I must use port too. global ip/local ip:port. I want to create on 5000 port. I'm used for ESP-01f and connect it arduino. Communicating by SoftwareSerial. I used AT+CIPSTA for static ip. When I used AT+CIPSERVER=1,5000 it worked. But when I connect to it, it works, but in second connection it doesn't want to connect itor when I use digitalWrite it doesn't connect to this server. How can I solute this problem?

code:


#include <SoftwareSerial.h>



SoftwareSerial softSerial(10, 11);
String ssid = "ssid";
String ip = "staticip";
String gateway="gateway";
String sub="submask;
String password = "password";
String _response = "";
#define DEBUG true
void setup()
{

  Serial.begin(115200);

pinMode(2,OUTPUT);

  softSerial.begin(115200);
  connectWifi();
}

void connectWifi() {

  do {

    sendData("AT+RESTORE\r\n", 5000, DEBUG);
  } while (softSerial.find("OK"));
  do {

    sendData("AT\r\n", 2000, DEBUG);
  } while (softSerial.find("OK"));
  do {

    sendData("AT+CWMODE=1\r\n", 1000, DEBUG);
  } while (softSerial.find("OK"));
  do {

    sendData("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"\r\n", 5000, DEBUG);
  } while (softSerial.find("OK"));
   do {

    sendData("AT+CIPSTA=\"" + ip + "\",\"" + gateway +"\",\"" + sub + "\"\r\n", 2000, DEBUG);
  } while (softSerial.find("OK"));
  do {

    sendData("AT+CIFSR\r\n", 1000, DEBUG);
  } while (softSerial.find("OK"));
  do {

    sendData("AT+CIPMUX=1\r\n", 1000, DEBUG);
  } while (softSerial.find("OK"));
  do {

    sendData("AT+CIPSERVER=1,5000\r\n", 1000, DEBUG);
  } while (softSerial.find("OK"));





}

String sendData(String command, const int timeout, boolean debug)
{


  String response = "";

  softSerial.print(command);        long int time = millis();
  while ((time + timeout) > millis())
  {
    while (softSerial.available())
    {

      char c = softSerial.read();
      response += c;
    }
  }

  if (debug)
  {
    Serial.print(response);
  }

  return response;
}

void loop()
{

if(softSerial.available()){
  if(softSerial.find("+IPD,"){
   int connectionId = softSerial.read()-48;
softSerial.find("pin/");
int pin = softSerial.read()-48;

String htmlCode = "<label>"+String(pin)+"</label>";
digitalWrite(pin,HIGH);
sendData("AT+CIPSEND="+String(connectionId)+","+htmlCode.length()+"\r\n",1000,DEBUG);
sendData(htmlCode+"\r\n",1000,DEBUG);


sendData("AT+CIPCLOSE="+String(connectionId),3000,DEBUG);



  }
}
}

9
  • don't use restore. it clears the settings part of the flash. you wear out the flash. use AT+RST. set AT+CWAUTOCONN=0 if you don't want the esp8266 to start the connection on startup or use _CUR commands to not save the connection to flash Commented Aug 9, 2019 at 14:01
  • you should read all the request bytes. what is on pin 2? Commented Aug 9, 2019 at 14:06
  • On pin 2 led... Commented Aug 9, 2019 at 14:16
  • How can I read all request bytes Commented Aug 9, 2019 at 14:20
  • 1
    use the WiFiEsp library Commented Aug 9, 2019 at 14:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.