A simple web server for Particle devices supporting REST and static files
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea
GenResources
examples
src
.gitignore
LICENSE
README.md
library.properties

README.md

RdWebServer

Library for a simple web server which supports a REST API and static pages/images

Usage

#include "RdWebServer.h"
#include "GenResources.h"

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(AUTOMATIC);

// Web server
RdWebServer* pWebServer = NULL;

void setup()
{
    // Construct server
    pWebServer = new RdWebServer();

    // Configure web server
    if (pWebServer)
    {
      // Add resources to web server
      pWebServer->addStaticResources(genResources, genResourcesCount);

      // Start the web server
      pWebServer->start(webServerPort);
    }
}

void loop()
{
    // Service the web server
    if (pWebServer)
      pWebServer->service();
}

See the examples folder for more details.

Documentation

To add a REST API:

  1. Include the RestAPIEndpoints helper
// API Endpoints
#include "RestAPIEndpoints.h"
RestAPIEndpoints restAPIEndpoints;
  1. Create a method to handle the callback that you will receive from RdWebServer when the API endpoint is hit
void restAPI_QueryStatus(RestAPIEndpointMsg& apiMsg, String& retStr)
{
    retStr = "{\"rslt\":\"ok\"}";
}
  1. Register the endpoint with RestAPIEndpoint helper - do this anywhere in setup()
restAPIEndpoints.addEndpoint("Q", RestAPIEndpointDef::ENDPOINT_CALLBACK, restAPI_QueryStatus, "");
  1. Add the endpoint definitions to the server

Do this after the pWebServer has been initialized to point to the instance of an RdWebServer

  // Add resources to web server
  pWebServer->addStaticResources(genResources, genResourcesCount);

LICENSE

Copyright 2017 Rob Dobson

Licensed under the MIT license