Hello , i'm trying to make RC Plane with Arduino Uno + Esp8266 esp-01. i was updated firmware of esp to sdk 2.1 and i changed to baud rate 9600 for stable communication with software serial libarary. i'm trying to control servo angle with http requests, but i dont understand whats wrong with my diagram or my code . i attached electronic diagram image and below my project code.
#include <SoftwareSerial.h>
#include <Servo.h>
Servo sg90;
SoftwareSerial esp(2,3);
void WebServer(void){
if(esp.find("+IPD,")){ //if incoming data include "+IPD" command enter
String espRead = esp.readString(); delay(500); //reading esp response
String Header,Content;
Header = "HTTP/1.1 200 OK\r\n";
Header += "Content-Type: text/html\r\n";
Header += "Connection: close\r\n";
//Header += "Refresh: 5\r\n";
Content += "<head> Hello World </head><body>";
Content += "<a href=\"?p=45\"><button type='button' style='font-size: -webkit-xxx-large;'>45</button></a>";
Content += "<a href=\"?p=90\"><button type='button' style='font-size: -webkit-xxx-large;'>90</button></a>";
Content += "<a href=\"?p=135\"><button type='button' style='font-size: -webkit-xxx-large;'>135</button></a>";
Content += "<a href=\"?p=180\"><button type='button' style='font-size: -webkit-xxx-large;'>180</button></a></body>";
Header += "Content-Length: ";
Header += (int)(Content.length());delay(10);
Header += "\r\n\r\n";delay(20);
String cipsend = "AT+CIPSEND=";delay(10);
cipsend +="0";
cipsend +=",";
cipsend +=(int)(Header.length()+ Content.length());
cipsend += "\r\n";delay(20);
esp.print(cipsend);delay(20);
esp.print(Header);delay(20);
esp.print(Content);delay(20);
esp.println("AT+CIPCLOSE=0");delay(200); //Connection Close
if(espRead.indexOf(":GET /?p=45")>=0){ //if request p=45
sg90.write(45);delay(10);
Serial.println("45 geldi");delay(20);
}
else if(espRead.indexOf(":GET /?p=90")>=0){ //if request p=90
sg90.write(90);delay(10);
Serial.println("90 geldi");delay(20);
}
else if(espRead.indexOf(":GET /?p=135")>=0){ //if request p=135
sg90.write(135);delay(10);
Serial.println("135 geldi");delay(20);
}
else if(espRead.indexOf(":GET /?pin=180")>=0){ //if request p=180
sg90.write(180);delay(10);
Serial.println("180 geldi");delay(20);
}
else{
Serial.println("There is no request!");delay(20); //There is no request!
}
Serial.println(cipsend);delay(100);
}
}
void SendToEsp(){ // reading Serial inputs and sending to esp software serial channel
char character2;
String message;
while(Serial.available()>0){
character2 = Serial.read();
if(character2 !='\n'){ //if character not end marker append to message string
//if(isAscii(character2)){
message += character2;
//}
delay(10);
}
else{ // if character is end marker then send message to esp
esp.println(message);
Serial.println("Esp outgoing: "+message);
message=" ";delay(10);
}
}
}
void ReadFromEsp(){
char character;
String text;
while(esp.available()>0){
Serial.println(esp.readString());
character = esp.read();
if(character !='\n'){
delay(50);
if(isAscii(character)){
text += character;
}
delay(10);
}
else{
Serial.println(text);
text=" ";
delay(10);
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);delay(200); //Debugging serial port
Serial.println("Started"); delay(200);
esp.begin(9600);delay(200); // Software Serial port for esp8266
esp.println("AT"); delay(500); //ESP8266 check ready.
while(!esp.find("OK")){
esp.println("AT");
Serial.println("ESP Not Found!... Trying again...");
}
Serial.println("Found it!");delay(20);
esp.println("AT+CWMODE=2"); //ESP8266 set ap mode.
while(!esp.find("OK")){
esp.println("AT+CWMODE=2");
Serial.println("ESP not set for ap mode... Trying again...");
}
Serial.println("Ap mode has been set.(AT+CWMODE2)");delay(500);
esp.println("AT+CIPMUX=1"); //ESP8266 Set TCP multi
while(!esp.find("OK")){
esp.println("AT+CIPMUX=1");
Serial.println("Multi setting..(AT+CIPMUX1)");delay(50);
}
delay(100);
Serial.println("Multi has been set.(AT+CIPMUX1)");
esp.println("AT+CIPSERVER=1,80");delay(100); //Server 80 port openning
while(!esp.find("OK")){
delay(12);
esp.println("AT+CIPSERVER=1,80");
Serial.println("ESP 80 portunu açamadı... Tekrar deneniyor...AT+CIPSERVER1,80");
}
Serial.println("TCP Port ayarlandı. AT+CIPSERVER1,80"); delay(100);
//esp.println("AT+CIPSTATUS");
//delay(1500);
//esp.println("AT+CIFSR?");
//delay(1500);
//esp.println("AT+CWLIF?");
//delay(1500);
//esp.println("AT+UART_DEF?");
//delay(1500);
sg90.attach(7);
}
void loop() {
// put your main code here, to run repeatedly:
SendToEsp();
ReadFromEsp();
WebServer();
}