Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:basic code for ESP server added #48

Merged
merged 1 commit into from
Aug 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions ESP_Server/server.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid = "PSLab_ESP";

ESP8266WebServer server(80);

//Simple http server to handle different requests on endpoints

//Method to handle root endpoint
void handleRoot() {
server.send(200, "text/plain", "hello PSLab");
}

//Method to handle 404 error
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}

void setup(void) {
//Create wifi accesspoint
WiFi.softAP(ssid);
IPAddress myIP = WiFi.softAPIP();

//Declare endpoints here
server.on("/", handleRoot);

//endpoint to get vesion of PSLab
server.on("/version", []() {
server.send(200, "text/plain", "This will return PSLab version");
});

//end point for 404 error
server.onNotFound(handleNotFound);

//initiate the server
server.begin();
}

void loop(void) {
server.handleClient();
}