ESP-8266-WebSocket
所属分类:wifi
开发工具:C
文件大小:0KB
下载次数:2
上传日期:2015-09-23 08:58:10
上 传 者:
sh-1993
说明: ESP 8266 WiFi模块的WebSocket服务器,
(A WebSocket Server for the ESP 8266 WiFi Module,)
文件列表:
LICENCE (1085, 2015-09-23)
Makefile (4388, 2015-09-23)
include/ (0, 2015-09-23)
include/espmissingincludes.h (2091, 2015-09-23)
include/httpdconfig.h (261, 2015-09-23)
include/lwipopts.h (55063, 2015-09-23)
include/mem_manager.h (2436, 2015-09-23)
include/uart_hw.h (5842, 2015-09-23)
include/user_config.h (1, 2015-09-23)
user/ (0, 2015-09-23)
user/base64.c (3516, 2015-09-23)
user/base64.h (171, 2015-09-23)
user/sha1.c (4204, 2015-09-23)
user/sha1.h (644, 2015-09-23)
user/uart.c (5734, 2015-09-23)
user/uart.h (2170, 2015-09-23)
user/uart_register.h (4752, 2015-09-23)
user/user_main.c (1808, 2015-09-23)
user/websocketd.c (7214, 2015-09-23)
user/websocketd.h (3296, 2015-09-23)
Websocket Daemon for the ESP 8266
=================================
This is a Websocket implementation based upon the [RFC 6455](https://tools.ietf.org/html/rfc6455).
The Library uses a stripped Version of the [esphttpd](http://git.spritesserver.nl/esphttpd.git/) as a base. Even though the Websocket Implementation is fully independent from this code, I used this Project as a starting point. So thanks to the author os this project.
The code implements a WebSocket Deamon for the ESP8266 WiFi Module. It supports:
* listening for new Clients
* doing the handshake
* Sending and receiving messages
* as plain text
* as binary
* message unmasking
* creating arbitary message frames
* callbacks for received messages
ToDo:
* connect to servers
* send as client
There are no direct dependencies, as a SHA1 and a Base64 implementation are part of the code.
Sample server that just echoes ws frames on the UART (serial)
-------------------------------------------------------------
```c
void onWsMessage(WSConnection *connection, const WSFrame *message) {
for (int i = 0; i < message->payloadLength; i++) {
stdoutPutchar(message->payloadData[i]);
}
stdoutPutchar('\n');
}
void onWsConnection(WSConnection *connection) {
connection->onMessage = &onWsMessage;
}
//Main routine. Initialize stdout, the I/O and the webserver and we're done.
void user_init(void) {
uart_init();
ioInit();
websocketdInit(8080, &onWsConnection);
//Start os task
system_os_task(rxLoop, RX_PRIO, user_procTaskQueue, RX_QUEUE_LEN);
system_os_post(RX_PRIO, 0, 0 );
os_printf("\nReady\n");
}
```
sample server that echoes the serial data (bidirectional)
---------------------------------------------------------
```c
#include "espmissingincludes.h"
#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"
#include "uart.h"
#include "websocketd.h"
#define RX_PRIO 0
#define RX_QUEUE_LEN 1
#define COMMAND_MAXLEN 1024
os_event_t user_procTaskQueue[RX_QUEUE_LEN];
static char *statusCommand = "STATUS:";
static uint8_t commandMatcher = 0;
static uint8_t commandMatched = FALSE;
static char command[COMMAND_MAXLEN];
static uint32_t commandPosition = 0;
//Main code function
static void ICACHE_FLASH_ATTR rxLoop(os_event_t *events) {
int c = uart0_rx_one_char();
if (c != -1) {
if (commandMatched == TRUE) {
if (c == '\n' || c == '\r') {
broadcastWsMessage(command, commandPosition, FLAG_FIN | OPCODE_TEXT);
commandMatched = FALSE;
commandPosition = 0;
commandMatcher = 0;
}else{
command[commandPosition++] = c;
}
if (commandPosition == COMMAND_MAXLEN) {
commandMatched = FALSE;
commandPosition = 0;
commandMatcher = 0;
}
} else {
if (statusCommand[commandMatcher] == c) {
commandMatcher++;
if (commandMatcher == strlen(statusCommand)) {
commandMatched = TRUE;
}
} else {
commandMatcher = 0;
}
}
}
system_os_post(RX_PRIO, 0, 0 );
}
void onWsMessage(WSConnection *connection, const WSFrame *message) {
for (int i = 0; i < message->payloadLength; i++) {
stdoutPutchar(message->payloadData[i]);
}
stdoutPutchar('\n');
}
void onWsConnection(WSConnection *connection) {
connection->onMessage = &onWsMessage;
}
//Main routine. Initialize stdout, the I/O and the webserver and we're done.
void user_init(void) {
uart_init();
ioInit();
websocketdInit(8080, &onWsConnection);
//Start os task
system_os_task(rxLoop, RX_PRIO, user_procTaskQueue, RX_QUEUE_LEN);
system_os_post(RX_PRIO, 0, 0 );
os_printf("\nReady\n");
}
```
For Build and upload instructions please refer to the `README.esphttpd` of the esphttpd author.
近期下载者:
相关文件:
收藏者: