WebServer – the Webduino class/constructor

Description

When creating an instance of the WebServer class you can specify a prefix for the local part of the URLs and a TCP port to listen on.

To be able to access the object both in the setup() and loop() functions of your sketch you should construct the object outside of either function.

Syntax

WebServer webserver;
WebServer webserver(urlPrefix);
WebServer webserver(urlPrefix, port);

Parameters

urlPrefix
Specify a prefix to be used on the local part of all URLs. Array of char. Optional. Defaults to an empty string (i.e. no prefix). Should never end in a slash, i.e. if you want your URLs to be /lights/on and /lights/off you need to specify a prefix of /lights (see examples below)
port
Specify a TCP port to listen on. Int. Optional. Defaults to 80.

Returns

Constructors never return anything (not even void).

Examples

Instantiate with default values (no prefix, port 80):

#include "WebServer.h"
WebServer webserver;

Instatiate without a prefix, but listening on an alternative port:

#include "WebServer.h"
WebServer webserver("", 8000);

Instatiate with a prefix, so that your URLs will be /lights/on and /lights/off (listening on the default port 80):

#include "WebServer.h"
WebServer webserver("/lights");

void setup() {
  // Ethernet stuff here, e.g. set MAC and IP address
  webserver.addCommand("on", &onCmd);
  webserver.addCommand("off", &offCmd);
  webserver.begin();
}

« back to Webduino

Reply

Your email address will not be published. Required fields are marked *