Here is my code:
#include <SoftwareSerial.h>
SoftwareSerial wifi(2,3); // RX, TX
void setup()
{
Serial.begin(9600);
wifi.begin(115200);
pinMode(11,OUTPUT); //define default settings for pins here
digitalWrite(11,LOW);
sendCommand("AT+RST\r\n",2000); // reset module
sendCommand("AT+CWMODE=1\r\n",1000); // configure as access point
sendCommand("AT+CWJAP=\"rose\",\"\"\r\n",10000); //edit this line to match your network settings
sendCommand("AT+CIFSR\r\n",1000); // get ip address
sendCommand("AT+CIPMUX=1\r\n",1000); // configure for multiple connections
sendCommand("AT+CIPSERVER=1,80\r\n",1000); // turn on server on port 80
Serial.println("Server Ready");
}
void loop()
{
if(wifi.available()) // check if the esp is sending a message
{
if(wifi.find("+IPD,"))
{
delay(1000);
// get the connection id so that we can then disconnect
int connectionId = wifi.read()-48; // subtract 48 because the read() function returns
// the ASCII decimal value and 0 (the first decimal number) starts at 48
wifi.find("pin="); // advance cursor to "pin=" !)192.168.0.67/?pin=11
int pinNumber = (wifi.read()-48); // get first number i.e. if the pin 13 then the 1st number is 1
int secondNumber = (wifi.read()-48);
if(secondNumber>=0 && secondNumber<=9)
{
pinNumber*=10;
pinNumber +=secondNumber; // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
}
pinMode(pinNumber,OUTPUT);
digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin
// build string that is send back to device that is requesting pin toggle
String content;
content = "Pin ";
content += pinNumber;
content += " is ";
if(digitalRead(pinNumber))
{
content += "ON";
}
else
{
content += "OFF";
}
sendHTTPResponse(connectionId,content);
//send close command
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendCommand(closeCommand,1000); // close connection
}
}
}
String sendData(String command, const int timeout)
{
String response = "";
int dataSize = command.length();
char data[dataSize];
command.toCharArray(data,dataSize);
wifi.write(data,dataSize); // send the read character to the wifi
Serial.write(data,dataSize);
long int time = millis();
while( (time+timeout) > millis())
{
while(wifi.available())
{
// The esp has data so display its output to the serial window
char c = wifi.read(); // read the next character.
response+=c;
}
}
//Serial.print(response);
return response;
}
void sendHTTPResponse(int connectionId, String content)
{
// build HTTP response
String httpResponse;
String httpHeader;
// HTTP Header
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
httpResponse = httpHeader + content + " ";
sendCIPData(connectionId,httpResponse);
}
/*
* Name: sendCIPDATA
* Description: sends a CIPSEND=<connectionId>,<data> command
*
*/
void sendCIPData(int connectionId, String data)
{
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=data.length();
cipSend +="\r\n";
sendCommand(cipSend,1000);
sendData(data,1000);
}
String sendCommand(String command, const int timeout)
{
String response = "";
wifi.print(command); // send the read character to the wifi
long int time = millis();
while( (time+timeout) > millis())
{
while(wifi.available())
{
// The esp has data so display its output to the serial window
char c = wifi.read(); // read the next character.
response+=c;
}
}
//Serial.print(response);
return response;
}