AP + Webserver
Posted: Sat Oct 21, 2017 6:39 am
Hello,
I would need to help connect these two codes in one. One can create an AP and the other can create a web server, the combination of these two codes is above my power.
Firt code:
Second code:
I would need to help connect these two codes in one. One can create an AP and the other can create a web server, the combination of these two codes is above my power.
Firt code:
Code: Select all
/*
Arduino Due - ESP 8266 WiFi Module
Serial (Tx/Rx) communicate to PC via USB
Serial3 (Tx3/Rx3) connect to ESP8266
Tx3 - ESP8266 Rx
Rx3 - ESP8266 Tx
ESP8266 CH_PD Connect to ESP8266 VCC
*/
#define ASCII_0 48
#define ESP8266 Serial3
String SSID = "MojeWifi";
String PASSWORD = "123456789";
int LED = 13;
boolean FAIL_8266 = false;
String strHTML;
String strHTMLstart = "<!doctype html>\
<html>\
<head>\
<title>";
String strHTMLend ="</title>\
</head>\
<body>\
<H1>Ahoj Lidi</H1>\
</body>\
</html>";
void htlmcode(){
strHTML = "";
strHTML = strHTML + millis() + strHTMLend;
}
boolean cwJoinAP()
{
String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"";
ESP8266.println(cmd);
return waitOKfromESP8266(10);
}
void setup() {
pinMode(LED, OUTPUT);
do{
Serial.begin(115200);
ESP8266.begin(115200);
//Wait Serial Monitor to start
while(!Serial);
Serial.println("--- Start ---");
ESP8266.println("AT+RST");
delay(1000);
if(ESP8266.find("ready"))
{
Serial.println("Module is ready");
ESP8266.println("AT+CWMODE=1");
delay(2000);
//Quit existing AP, for demo
Serial.println("Quit AP");
ESP8266.println("AT+CWQAP");
delay(1000);
clearESP8266SerialBuffer();
if(cwJoinAP())
{
Serial.println("CWJAP Success");
FAIL_8266 = false;
delay(3000);
clearESP8266SerialBuffer();
//Get and display my IP
sendESP8266Cmdln("AT+CIFSR", 1000);
//Set multi connections
sendESP8266Cmdln("AT+CIPMUX=1", 1000);
//Setup web server on port 80
sendESP8266Cmdln("AT+CIPSERVER=1,80",1000);
Serial.println("Server setup finish");
}else{
Serial.println("CWJAP Fail");
delay(500);
FAIL_8266 = true;
}
}else{
Serial.println("Module have no response.");
ESP8266.println("AT+RST");
delay(3000);
FAIL_8266 = true;
}
}while(FAIL_8266);
digitalWrite(LED, HIGH);
}
void loop(){
if(ESP8266.available())
{
Serial.println("Something received");
delay(1000);
if(ESP8266.find("+IPD,"))
{
String action;
Serial.println("+IPD, found");
int connectionId = ESP8266.read()-ASCII_0;
Serial.println("connectionId: " + String(connectionId));
htlmcode();
sendHTTPResponse(connectionId, strHTML);
//Close TCP/UDP
String cmdCIPCLOSE = "AT+CIPCLOSE=";
cmdCIPCLOSE += connectionId;
sendESP8266Cmdln(cmdCIPCLOSE, 1000);
}
}
}
void sendHTTPResponse(int id, String content)
{
String response;
response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: text/html; charset=UTF-8\r\n";
response += "Content-Length: ";
response += content.length();
response += "\r\n";
response +="Connection: close\r\n\r\n";
response += content;
String cmd = "AT+CIPSEND=";
cmd += id;
cmd += ",";
cmd += response.length();
Serial.println("--- AT+CIPSEND ---");
sendESP8266Cmdln(cmd, 1000);
Serial.println("--- data ---");
sendESP8266Data(response, 1000);
}
boolean waitOKfromESP8266(int timeout)
{
do{
Serial.println("wait OK...");
delay(1000);
if(ESP8266.find("OK"))
{
return true;
}
}while((timeout--)>0);
return false;
}
//Send command to ESP8266, assume OK, no error check
//wait some time and display respond
void sendESP8266Cmdln(String cmd, int waitTime)
{
ESP8266.println(cmd);
delay(waitTime);
clearESP8266SerialBuffer();
}
//Basically same as sendESP8266Cmdln()
//But call ESP8266.print() instead of call ESP8266.println()
void sendESP8266Data(String data, int waitTime)
{
ESP8266.print(data);
delay(waitTime);
clearESP8266SerialBuffer();
}
//Clear and display Serial Buffer for ESP8266
void clearESP8266SerialBuffer()
{
Serial.println("= clearESP8266SerialBuffer() =");
while (ESP8266.available() > 0) {
char a = ESP8266.read();
Serial.write(a);
}
Serial.println("==============================");
}
Second code:
Code: Select all
char ssid[] = "ssid-site"; //SSID wifi sítě
char pwd[] = "heslo-site"; //heslo sítě
String cmd; //řetězec pro ukládání a skládání příkazů
String http;
void setup(){
Serial.begin(115200);
Serial3.begin(115200); //9600, 57600, 115200
cmd = "AT";
command(cmd, "Zkousim pritomnost modulu", "Modul komunikuje",
"Modul nekomunikuje", "OK", 3000);
cmd = "AT+RST";
command(cmd, "Restartuji modul", "Modul restartovan", "Modul se nepovedlo restartovat", "ready", 5000);
cmd = "AT+CIPMUX=1";
command(cmd, "Nastavuji vicenasobne pripojeni", "Nastaveno", "Nepovedlo se nastavit", "OK", 1000);
cmd = "AT+CWMODE=3";
command(cmd, "Nastavuji AP mod", "Nastaveno", "Nepovedlo se nastavit", "OK", 2000);
cmd = "AT+CWSAP=\"ESP-WIFI\",\"12345678\",8,3";
command(cmd, "Vytvarim pristupovy bod", "Vytvoreno", "Nepovedlo se vytvorit pristupovy bod", "OK", 3000);
cmd = "AT+CIPSERVER=1,80";
command(cmd, "Vytvarim server", "Vytvoreno", "Nepovedlo se vytvoritserver", "OK", 2000);
cmd = "AT+CIFSR";
command(cmd, "Zjistuji aktualni adresu", "", "", "", 2000);
}
void loop(){
if(waitFor("+IPD", 50)){ //hledej výskyt +IPD
Serial3.parseInt(); //jedno číslo zahoď
Serial3.parseInt(); //druhé číslo zahoď
int p = Serial3.parseInt(); //přečti třetí číslo
Serial.print("Nacetl jsem: ");
Serial.println(p);
Serial3.flush(); //zbytek zpravy me ted nezajima, zahod ho
}
resendData();
}
boolean command(String cmd, String comment, String success, String
unsuccess, char *waitForString, long waitingTime){
Serial.println();
Serial.println(comment);
Serial.println("] " + cmd);
Serial3.println(cmd);
boolean result = waitFor(waitForString, waitingTime);
if(result){
Serial.println("[ " + success);
}
else{
Serial.println("[ " + unsuccess);
}
resendData();
return result;
}
boolean waitFor(char *s, long waitingTime){
Serial3.setTimeout(waitingTime);
return Serial3.find(s);
}
boolean resendData(){
while(Serial.available()){
Serial3.write(Serial.read());
}
if(Serial3.available()){
while(Serial3.available()){
Serial.write(Serial3.read());
}
}
}