-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.c
147 lines (124 loc) · 3.94 KB
/
http_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#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>
extern float temperature;
enum {
SSI_UPTIME,
SSI_FREE_HEAP,
SSI_TEMPERATURE
};
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;
default:
snprintf(pcInsert, iInsertLen, "N/A");
break;
}
/* Tell the server how many characters to insert */
return (strlen(pcInsert));
}
const char *websocket_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
return "/temperature.html";
}
void websocket_task(void *pvParameter)
{
struct tcp_pcb *pcb = (struct tcp_pcb *) pvParameter;
for (;;) {
if (pcb == NULL || pcb->state != ESTABLISHED) {
printf("Connection closed, deleting task\n");
break;
}
int uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
int heap = (int) xPortGetFreeHeapSize();
/* Generate response in JSON format */
char response[64];
int len = snprintf(response, sizeof (response),
"{\"uptime\" : \"%d\","
" \"temperature\" : \"%.2f\","
" \"heap\" : \"%d\"}", uptime, temperature, heap);
if (len < sizeof (response))
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
/**
* This function is called when websocket frame is received.
*
* Note: this function is executed on TCP thread and should return as soon
* as possible.
*/
void websocket_cb(struct tcp_pcb *pcb, uint8_t *data, u16_t data_len, uint8_t mode)
{
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char*) data);
uint8_t response[2];
uint16_t val;
switch (data[0]) {
case 'A': // ADC
/* This should be done on a separate thread in 'real' applications */
val = temperature;
break;
default:
printf("Unknown command\n");
val = 0;
break;
}
response[1] = (uint8_t) val;
response[0] = val >> 8;
websocket_write(pcb, response, 2, WS_BIN_MODE);
}
/**
* This function is called when new websocket is open and
* creates a new websocket_task if requested URI equals '/stream'.
*/
void websocket_open_cb(struct tcp_pcb *pcb, const char *uri)
{
printf("WS URI: %s\n", uri);
if (!strcmp(uri, "/stream")) {
printf("request for streaming\n");
xTaskCreate(&websocket_task, "websocket_task", 256, (void *) pcb, 2, NULL);
}
}
void httpd_task(void *pvParameters)
{
tCGI pCGIs[] = {
{"/temperature", (tCGIHandler) websocket_cgi_handler},
};
const char *pcConfigSSITags[] = {
"uptime", // SSI_UPTIME
"heap", // SSI_FREE_HEAP
"temperature", // SSI_TEMPERATURE
};
/* 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]));
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb,
(tWsHandler) websocket_cb);
httpd_init();
for(;;);
}
void http_init() {
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();
}