À propos
Bibliothèque de serveur web pour Arduino.
Systèmes et infrastructureProgression mesurée des étoiles
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- Historique en cours
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
README.md
OriginalaWOT
A portable HTTP server library for Arduino, with an API inspired by Express.js.
aWOT is built to work across Arduino platforms, from an Uno to an ESP32, and
on macOS, Windows, and Linux with Arduino-compatible socket adapters. It uses
the standard Arduino Client and Stream interfaces, so your route handlers
work over Wi-Fi, Ethernet, Bluetooth, or even a serial port.
Requests are Arduino Stream objects and responses are Print objects. That
means you can pass a request directly to ArduinoJson or an OTA updater, and
write a response with the same methods you already use elsewhere in Arduino.
Build an HTTP API, serve a browser interface, or combine both into a complete web application. The companion asset tooling turns your frontend build into gzipped files that mount on the server with one line.
Get started
Install aWOT through your environment's library manager:
- Arduino IDE: Open Library Manager, search for
aWOT, and choose Install. - PlatformIO: Open Library Manager, search for
aWOT, and add it to your project.
This complete example uses an ESP32 with its Arduino board package installed:
#include <WiFi.h>
#include <aWOT.h>
WiFiServer server(80);
HttpServer http;
void hello(Request& req, Response& res) {
res.print("Hello World!");
}
void setup() {
Serial.begin(115200);
WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println(WiFi.localIP());
http.get("/", hello);
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client.connected()) {
http.process(&client);
}
}
Set your Wi-Fi credentials, upload the sketch, and open the address printed in
the Serial Monitor at 115200 baud. The browser displays Hello World!.
Your transport, the same application
aWOT handles HTTP using the transport you provide. Any implementation of
Arduino's Client or Stream interfaces can be passed to http.process().
| Transport | Processing a request |
|---|---|
| Wi-Fi or Ethernet | http.process(&client) |
| Serial, with a peer sending HTTP | http.process(&Serial) |
| Native macOS sockets | http.process(&client) using the supplied adapter |
| Native Windows sockets | http.process(&client) using the Winsock adapter |
| Native Linux sockets | http.process(&client) using the POSIX adapter |
See the Uno Ethernet example,
ESP32/ESP8266 Wi-Fi example,
native macOS example,
native Windows example, and
native Linux example. The native adapters
are available in a Git checkout under .extras/.
Routes and middleware
Register handlers for HTTP methods and URL patterns. Middleware can inspect a
request and call req.next() to continue to the next matching handler.
void logRequest(Request& req, Response& res) {
Serial.println(req.path());
req.next();
}
void greet(Request& req, Response& res) {
char name[32];
if (!req.route("name", name, sizeof(name))) {
return res.sendStatus(400);
}
res.print("Hello, ");
res.print(name);
}
Add these registrations to setup(), before server.begin():
http.use(logRequest);
http.get("/hello/:name", greet);
Visiting /hello/Arduino returns Hello, Arduino. Handlers run in registration
order; returning without req.next() finishes dispatch.
Use get, head, post, put, patch, del, and options for method-specific
routes. Group routes with Router, mount them with http.use(), and customize
missing routes with http.notFound().
Works with existing Arduino libraries
Request inherits from Stream, and Response inherits from Print. Libraries
that use these interfaces can read the request body and write the response
directly.
For example, install ArduinoJson and include
<ArduinoJson.h> in your sketch. Inside a handler, read JSON from the request
and write it to the response:
JsonDocument doc;
if (deserializeJson(doc, req)) {
return res.sendStatus(400);
}
res.set("Content-Type", "application/json");
serializeJson(doc, res);
The body goes straight to the parser without an intermediate string. This
snippet uses ArduinoJson 7, whose
parser accepts Stream
and serializer accepts Print.
The same interface makes firmware uploads straightforward on supported boards. Once the updater is initialized, stream uploaded firmware from the request:
Update.writeStream(req);
The OTA example shows the complete update flow, including setup, error handling, and restarting the device.
Build the frontend and firmware together
Develop your browser interface with the frontend tools you prefer. During local
development, configure your frontend development server to proxy API requests
to the Arduino. Relative requests such as fetch('/led') then work during
development and when the interface is served by the device.
The companion awot-static tool
compresses your frontend build with gzip and generates StaticFiles.h. The
generated routes include the content type, content length, and encoding headers
for each asset.
Include the generated header:
#include "StaticFiles.h"
Then mount the files in setup() after registering your API routes:
http.use(staticFiles());
Build the frontend, generate the assets, and upload the sketch. Your device now serves both the browser interface and its HTTP API.
The full-stack tutorial walks through the workflow with a browser-controlled LED.
Explore further
| Example | What it demonstrates |
|---|---|
| Authentication | Request headers and middleware |
| WebSockets | A browser chat interface |
| Server-sent events | Streaming updates to a named room |
| File server | Serving files from storage |
| Member callbacks | Organizing handlers in classes |
| Request context | Sharing application data with handlers |
| Keep-alive | Reusing HTTP connections |
See the documentation website for guides, the API reference, and migration instructions.
License
Licensed under MIT. Based on Webduino, Copyright 2009–2014 Ben Combee, Ran Talbott, Christopher Lee, Martin Lormes, Francisco M Cuenca-Acuna.
Explorer des projets similaires
Chargement…