I found an example web server that works. However, it very slow. This maybe the result of serial communication rate(9600) and the time required by ESP8266 to process a command. The combination of the Pro Micro and ESP8266 is slower that Ardunio Yun for web service.
There is an undocumented ESP8266 command to report (AT+CIOBAUD?) and set (AT+CIOBAUD=) a new rate. I have tried the report command and it works. I have not tried the set command. I have found mention of this command on a few web sites. However, I have not found the command in the various lists of commands.
Additionally, there is special firmware available called NodeMCU. It may be faster and with a version of the ESP8266 with access to all its pins, then the Arduino process may not be required. I have no real knowledge of NodeMCU.
Here is the code, I modified for use with Pro Micro:
/* Basic Arduino & ESP8266 webserver from
http://www.martyncurrey.com/arduino-esp8266-web-server/
uses AltSoftSerial download from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
this can be replaced with the normal software serial
modified by G.Majewski, 8 April 2015 for use with Pro Micro
*/
//#include <AltSoftSerial.h>
// Arduino pin 8 for RX
// Arduino Pin 9 for TX
//AltSoftSerial espSerial;
// sub Serial1 for espSerial with Pro Micro second UART (hardware) pin 0,1
const bool printReply = true;
const char line[] = "-----\n\r";
int loopCount=0;
char html[50];
char command[20];
char reply[500]; // 500 seems to be the size needed
char ipAddress [20];
char name[30];
int lenHtml = 0;
char temp[5];
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for ATmeg32u4 boards only
}
Serial.println("Start\r\n\r\n");
Serial1.begin(9600); // your ESP8266 module's baud rate might be different
// reset the ESP8266
Serial.println("reset the module");
Serial1.print("AT+RST\r\n");
getReply( 2000 );
// configure as a station
Serial.println("Change to station mode");
Serial1.print("AT+CWMODE=1\r\n");
getReply( 1500 );
// connect to the network. Uses DHCP. ip will be assigned by the router.
Serial.println("Connect to a network ");
// Enter the SSID and password for your own network
Serial1.print("AT+CWJAP=\"wireless1_ext\",\"PI~355/113\"\r\n");
getReply( 6000 );
// get ip address
Serial.println("Get the ip address assigned ny the router");
Serial1.print("AT+CIFSR\r\n");
getReply( 1000 );
// parse ip address.
int len = strlen( reply );
bool done=false;
bool error = false;
int pos = 0;
while (!done)
{
if ( reply[pos] == 10) { done = true;}
pos++;
if (pos > len) { done = true; error = true;}
}
if (!error)
{
int buffpos = 0;
done = false;
while (!done)
{
if ( reply[pos] == 13 ) { done = true; }
else { ipAddress[buffpos] = reply[pos]; buffpos++; pos++; }
}
ipAddress[buffpos] = 0;
}
else { strcpy(ipAddress,"ERROR"); }
// configure for multiple connections
Serial.println("Set for multiple connections");
Serial1.print("AT+CIPMUX=1\r\n");
getReply( 1500 );
// start server on port 8
Serial.println("Start the server");
Serial1.print("AT+CIPSERVER=1,80\r\n");
getReply( 1500 );
Serial.println("");
Serial.println("Waiting for page request");
Serial.print("Connect to "); Serial.println(ipAddress);
Serial.println("");
}
void loop()
{
if(Serial1.available()) // check if the ESO8266 is sending data
{
// this is the +IPD reply - it is quite long.
// normally you would not need to copy the whole message in to a variable you can copy up to "HOST" only
// or you can just search the data character by character as you read the serial port.
getReply( 2000 );
bool foundIPD = false;
for (int i=0; i<strlen(reply); i++)
{
if ( (reply[i]=='I') && (reply[i+1]=='P') && (reply[i+2]=='D') ) { foundIPD = true; }
}
if ( foundIPD )
{
loopCount++;
// Serial.print( "Have a request. Loop = "); Serial.println(loopCount); Serial.println("");
// check to see if we have a name - look for name=
bool haveName = false;
int nameStartPos = 0;
for (int i=0; i<strlen(reply); i++)
{
if (!haveName) // just get the first occurrence of name
{
if ( (reply[i]=='n') && (reply[i+1]=='a') && (reply[i+2]=='m') && (reply[i+3]=='e') && (reply[i+4]=='=') )
{
haveName = true;
nameStartPos = i+5;
}
}
}
// get the name - copy everything from nameStartPos to the first space character
if (haveName)
{
int tempPos = 0;
bool finishedCopying = false;
for (int i=nameStartPos; i<strlen(reply); i++)
{
if ( (reply[i]==' ') && !finishedCopying ) { finishedCopying = true; }
if ( !finishedCopying ) { name[tempPos] = reply[i]; tempPos++; }
}
name[tempPos] = 0;
}
if (haveName) { Serial.print( "name = "); Serial.println(name); Serial.println(""); }
else { Serial.println( "no name entered"); Serial.println(""); }
// start sending the HTML
strcpy(html,"<html><head></head><body>");
strcpy(command,"AT+CIPSEND=0,25\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
strcpy(html,"<h1>ESP8266 Webserver</h1>");
strcpy(command,"AT+CIPSEND=0,26\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
strcpy(html,"<p>Served by Arduino and ESP8266</p>");
strcpy(command,"AT+CIPSEND=0,36\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
strcpy(html,"<p>Request number ");
itoa( loopCount, temp, 10);
strcat(html,temp);
strcat(html,"</p>");
// need the length of html
int lenHtml = strlen( html );
strcpy(command,"AT+CIPSEND=0,");
itoa( lenHtml, temp, 10);
strcat(command, temp);
strcat(command, "\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
if (haveName)
{
// write name
strcpy(html,"<p>Your name is "); strcat(html, name ); strcat(html,"</p>");
// need the length of html for the cipsend command
lenHtml = strlen( html );
strcpy(command,"AT+CIPSEND=0,"); itoa( lenHtml, temp, 10); strcat(command, temp); strcat(command, "\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
}
strcpy(html,"<form action=\""); strcat(html, ipAddress); strcat(html, "\" method=\"GET\">"); strcat(command, "\r\n");
lenHtml = strlen( html );
itoa( lenHtml, temp, 10);
strcpy(command,"AT+CIPSEND=0,");
itoa( lenHtml, temp, 10);
strcat(command, temp);
strcat(command, "\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
strcpy(html,"Name:
<input type=\"text\" name=\"name\">");
strcpy(command,"AT+CIPSEND=0,40\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
strcpy(html,"<input type=\"submit\" value=\"Submit\"></form>");
strcpy(command,"AT+CIPSEND=0,43\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
strcpy(html,"</body></html>");
strcpy(command,"AT+CIPSEND=0,14\r\n");
Serial1.print(command);
getReply( 2000 );
Serial1.print(html);
getReply( 2000 );
// close the connection
Serial1.print( "AT+CIPCLOSE=0\r\n" );
getReply( 1500 );
Serial.println("last getReply 1 ");
getReply( 1500 );
Serial.println("last getReply 2 ");
getReply( 1500 );
} // if(espSerial.find("+IPD"))
} //if(espSerial.available())
delay (100);
// drop to here and wait for next request.
}
void getReply(int wait)
{
// had to change espSerial to Serial1 due espSerial not being in scope
int tempPos = 0;
long int time = millis();
while( (time + wait) > millis())
{
while(Serial1.available())
{
char c = Serial1.read();
if (tempPos < 500) { reply[tempPos] = c; tempPos++; }
}
reply[tempPos] = 0;
}
if (printReply) { Serial.println(reply); Serial.println(line); }
}
I think I have said enough on this topic. This will be the last post, unless someone has a question. I hope this is a help.