MiniApache
"HTTP server library"
This library allows Arduino with ethernet shield to handle HTTP requests. Library gives user simple tools to write custom, dynamic webpages and serves files directly from SD card. MiniApache is designed to make Arduino a part of Internet of Things.
Navigation
Examples
Hello world!
#include <Ethernet.h>
#include <MiniApache.h>
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // MAC address from Ethernet shield sticker under board
IPAddress ip(192, 168, 0, 179); // IP address, may need to be changed depending on network
MiniApache apache(80); // Create a server at port 80 - default for http
void setup(){
Serial.begin(9600); // Serial for debugging
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); // Disable Ethernet chip
Ethernet.begin(mac, ip); // Initialize Ethernet device
apache.begin("/served", 4); // Initialize MiniApache server: served path and SD card pin
}
void loop() {
apache.PendingRequest();
apache.ProcessRequest();
}
Change
mac
to MAC address from Ethernet shield sticker under board. On SD card create directory
served
and file within it named
index.htm
, containing
Hello World!
. After opening in browser address
192.168.0.179
you should see
Hello World!
. Just by editing contents of created directory (
served
) user can change displayed web pages. It's caused by design of
MiniApache library which serves all files in given directory (
/served
).
Listing directory
Extend previous example by modifying file tree to:
After opening in browser address 192.168.0.179
you should still see contents of index.htm
. List of files in served/data/
will be displayed under address 192.168.0.179/data/
.
Custom view
#include <Ethernet.h>
#include <MiniApache.h>
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // MAC address from Ethernet shield sticker under board
IPAddress ip(192, 168, 0, 179); // IP address, may need to be changed depending on network
MiniApache apache(80); // Create a server at port 80
void setup(){
Serial.begin(9600); // Serial for debugging
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); // Disable Ethernet chip
Ethernet.begin(mac, ip); // Initialize Ethernet device
apache.begin("/served", 4); // Initialize MiniApache server: served path and SD card pin
}
void loop() {
if(apache.PendingRequest()){ // Check for pending request
if (apache.RequestIs("/test")){ // Check if request path is equal to /test
apache.PrintHeader(); // Start response
// equivalent to PrintHeader(200, "OK", "text/html", false); - 200 (OK) response of text/html type, not cached
apache.client.print("Test succesfull"); // Write response
apache.ReportClientServed(); // Finish response
}
}
apache.ProcessRequest();
}
Change
mac
to MAC address from Ethernet shield sticker under board. Create directory
served
on SD card and file
index.htm
within it with any content. After opening in browser address
192.168.0.179/test
you should see
Test succesfull
. When your browser sends request and arduino receives it, function
apache.PendingRequest()
returns true. You can check request path with function
apache.RequestIs("/path_to_check")
. If it returns true, you should handle request. Handling requests consists of 3 phases: writing headers, writing content and closing response. Writing headers is implemented by function
apache.PrintHeader()
. Writing content is done by calling
apache.client.print("...content...")
. Closing response is implemented by function
apache.ReportClientServed()
. Calling it is very important because server will be locked until response is finished.
For advanced usage, in MiniApache.h
, check out:
- Default arguments of functions:
PrintHeader
, ReportClientServed
.
- Functions:
RequestStartsWith
, QuickResponse
.
Error handling
#include <Ethernet.h>
#include <MiniApache.h>
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // MAC address from Ethernet shield sticker under board
IPAddress ip(192, 168, 0, 179); // IP address, may need to be changed depending on network
MiniApache apache(80); // Create a server at port 80
void setup(){
Serial.begin(9600); // Serial for debugging
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); // Disable Ethernet chip
Ethernet.begin(mac, ip); // Initialize Ethernet device
apache.begin("/served", 4); // Initialize MiniApache server: served path and SD card pin
}
void loop() {
if(apache.PendingRequest()){ // Check for pending request
if (apache.RequestIs("/error")){ // Check if request path is equal to /test
apache.PrintHeader(); // Start response
apache.client.print("Error state set"); // Write response
apache.ReportClientServed(); // Finish response
apache.SetError(100); // Set server in error state
}
}
apache.ProcessRequest();
}
Change
mac
to MAC address from Ethernet shield sticker under board. Create directory
served
on SD card and file
index.htm
within it with any content. At this point server will function normally. After opening in browser address
192.168.0.179/error
you should see
Error state set
. After that, server will no longer handle requests. Instead, on any request it will display error message containing given code and corresponding message. For custom errors, high (>100) codes are advised.
MiniApache can automatically detect and report errors:
1
SD card initialization failed!
2
Can't find index file! Check SD card and reset device.
3
Ethernet module initialization failed!
GET Data
#include <Ethernet.h>
#include <MiniApache.h>
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // MAC address from Ethernet shield sticker under board
IPAddress ip(192, 168, 0, 179); // IP address, may need to be changed depending on network
MiniApache apache(80); // Create a server at port 80
void setup(){
Serial.begin(9600); // Serial for debugging
pinMode(10, OUTPUT);
digitalWrite(10, HIGH); // Disable Ethernet chip
Ethernet.begin(mac, ip); // Initialize Ethernet device
apache.begin("/served", 4); // Initialize MiniApache server: served path and SD card pin
}
void loop() {
if(apache.PendingRequest()){ // Check for pending request
if (apache.RequestIs("/get_data")){ // Check if request path is equal to /get_data
apache.PrintHeader(); // Start response
if (apache.GETVariableIs("password", "admin")){ // Check if GET variable named "password" is equal to "admin"
apache.client.print("My secret pie recipe:<br>..."); // Tell my secrets
} else {
apache.client.print("Nothing here :)"); // Not telling
}
apache.ReportClientServed(); // Finish response
}
}
apache.ProcessRequest();
}
Change
mac
to MAC address from Ethernet shield sticker under board. Create directory
served
on SD card and file
index.htm
within it with any content. After opening in browser address
192.168.0.179/get_data
you should see
Nothing here :)
. But after opening in
192.168.0.179/get_data?password=admin
you should see
My secret pie recipe: ...
. This mechanism can be used to exchange date between browser and Arduino.
For advanced usage, in MiniApache.h
, check out:
- Function:
GetGETVariable
.
- Memory handling in body of function
GETVariableIs
.