Skip to content

Commit

Permalink
ASynchronousXMLHTTP request via HTML by browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 1, 2023
1 parent 56fd332 commit c9266e3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
4 changes: 2 additions & 2 deletions PageIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ const char MAIN_page[] PROGMEM = R"=====(
document.getElementById("joy1Y").innerHTML = throttle;
throttle=Math.min(100,Math.max(throttle,-100));yaw=Math.min(100,Math.max(yaw,-100));
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "Stick1?THROTTLE="+throttle+"&YAW="+yaw, true);
xhttp.open("GET", "Stick1?THROTTLE="+throttle+"&YAW="+yaw,false );
xhttp.send();
}
function sendStick2(pitch,roll) {
document.getElementById("joy2X").innerHTML = pitch;
document.getElementById("joy2Y").innerHTML = roll;
pitch=Math.min(100,Math.max(pitch,-100));roll=Math.min(100,Math.max(roll,-100));
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "Stick2?PITCH="+pitch+"&ROLL="+roll, true);
xhttp.open("GET", "Stick2?PITCH="+pitch+"&ROLL="+roll, false);
xhttp.send();
}

Expand Down
72 changes: 37 additions & 35 deletions Web.ino
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
//First install two zip files ESPAsyncWebServer ESPAsyncTCP

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncTCP.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
#include "PageIndex.h"
const char* ssid = "ESPServo"; // your SSID
const char* password = "11111111"; //WIFI Password #include <ESP8266WebServer.h>
int Throttle=0;
Servo servo_THROTTLE,servo_YAW,servo_PITCH,servo_ROLL;
AsyncWebServer server(80);
ESP8266WebServer server(80);
void handleRoot() {
String s = MAIN_page;
server.send(200, "text/html", s);
}

void handleStick1() {
String tmp=server.arg("THROTTLE");int temp=tmp.toInt();
Throttle = map(temp,-100,100,0,180);
tmp=server.arg("YAW");temp=tmp.toInt();
int Yaw = map(temp,-100,100,0,180);
server.send(200, "text/plane", "");
if(Throttle>=0&&Throttle<=180)servo_THROTTLE.write(Throttle);
if(Yaw>=0&&Yaw<=180)servo_YAW.write(Yaw);
Serial.print("Throttle:\t");Serial.print(Throttle);
Serial.print("\t Yaw:\t");Serial.println(Yaw);
}
void handleStick2() {
String tmp=server.arg("PITCH");int temp=tmp.toInt();
int Pitch = map(temp,-100,100,0,180);
tmp=server.arg("ROLL");temp=tmp.toInt();
int Roll = map(temp,-100,100,0,180);
server.send(200, "text/plane", "");
if(Pitch>=0&&Pitch<=180)servo_PITCH.write(Pitch);
if(Roll>=0&&Roll<=180)servo_ROLL.write(Roll);
Serial.print("Pitch:\t");Serial.print(Pitch);
Serial.print("\t Roll:\t");Serial.println(Roll);

}
void NotC(){
if(Throttle>0){Throttle>4?(Throttle=Throttle-4):(Throttle=Throttle-1);
servo_THROTTLE.write(Throttle);
servo_YAW.write(90);
servo_PITCH.write(90);
servo_ROLL.write(90);Serial.print(Throttle);
delay(1000);
delay(1000); server.handleClient();
} else if (Throttle<=0) server.handleClient();
}



void setup() {
Serial.begin(115200);
delay(500);
Serial.setDebugOutput(true);
servo_THROTTLE.attach(2,1000,2000,0);
servo_YAW.attach(12,1000,2000,0);
servo_PITCH.attach(13,1000,2000,0);
Expand All @@ -34,34 +61,9 @@ void setup() {
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", MAIN_page);
});
server.on("/Stick1", HTTP_GET, [] (AsyncWebServerRequest *request) {
String tmp = request->getParam("THROTTLE")->value();
int temp=tmp.toInt();Throttle = map(temp,-100,100,0,180);
tmp=request->getParam("YAW")->value();
temp=tmp.toInt();
int Yaw = map(temp,-100,100,0,180);
if(Throttle>=0&&Throttle<=180)servo_THROTTLE.write(Throttle);
if(Yaw>=0&&Yaw<=180)servo_YAW.write(Yaw);
Serial.print("Throttle:\t");Serial.print(Throttle);
Serial.print("\t Yaw:\t");Serial.println(Yaw);
request->send(200, "text/plain", "");
});
server.on("/Stick2", HTTP_GET, [] (AsyncWebServerRequest *request) {
String tmp = request->getParam("PITCH")->value();
int temp=tmp.toInt();
int Pitch= map(temp,-100,100,0,180);
tmp = request->getParam("ROLL")->value();
temp=tmp.toInt();
int Roll = map(temp,-100,100,0,180);
if(Pitch>=0&&Pitch<=180)servo_PITCH.write(Pitch);
if(Roll>=0&&Roll<=180)servo_ROLL.write(Roll);
Serial.print("Pitch:\t");Serial.print(Pitch);
Serial.print("\t Roll:\t");Serial.println(Roll);
request->send(200, "text/plain", "");
});
server.on("/", handleRoot);
server.on("/Stick1", handleStick1);
server.on("/Stick2", handleStick2);
server.begin();
Serial.println("HTTP server started");

Expand Down

0 comments on commit c9266e3

Please sign in to comment.