1

I'm setting up a "Smart House" project with Arduino+Esp01F(Serial Communication). But I want to make it global server. For it I use WiFiEsp library. But for it I must Get any port (neither 80 nor 443), then make port forwarding on router. But there are no port forwarding in my router. I think how can I do it? Code:

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif
 String response = "";
char ssid[] = "Nazim";            // your network SSID (name)
char pass[] = "eminmamedov1";         // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status
int reqCount = 0;                // number of requests received

WiFiEspServer server(5000);

 WiFiEspClient client;


void setup() {
Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
Serial.println(WiFi.localIP());

WiFi.config(IPAddress(192,168,0,165));
  // start the web server on port 80
  server.begin();

}

void loop() {
   WiFiEspClient client = server.available();
   if(client){
    String res = "";
     Serial.println("New client");
      while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        res+=c;

        if (c == '\n') {
          Serial.println("Sending response");
           client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
               "\r\n");
               if(res.indexOf("on?") >=0){
               int s = res.indexOf("on?")+7;
               int f = s+2;
               String ss = res.substring(s,f);
                  int i = ss.toInt();
                 if(i != 6 && i != 7&& (i>=2)&&(i<=13)){

                  pinMode(i,OUTPUT);
                  digitalWrite(i,HIGH);
                  bool s = digitalRead(i);
                  String state = s ? "OFF\r\n" : "ON\r\n";
                  client.println(state);
                 }else{
                  client.println("ERROR\r\n");
                 }
               }else if(res.indexOf("off?") >=0){
                 int s = res.indexOf("off?")+8;
               int f = s+2;
               String ss = res.substring(s,f);
                  int i = ss.toInt();
                 if(i != 6 && i != 7&& (i>=2)&&(i<=13)){

                  pinMode(i,OUTPUT);
                  digitalWrite(i,LOW);
                  bool s = digitalRead(i);
                  String state = s ? "OFF\r\n" : "ON\r\n";
                  client.println(state);
                 }else{
                  client.println("ERROR\r\n");
                 }
               }else if(res.indexOf("state?") >=0){
                 int s = res.indexOf("state?")+9;
               int f = s+2;
               String ss = res.substring(s,f);
                  int i = ss.toInt();
                 if(i != 6 && i != 7&& (i>=2)&&(i<=13)){

                  bool s = digitalRead(i);
                  String state = s ? "OFF\r\n" : "ON\r\n";
                  client.println(state);
               }
               }





                   client.stop();

      }
      }


   }






}

17
  • 1
    You can't. Get a better router.
    – Majenko
    Commented Aug 19, 2019 at 13:49
  • 1
    Can I use for it Esp01f adapter? Commented Aug 19, 2019 at 15:37
  • 1
    Can I use UPnP Tiny library with WiFiEsp? Commented Aug 19, 2019 at 19:40
  • 1
    What? How to use it? And Can I use it with WiFiEsp + TinyUPnP Commented Aug 19, 2019 at 20:34
  • 1
    STOP. I understand. But how to use WEBSOCKETS and ALWAYS-ON CLOUD SERVER I didn't understand Commented Aug 20, 2019 at 7:43

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.