Communication Arduino + ESP8266 with server by WIFI

Hello everyone,

I am trying to control an arduino + ESP8266 WIFI module over internet from a simple HTML webpage.
The connexion of ESP8266 to the WIFI is working well. I also send data to a php file, this works well also.
Now what I would like is receiving a simple message, "Hello world", sent from my website (ex: www.mywebsite.com/index.html) to my arduino through a GET request, and print the message on the serial monitor.
I am confused now, because I don't know what method to choose: through UDP or TCP?
I precise that I have no access to the router, so I couldn't use a port forwarding.

the ESP8266 module is a generic model ESP-01 and it is connected to the pin 10 (RX) and 11 (TX) of the arduino uno.

Thanks for your help.

Here is my code so far:

#include <SoftwareSerial.h>

SoftwareSerial ESP8266(10, 11);

String Network = "SSID"; 
String Password = "MyPassword";


void setup()
{

  Serial.begin(9600);
  
  ESP8266.begin(115200);
  ESP8266.println("AT+CIOBAUD=9600");
  readESP8266(4000);

  ESP8266.begin(9600);  
  InitESP8266();
  ConnectToWebsite();
}

void loop()
{
   while(ESP8266.available())
   {    
     Serial.println(ESP8266.readString());
   }  
   
}

/* Function to initialise ESP8266 */
void InitESP8266()
{  

  ESP8266.println("AT");
  readESP8266(2000);

  ESP8266.println("AT+CWMODE=3"); //Wifi mode
  readESP8266(5000);

  ESP8266.println("AT+CWJAP=\""+ Network + "\",\"" + Password +"\""); //connect wifi
  readESP8266(10000);

  ESP8266.println("AT+CIFSR"); // IP adress
  readESP8266(1000);

  ESP8266.println("AT+CIPMUX=1");  // multiple connections 
  readESP8266(1000);

}

/* Function to connect to the server */
void ConnectToWebsite()
{

  ESP8266.println("AT+CIPSTART=1,\"TCP\",\"www.mywebsite.com\",80"); //connect to website
  readESP8266(10000);

  ESP8266.println("AT+CIPSEND=1,114"); 
  readESP8266(2000);

  String httpreq = "GET /test.php?ldata=233 HTTP/1.1";
  // Make a HTTP request:
  ESP8266.println(httpreq);
  readESP8266(10000);

  ESP8266.println("Host: www.mywebsite.com");
  ESP8266.println("Connection: keep-alive");
  ESP8266.println("");   
  readESP8266(5000);

  Serial.println("\r\n");
}


//read and display message
void readESP8266(const int timeout)
{
  String reponse = "";
  long int time = millis();
  while( (time+timeout) > millis())
  {
    while(ESP8266.available())
    {
      char c = ESP8266.read();
      reponse+=c;
    }
  }
  Serial.print(reponse);   
}

I used TCP in my sketch.

You say you want to get the contents of html file, but your sketch is has (String httpreq = "GET /test.php?ldata=233 HTTP/1.1";).

ieee488:
I used TCP in my sketch.

You say you want to get the contents of html file, but your sketch is has (String httpreq = "GET /test.php?ldata=233 HTTP/1.1";).

Thanks for your answer.
In fact, it is a bidirectionel communication. Arduino send data to the website, and this is what you see in my code (String httpreq = "GET /test.php?ldata=233 HTTP/1.1";). But also I would like to be able to receive data, so I need to complete the code... That is why I am asking you guys :slight_smile:

Have you looked at the example wifi client sketch?

ieee488:
Have you looked at the example wifi client sketch?

Yes I did. I tried WifiUdpSendReceiveString example. It does not work, probably because I am using ESP8266 instead a wifi shield. I am still trying to adapt it and to see what is wrong but still not find...
Do you have an example involving ESP8266?

Thank you

there is no way for a server in Internet to send data into your local network without configuring it on the router/firewall. the client in your local network can request the data.

Juraj:
there is no way for a server in Internet to send data into your local network without configuring it on the router/firewall. the client in your local network can request the data.

OK, so does that mean I will have to use another router instead the one I should use (university)? A phone Hotspot for instance?

ArduiCrispy:
OK, so does that mean I will have to use another router instead the one I should use (university)? A phone Hotspot for instance?

has your phone or router a public IP address?

Juraj:
has your phone or router a public IP address?

Yes It have a public address. I still not tried it but I saw some ways to setup a port forwarding for android.

ArduiCrispy:
Yes I did. I tried WifiUdpSendReceiveString example. It does not work, probably because I am using ESP8266 instead a wifi shield. I am still trying to adapt it and to see what is wrong but still not find...
Do you have an example involving ESP8266?

Thank you

Part of your problem is that you don't understand HTTP basics.
Once you are connected to your router, everything else is HTTP and you have understand the handshaking.
https://www.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html