A simple server for the ESP8266 and ESP32, that implements the W3C Web of Things API.
Install the PlatformIO CLI via Python with pip install platformio
.
Add the esp-thingserver
library through PlatformIO's package management interface. For example, in your projects platformio.ini
file, include:
[global]
lib_deps =
https://github.com/labthings/esp-thingserver
Run platformio lib install
from within your project directory to install all dependencies,
and run platformio run -e huzzah -t upload
to build and upload (replace huzzah
with your boards environment).
See the examples
folder for platformio.ini
examples.
platformio.ini
[platformio]
env_default= esp32
[global]
lib_deps =
https://github.com/labthings/esp-thingserver
monitor_speed = 115200
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
${global.lib_deps}
lib_ldf_mode = deep+
monitor_speed = ${global.monitor_speed}
[env:huzzah]
platform = espressif8266
board = huzzah
framework = arduino
lib_deps =
${global.lib_deps}
lib_ldf_mode = deep+
monitor_speed = ${global.monitor_speed}
main.cpp
#include <Arduino.h>
#include <Thing.h>
#include "WebThingAdapter.h"
// TODO: Hardcode your wifi credentials here (and keep it private)
const char *ssid = "public";
const char *password = "";
#if defined(LED_BUILTIN)
const int ledPin = LED_BUILTIN;
#else
const int ledPin = 13; // manually configure LED pin
#endif
WebThingAdapter *adapter;
const char *ledTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice led("led", "Built-in LED", ledTypes);
ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty");
bool lastOn = false;
void setup(void) {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.begin(115200);
Serial.println("");
Serial.print("Connecting to \"");
Serial.print(ssid);
Serial.println("\"");
#if defined(ESP8266) || defined(ESP32)
WiFi.mode(WIFI_STA);
#endif
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
bool blink = true;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
blink = !blink;
}
digitalWrite(ledPin, HIGH); // active low led
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
led.addProperty(&ledOn);
adapter = new WebThingAdapter(&led, "w25", WiFi.localIP());
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(led.id);
}
void loop(void) {
adapter->update();
bool on = ledOn.getValue().boolean;
digitalWrite(ledPin, on ? LOW : HIGH); // active low led
if (on != lastOn) {
Serial.print(led.id);
Serial.print(": ");
Serial.println(on);
}
lastOn = on;
}
-
If you have a complex device with large thing descriptions, you may need to increase the size of the JSON buffers. The buffer sizes are configurable as such:
// By default, buffers are 256 bytes for small documents, 1024 for larger ones // To use a pre-defined set of larger JSON buffers (4x larger) #define LARGE_JSON_BUFFERS 1 // Else, you can define your own size #define SMALL_JSON_DOCUMENT_SIZE <something> #define LARGE_JSON_DOCUMENT_SIZE <something> #include <Thing.h> #include <WebThingAdapter.h>
ESP Thing Server was originally based on the Mozilla IoT WebThing-Arduino library.