parent
ebfbc1144c
commit
f64935eb1d
19 changed files with 5050 additions and 0 deletions
13
examples/http_server/Makefile
Normal file
13
examples/http_server/Makefile
Normal file
|
@ -0,0 +1,13 @@
|
|||
PROGRAM=http_server
|
||||
|
||||
#ESPBAUD=921600
|
||||
|
||||
EXTRA_CFLAGS=-DLWIP_HTTPD_CGI=1 -DLWIP_HTTPD_SSI=1 -I./fsdata
|
||||
|
||||
EXTRA_COMPONENTS=extras/httpd
|
||||
|
||||
include ../../common.mk
|
||||
|
||||
html:
|
||||
@echo "Generating fsdata.."
|
||||
cd fsdata && ./makefsdata
|
25
examples/http_server/fsdata/fs/404.html
Normal file
25
examples/http_server/fsdata/fs/404.html
Normal file
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, user-scalable=no">
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="css/siimple.min.css">
|
||||
<link rel="stylesheet" type="text/css" href="css/style.css">
|
||||
<link rel="shortcut icon" href="img/favicon.png">
|
||||
|
||||
<title>HTTP Server</title>
|
||||
</head>
|
||||
<body>
|
||||
<ul class="navbar">
|
||||
<li><a href="/">Home</a></li>
|
||||
<li><a href="about">About</a></li>
|
||||
</ul>
|
||||
|
||||
<div class="grid main">
|
||||
<h1>404 - Page not found</h1>
|
||||
<div class="alert alert-error">Sorry, the page you are requesting was not found on this server.</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
26
examples/http_server/fsdata/fs/about.html
Normal file
26
examples/http_server/fsdata/fs/about.html
Normal file
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, user-scalable=no">
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="css/siimple.min.css">
|
||||
<link rel="stylesheet" type="text/css" href="css/style.css">
|
||||
<link rel="shortcut icon" href="img/favicon.png">
|
||||
|
||||
<title>HTTP Server</title>
|
||||
</head>
|
||||
<body>
|
||||
<ul class="navbar">
|
||||
<li><a href="/">Home</a></li>
|
||||
<li><a class="active" href="about">About</a></li>
|
||||
</ul>
|
||||
|
||||
<div class="grid main">
|
||||
<h1>About</h1>
|
||||
<p>This server is built on httpd from LwIP.</p>
|
||||
<p>For more info see <a href="http://www.nongnu.org/lwip/2_0_0/group__httpd.html">HTTP Server documentation</a>.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
8
examples/http_server/fsdata/fs/css/siimple.min.css
vendored
Normal file
8
examples/http_server/fsdata/fs/css/siimple.min.css
vendored
Normal file
File diff suppressed because one or more lines are too long
73
examples/http_server/fsdata/fs/css/style.css
Normal file
73
examples/http_server/fsdata/fs/css/style.css
Normal file
|
@ -0,0 +1,73 @@
|
|||
ul.navbar {
|
||||
list-style-type: none;
|
||||
margin-bottom: 32px;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
background-color: #333;
|
||||
}
|
||||
ul.navbar li {
|
||||
float: left;
|
||||
}
|
||||
ul.navbar li a {
|
||||
display: block;
|
||||
color: white;
|
||||
text-align: center;
|
||||
padding: 14px 16px;
|
||||
text-decoration: none;
|
||||
}
|
||||
ul.navbar li a:hover:not(.active) {
|
||||
background-color: #111;
|
||||
}
|
||||
ul.navbar li a.active {
|
||||
background-color: #09a0f6;
|
||||
}
|
||||
@media screen and (max-width: 600px){
|
||||
ul.navbar li.right,
|
||||
ul.navbar li {float: none;}
|
||||
}
|
||||
.onoffswitch {
|
||||
position: relative; width: 90px;
|
||||
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
|
||||
}
|
||||
.onoffswitch-checkbox {
|
||||
display: none;
|
||||
}
|
||||
.onoffswitch-label {
|
||||
display: block; overflow: hidden; cursor: pointer;
|
||||
border: 2px solid #03A9F4; border-radius: 20px;
|
||||
}
|
||||
.onoffswitch-inner {
|
||||
display: block; width: 200%; margin-left: -100%;
|
||||
transition: margin 0.3s ease-in 0s;
|
||||
}
|
||||
.onoffswitch-inner:before, .onoffswitch-inner:after {
|
||||
display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
|
||||
font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.onoffswitch-inner:before {
|
||||
content: "ON";
|
||||
text-align: left;
|
||||
padding-left: 14px;
|
||||
background-color: #E1F5FE; color: #03A9F4;
|
||||
}
|
||||
.onoffswitch-inner:after {
|
||||
content: "OFF";
|
||||
padding-right: 14px;
|
||||
background-color: #FFFFFF; color: #999999;
|
||||
text-align: right;
|
||||
}
|
||||
.onoffswitch-switch {
|
||||
display: block; width: 18px; margin: 6px;
|
||||
background: #FFFFFF;
|
||||
position: absolute; top: 0; bottom: 0;
|
||||
right: 56px;
|
||||
border: 2px solid #03A9F4; border-radius: 20px;
|
||||
transition: all 0.3s ease-in 0s;
|
||||
}
|
||||
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
|
||||
margin-left: 0;
|
||||
}
|
||||
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
|
||||
right: 0px;
|
||||
}
|
BIN
examples/http_server/fsdata/fs/img/favicon.png
Normal file
BIN
examples/http_server/fsdata/fs/img/favicon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 760 B |
76
examples/http_server/fsdata/fs/index.ssi
Normal file
76
examples/http_server/fsdata/fs/index.ssi
Normal file
|
@ -0,0 +1,76 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, user-scalable=no">
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="css/siimple.min.css">
|
||||
<link rel="stylesheet" type="text/css" href="css/style.css">
|
||||
<link rel="shortcut icon" href="img/favicon.png">
|
||||
|
||||
<title>HTTP Server</title>
|
||||
</head>
|
||||
<body>
|
||||
<ul class="navbar">
|
||||
<li><a class="active" href="/">Home</a></li>
|
||||
<li><a href="about">About</a></li>
|
||||
</ul>
|
||||
|
||||
<div class="grid main">
|
||||
<h1>ESP8266 HTTP Server</h1>
|
||||
|
||||
<div class="alert alert-done">HTTP Server is up and running.</div>
|
||||
|
||||
<p>This is an example HTTP server with CGI and SSI support. The switch below will allow you to test CGI handler and turn
|
||||
the blue LED on or off.</p>
|
||||
|
||||
<div class="cover" align="center">
|
||||
<div class="onoffswitch">
|
||||
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="led-switch" onclick="gpio();">
|
||||
<label class="onoffswitch-label" for="led-switch">
|
||||
<span class="onoffswitch-inner"></span>
|
||||
<span class="onoffswitch-switch"></span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h1>Server Status</h1>
|
||||
<table class="table table-striped">
|
||||
<tr>
|
||||
<td><b>Uptime:</b></td>
|
||||
<td><!--#uptime--> seconds</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><b>Free heap:</b></td>
|
||||
<td><!--#heap--> bytes</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><b>LED state:</b></td>
|
||||
<td id="ledState"><!--#led--></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h1>How it works</h1>
|
||||
<p> Each time the server detects a tag of the form <code><!--#name--></code> in a .shtml, .ssi or .shtm file
|
||||
where <code>name</code> appears as one of the tags supplied to <code>http_set_ssi_handler</code> in the <code>pcConfigSSITags</code> array,
|
||||
an insert string is appended after the tag string in file and sent back to the client.</p>
|
||||
<p>A CGI handler function is called each time the server is asked for a file
|
||||
whose name was previously registered as a CGI function using a call to <code>http_set_cgi_handler</code>.
|
||||
This function allows you to access the parameters provided along with the URI.</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
window.onload = function () {
|
||||
var ls = document.getElementById('ledState').innerHTML;
|
||||
ls = ls.split(/-->/).pop().trim();
|
||||
document.getElementById('led-switch').checked = (ls == 'On');
|
||||
};
|
||||
function gpio() {
|
||||
if (document.getElementById('led-switch').checked)
|
||||
window.location.href = 'gpio?off=2';
|
||||
else
|
||||
window.location.href = 'gpio?on=2';
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
1361
examples/http_server/fsdata/fsdata.c
Normal file
1361
examples/http_server/fsdata/fsdata.c
Normal file
File diff suppressed because it is too large
Load diff
114
examples/http_server/fsdata/makefsdata
Executable file
114
examples/http_server/fsdata/makefsdata
Executable file
|
@ -0,0 +1,114 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
$incHttpHeader = 1;
|
||||
|
||||
open(OUTPUT, "> fsdata.c");
|
||||
print(OUTPUT "#include \"httpd/fsdata.h\"\n\n");
|
||||
|
||||
chdir("fs");
|
||||
open(FILES, "find . -type f |");
|
||||
|
||||
while($file = <FILES>) {
|
||||
|
||||
# Do not include files in CVS directories nor backup files.
|
||||
if($file =~ /(CVS|~)/) {
|
||||
next;
|
||||
}
|
||||
|
||||
chop($file);
|
||||
|
||||
if($incHttpHeader == 1) {
|
||||
open(HEADER, "> /tmp/header") || die $!;
|
||||
if($file =~ /404/) {
|
||||
print(HEADER "HTTP/1.0 404 File not found\r\n");
|
||||
} else {
|
||||
print(HEADER "HTTP/1.0 200 OK\r\n");
|
||||
}
|
||||
print(HEADER "lwIP/1.4.1 (http://savannah.nongnu.org/projects/lwip)\r\n");
|
||||
if($file =~ /\.html$/ || $file =~ /\.htm$/ || $file =~ /\.shtml$/ || $file =~ /\.shtm$/ || $file =~ /\.ssi$/) {
|
||||
print(HEADER "Content-type: text/html\r\n");
|
||||
} elsif($file =~ /\.js$/) {
|
||||
print(HEADER "Content-type: application/x-javascript\r\n\r\n");
|
||||
} elsif($file =~ /\.css$/) {
|
||||
print(HEADER "Content-type: text/css\r\n\r\n");
|
||||
} elsif($file =~ /\.ico$/) {
|
||||
print(HEADER "Content-type: image/x-icon\r\n\r\n");
|
||||
} elsif($file =~ /\.gif$/) {
|
||||
print(HEADER "Content-type: image/gif\r\n");
|
||||
} elsif($file =~ /\.png$/) {
|
||||
print(HEADER "Content-type: image/png\r\n");
|
||||
} elsif($file =~ /\.jpg$/) {
|
||||
print(HEADER "Content-type: image/jpeg\r\n");
|
||||
} elsif($file =~ /\.bmp$/) {
|
||||
print(HEADER "Content-type: image/bmp\r\n\r\n");
|
||||
} elsif($file =~ /\.class$/) {
|
||||
print(HEADER "Content-type: application/octet-stream\r\n");
|
||||
} elsif($file =~ /\.ram$/) {
|
||||
print(HEADER "Content-type: audio/x-pn-realaudio\r\n");
|
||||
} else {
|
||||
print(HEADER "Content-type: text/plain\r\n");
|
||||
}
|
||||
print(HEADER "\r\n");
|
||||
close(HEADER);
|
||||
|
||||
unless($file =~ /\.plain$/ || $file =~ /cgi/) {
|
||||
system("cat /tmp/header $file > /tmp/file");
|
||||
} else {
|
||||
system("cp $file /tmp/file");
|
||||
}
|
||||
} else {
|
||||
system("cp $file /tmp/file");
|
||||
}
|
||||
|
||||
open(FILE, "/tmp/file");
|
||||
unlink("/tmp/file");
|
||||
unlink("/tmp/header");
|
||||
|
||||
$file =~ s/\.//;
|
||||
$fvar = $file;
|
||||
$fvar =~ s-/-_-g;
|
||||
$fvar =~ s-\.-_-g;
|
||||
|
||||
print(OUTPUT "static const unsigned char data".$fvar."[] = {\n");
|
||||
print(OUTPUT "\t/* $file */\n\t");
|
||||
for($j = 0; $j < length($file); $j++) {
|
||||
printf(OUTPUT "0x%02X, ", unpack("C", substr($file, $j, 1)));
|
||||
}
|
||||
printf(OUTPUT "0,\n");
|
||||
|
||||
|
||||
$i = 0;
|
||||
while(read(FILE, $data, 1)) {
|
||||
if($i == 0) {
|
||||
print(OUTPUT "\t");
|
||||
}
|
||||
printf(OUTPUT "0x%02X, ", unpack("C", $data));
|
||||
$i++;
|
||||
if($i == 10) {
|
||||
print(OUTPUT "\n");
|
||||
$i = 0;
|
||||
}
|
||||
}
|
||||
print(OUTPUT "};\n\n");
|
||||
close(FILE);
|
||||
push(@fvars, $fvar);
|
||||
push(@files, $file);
|
||||
}
|
||||
|
||||
for($i = 0; $i < @fvars; $i++) {
|
||||
$file = $files[$i];
|
||||
$fvar = $fvars[$i];
|
||||
|
||||
if($i == 0) {
|
||||
$prevfile = "NULL";
|
||||
} else {
|
||||
$prevfile = "file" . $fvars[$i - 1];
|
||||
}
|
||||
print(OUTPUT "const struct fsdata_file file".$fvar."[] = {{\n$prevfile,\ndata$fvar, ");
|
||||
print(OUTPUT "data$fvar + ". (length($file) + 1) .",\n");
|
||||
print(OUTPUT "sizeof(data$fvar) - ". (length($file) + 1) .",\n");
|
||||
print(OUTPUT $incHttpHeader."\n}};\n\n");
|
||||
}
|
||||
|
||||
print(OUTPUT "#define FS_ROOT file$fvars[$i - 1]\n\n");
|
||||
print(OUTPUT "#define FS_NUMFILES $i\n");
|
2
examples/http_server/fsdata/readme.txt
Normal file
2
examples/http_server/fsdata/readme.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
This directory contains a script ('makefsdata') to create C code suitable for
|
||||
httpd for given html pages (or other files) in a directory.
|
114
examples/http_server/http_server.c
Normal file
114
examples/http_server/http_server.c
Normal file
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* HTTP server example.
|
||||
*
|
||||
* This sample code is in the public domain.
|
||||
*/
|
||||
#include <espressif/esp_common.h>
|
||||
#include <esp8266.h>
|
||||
#include <esp/uart.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#include <ssid_config.h>
|
||||
#include <httpd/httpd.h>
|
||||
|
||||
#define LED_PIN 2
|
||||
|
||||
enum {
|
||||
SSI_UPTIME,
|
||||
SSI_FREE_HEAP,
|
||||
SSI_LED_STATE
|
||||
};
|
||||
|
||||
char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
|
||||
{
|
||||
for (int i = 0; i < iNumParams; i++) {
|
||||
if (strcmp(pcParam[i], "on") == 0) {
|
||||
uint8_t gpio_num = atoi(pcValue[i]);
|
||||
gpio_enable(gpio_num, GPIO_OUTPUT);
|
||||
gpio_write(gpio_num, true);
|
||||
} else if (strcmp(pcParam[i], "off") == 0) {
|
||||
uint8_t gpio_num = atoi(pcValue[i]);
|
||||
gpio_enable(gpio_num, GPIO_OUTPUT);
|
||||
gpio_write(gpio_num, false);
|
||||
} else if (strcmp(pcParam[i], "toggle") == 0) {
|
||||
uint8_t gpio_num = atoi(pcValue[i]);
|
||||
gpio_enable(gpio_num, GPIO_OUTPUT);
|
||||
gpio_toggle(gpio_num);
|
||||
}
|
||||
}
|
||||
return "/index.ssi";
|
||||
}
|
||||
|
||||
char *about_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
|
||||
{
|
||||
return "/about.html";
|
||||
}
|
||||
|
||||
int32_t ssi_handler(int32_t iIndex, char *pcInsert, int32_t iInsertLen)
|
||||
{
|
||||
switch (iIndex) {
|
||||
case SSI_UPTIME:
|
||||
snprintf(pcInsert, iInsertLen, "%d",
|
||||
xTaskGetTickCount() * portTICK_PERIOD_MS / 1000);
|
||||
break;
|
||||
case SSI_FREE_HEAP:
|
||||
snprintf(pcInsert, iInsertLen, "%d", (int) xPortGetFreeHeapSize());
|
||||
break;
|
||||
case SSI_LED_STATE:
|
||||
snprintf(pcInsert, iInsertLen, (GPIO.OUT & BIT(LED_PIN)) ? "Off" : "On");
|
||||
break;
|
||||
default:
|
||||
snprintf(pcInsert, iInsertLen, "N/A");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Tell the server how many characters to insert */
|
||||
return (strlen(pcInsert));
|
||||
}
|
||||
|
||||
void httpd_task(void *pvParameters)
|
||||
{
|
||||
tCGI pCGIs[] = {
|
||||
{"/gpio", (tCGIHandler) gpio_cgi_handler},
|
||||
{"/about", (tCGIHandler) about_cgi_handler},
|
||||
};
|
||||
|
||||
const char *pcConfigSSITags[] = {
|
||||
"uptime", // SSI_UPTIME
|
||||
"heap", // SSI_FREE_HEAP
|
||||
"led" // SSI_LED_STATE
|
||||
};
|
||||
|
||||
/* register handlers and start the server */
|
||||
http_set_cgi_handlers(pCGIs, sizeof (pCGIs) / sizeof (pCGIs[0]));
|
||||
http_set_ssi_handler((tSSIHandler) ssi_handler, pcConfigSSITags,
|
||||
sizeof (pcConfigSSITags) / sizeof (pcConfigSSITags[0]));
|
||||
httpd_init();
|
||||
|
||||
for (;;);
|
||||
}
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
uart_set_baud(0, 115200);
|
||||
printf("SDK version:%s\n", sdk_system_get_sdk_version());
|
||||
|
||||
struct sdk_station_config config = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PASS,
|
||||
};
|
||||
|
||||
/* required to call wifi_set_opmode before station_set_config */
|
||||
sdk_wifi_set_opmode(STATION_MODE);
|
||||
sdk_wifi_station_set_config(&config);
|
||||
sdk_wifi_station_connect();
|
||||
|
||||
/* turn off LED */
|
||||
gpio_enable(LED_PIN, GPIO_OUTPUT);
|
||||
gpio_write(LED_PIN, true);
|
||||
|
||||
/* initialize tasks */
|
||||
xTaskCreate(&httpd_task, "HTTP Daemon", 1024, NULL, 2, NULL);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue