8000 Feature/path arguments by Bmooij · Pull Request #5214 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Feature/path arguments #5214

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

Closed
wants to merge 17 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add example
  • Loading branch information
bmooij-beeliners committed Nov 1, 2019
commit 37f3d59c5e02e24f62313a0da9ce7706c4862c8c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "........";
const char *password = "........";

ESP8266WebServer server(80);

const int led = 2;

void setup(void)
{
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266"))
{
Serial.println("MDNS responder started");
}

server.on("/", []() {
server.send(200, "text/plain", "hello from esp8266!");
});

server.on("/led/2/actions/{}", []() {
String action = server.pathArg(0);
if (action == "on")
{
// /led/2/actions/on
digitalWrite(led, 1);
server.send(200, "text/plain", "Led 2 on");
}
else if (action == "off")
{
// /led/2/actions/off
digitalWrite(led, 0);
server.send(200, "text/plain", "Led 2 off");
}
else
{
server.send(404, "text/plain", "Action '" + action + "' was not found");
}
});

server.begin();
Serial.println("HTTP server started");
}

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