This is the sketch.
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(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
void setup()
{
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
sendData("AT+RST\r\n",2000,DEBUG); // reset module
// sendData("AT",2000,DEBUG); // send AT command
// sendData("AT+GMR\r\n",2000,DEBUG); // returns firmware
// Serial.println("checking mode");
// sendData("AT+CWMODE=?\r\n",5000,DEBUG); // check supported modes
//
// Serial.println("scanning APs");
// sendData("AT+CWLAP\r\n",21000,DEBUG); // scan list of access points
Serial.println("set mode 1");
sendData("AT+CWMODE=1\r\n",5000,DEBUG); // configure as both mode
// Serial.println("joining AP");
sendData("AT+CWJAP=\"myssid\",\"982912701284\"\r\n", 16000, DEBUG); // connect to wifi
Serial.println("Testing CIFSR");
sendData("AT+CIFSR\r\n",7000,DEBUG); // get ip address
Serial.println("setting for multiple connection");
sendData("AT+CIPMUX=1",2000,DEBUG); // configure for multiple connections
Serial.println("print MAC address");
sendData("AT+CIPSTAMAC?\r\n",2000,DEBUG); // print current MAC address
Serial.println("set port 80 for server");
sendData("AT+CIPSERVER=1\r\n,80",2000,DEBUG); // turn on server on port 80
}
void loop()
{
if(esp8266.available()) // check if the esp is sending a message
{
if(esp8266.find("+IPD,"))
{
delay(2000);
int connectionId = esp8266.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>Tyagi IoT</h1><button>LED1</button>";
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 sendData(String command, const int timeout, boolean debug)
{
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
Serial.write(esp8266.read());
}
}
}
And this is the output of the serial monitor.
AT+RST
OK
bBֆ@j⸮⸮⸮ѐ8B⸮⸮⸮⸮J⸮⸮I⸮⸮⸮⸮cD⸮⸮J⸮R
Vendor:www.ai-thinker.com
SDK Version:0.9.5(b1)
Compileset mode 1
d @⸮⸮⸮⸮I)SC\YXV5
AT+CWMODE=1
OK
AV])⸮թ*⸮T⸮PH⸮82912701284"
OK
Testing CIFSR
AT+CIFSR
+CIFSR:STAIP,"192.168.1.69"
+CIFSR:STAMAC,"xxxxxxxx" < I blanked this out
OK
setting for multiple connection
AT+CIPMUX=1print MAC address
AT+CIPSTAMAC?
OK
set port 80 for server
AV%AM⸮eUI⸮1
,80
OK
Joseph