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);
}