parent
ebfbc1144c
commit
f64935eb1d
19 changed files with 5050 additions and 0 deletions
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