I am using ESP8266 with Arduino MEGA. ESP8266 is powered 3.3v via voltage divider, which is taking 5v from arduino and converting it down to 3.3v with the help of 5K and 10K resistors. AT commands seems to be working OK. I am using SD_card reader and kept my index.html file in SD_card. I am able to make a successful connection and esp also sends the whole index.html file, But the issue is my browser does not shows the page in html format, instead the whole html code appears on the browser. Can anyone help me to sort this issue ? Herez the whole code;
#include <SPI.h>
#include <SD.h>
#define SSID "Maxcotec" // change this to match your WiFi SSID
#define PASS "qamar0334" // change this to match your WiFi password
#define PORT "80" // using port 80 by default
#define DEBUG true
#define BUFFER_SIZE 1024
char buffer[BUFFER_SIZE];
//SoftwareSerial Serial2(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
File webFile, CSS_file;
String webpage;
// By default we are looking for OK\r\n
char OKrn[] = "OK\r\n";
byte wait_for_esp_response(int timeout, char* term=OKrn) {
unsigned long t=millis();
bool found=false;
int i=0;
int len=strlen(term);
// wait for at most timeout milliseconds
// or if OK\r\n is found
while(millis()<t+timeout) {
if(Serial2.available()) {
buffer[i++]=Serial2.read();
if(i>=len) {
if(strncmp(buffer+i-len, term, len)==0) {
found=true;
break;
}
}
}
}
buffer[i]=0;
Serial.print(buffer);
return found;
}
void setup()
{
Serial.begin(9600);
Serial2.begin(115200); // your esp's baud rate might be different
setupWiFi();
// print device IP address
Serial.print("device ip addr:");
//esp.println("AT+CIFSR");
//sendData("AT+RST\r\n",2000,DEBUG); // reset module
//sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
sendData("AT+CIFSR\r",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r",1000,DEBUG); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r",1000,DEBUG); // turn on server on port 80
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(53)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()> 0) {
//client.write(webFile.read()); // send web page to client
char ch = webFile.read(); // Read a character
webpage += ch; // Add it
}
webFile.close();
}
Serial.print("file contents>>>");
//Serial.println(webpage);
//delay(20000);
}
void loop()
{
if(Serial2.available()) // check if the esp is sending a message
{
/*
while(Serial2.available())
{
// The esp has data so display its output to the serial window
char c = Serial2.read(); // read the next character.
Serial.write(c);
} */
if(Serial2.find("+IPD,"))
{
delay(1000);
int connectionId = Serial2.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
//String webpage = "<h1>Hello</h1><h2>World!</h2><button id=\"11\">Toggle Pin 11</button>";//<button>LED1</button>";
//Serial.print("file contents>>>");
//Serial.println(webpage);
//delay(20000);
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=webpage.length();
cipSend +="\r\n";
sendData(cipSend,1000,DEBUG);
sendData(webpage,1000,DEBUG);
//webpage="<button>LED2</button>";
// cipSend = "AT+CIPSEND=";
// cipSend += connectionId;
// cipSend += ",";
// cipSend +=webpage.length();
// cipSend +="\r\n";
//
// sendData(cipSend,1000,DEBUG);
// sendData(webpage,1000,DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendData(closeCommand,3000,DEBUG);
}
}
}
void setupWiFi() {
// try empty AT command
Serial2.println("AT");
wait_for_esp_response(2000);
// set mode 1 (client)
Serial2.println("AT+CWMODE=1");
wait_for_esp_response(2000);
// reset WiFi module
/*
esp.print("AT+RST\r\n");
wait_for_esp_response(3000);
delay(5000);
*/
// join AP
Serial2.print("AT+CWJAP=\"");
Serial2.print("xxxxxxx");
Serial2.print("\",\"");
Serial2.print("xxxxxxx");
Serial2.println("\"");
// this may take a while, so wait for 5 seconds
wait_for_esp_response(5000);
Serial2.println("AT+CIPSTO=30");
wait_for_esp_response(1000);
// start server
Serial2.println("AT+CIPMUX=1");
wait_for_esp_response(1000);
Serial2.print("AT+CIPSERVER=1,"); // turn on TCP service
Serial2.println(PORT);
wait_for_esp_response(1000);
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
Serial2.print(command); // send the read character to the Serial2
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial2.available())
{
// The esp has data so display its output to the serial window
char c = Serial2.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
And this is what I am getting in web browser;
I am using Opera browser but I have tried in Chrome and firefox aswell. when I open this index.html page in my laptop with the same browser, it opens perfectly.