library/CodeRacer_MKII/Webserver.cpp
2021-07-09 15:30:20 +02:00

142 lines
4.9 KiB
C++

#include "Webserver.h"
/** @brief Constructor for the webserver
* @param ssid Network SSID
* @param password password of the network the CodeRacer is connected to
* @return nothing
*/
Codeserver::Codeserver(char *ssid, char *password){
Ssid= ssid;
Password= password;
}
/** @brief Sends an error if no valid answer to a GET request has been found
*/
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
/** @brief The bread-and-butter-routine of the webserver. Connects to a Wifi network with the SSID and password defined by the constructor. Contains various routines
* telling the server what to do if a GET request comes in by the client.
*/
void Codeserver::Run(){
//Connecting to Wifi network
Serial.print("Connecting to");
Serial.println(Ssid);
WiFi.begin(Ssid, Password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Definition for events triggered by the GET request of the client.You can add events using server.on() here to call routines of the CodeRacer-
// if you define the GET request correctly in the HTML code.
server.on("/", HTTP_GET, [] (AsyncWebServerRequest *request){
String message;
request->send(200, "text/html", String(indexHTML));
Serial.printf("Webpage sent\n");
});
server.on("/SetInactive", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.set_inactive();
request->send(200, "text/plain", "Hello world");
//this text is returned with the AJAX GET- request, but not shown on the website. Needed for appropiate handling of the request though.
});
server.on("/SetActive", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.set_active();
request->send(200, "text/plain", "Hello world");
});
server.on("/DriveForward", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.drive_forward();
request->send(200, "text/plain", "Hello world");
});
server.on("/DriveBackward", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.drive_backward();
request->send(200, "text/plain", "Hello, world");
});
server.on("/StopDriving", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.stop_driving();
request->send(200, "text/plain", "Hello, world");
});
server.on("/TurnLeft", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.turn_degree(-90);
request->send(200, "text/plain", "Hello, world");
});
server.on("/TurnRight", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.turn_degree(90);
request->send(200, "text/plain", "Hello, world");
});
server.on("/ResetTable", HTTP_GET, [] (AsyncWebServerRequest *request){
Coderacer.Reset_Stats();
request->send(200, "text/plain", "Hello, world");
});
server.on("/SetSpeed", HTTP_GET, [] (AsyncWebServerRequest *request){
String Message;
if(request->hasParam("speed"))
{
Message= request->getParam("speed")->value();
Coderacer.speed_settings(Message.toInt(), Message.toInt());
Serial.printf("New Speed: %s", Message.c_str());
}
else {
request->send(200, "text/plain", "No speed sent");
}
request->send(200, "text/plain", "OK");
});
// Returns the many values contained by the table on the website via query string.
// Notice how before this routine ends, the debug_message variable on the Coderacer is resetted to avoid memory overflow.
server.on("/Values", HTTP_GET, [] (AsyncWebServerRequest *request){
String Message= "";
int leftsteps= Coderacer.show_left_stepcounter();
int leftspeed= Coderacer.drive_left_speed();
int distance= Coderacer.usonic_measure_single_shot_cm();
int drivendistancemm= Coderacer.show_distance_mm();
int rightsteps= Coderacer.show_right_stepcounter();
int rightspeed= Coderacer.drive_right_speed();
int servodegree= Coderacer.servo_position();
float drivendistancem= (float)drivendistancemm/1000.0;
String Text=Coderacer.wifi_printf();
request->send(200, "text/plain", Message+"?"+"leftstep="+leftsteps+"&leftspeed="+leftspeed+"&distance="+distance+"&DistanceMM="+drivendistancemm+"&rightstep="+rightsteps+"&rightspeed="+rightspeed+"&ServoPosition="+servodegree+"&DistanceM="+drivendistancem+"&TheMessage="+Text);
Coderacer.wifi_printf("");
});
server.on("/TurnServo", HTTP_GET, [] (AsyncWebServerRequest *request){
String message;
if(request->hasParam("degree"))
{
message= request->getParam("degree")->value();
Coderacer.servo_set_position_wait(message.toInt());
Serial.printf("New Servo Position: %s", message.c_str());
} else {
message= "No Servo message sent.";
}
request->send(200, "text/plain", "HELLO, Post"+ message);
});
// Sends an error if no valid answer has been found
server.onNotFound(notFound);
// At least, initializes the asynchronous webserver
server.begin();
}