#include <WiFi.h>
#include <WebSocketsServer.h>
WiFiServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
const char *ssid = "Alen's camera"; //Enter your WIFI ssid
const char *password = "alen2011";
String index_html = R"=====(
<html>
<head>
<title> Alen's camera streaming</title>\n \
<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script>\n \
</head>
<body>
<div id='output'></div>
</body>
<script>
jQuery(function($){
if (!('WebSocket' in window)) {
alert('Your browser does not support web sockets');
}else{
setup();
}
function setup(){
var host = 'ws://server_ip:81';
var socket = new WebSocket(host);
if(socket){
socket.onopen = function(){
}
socket.onmessage = function(msg){
showServerResponse(msg.data);
}
socket.onclose = function(){
showServerResponse('The connection has been closed.');
}
}
function showServerResponse(txt){
document.getElementById('output').innerHTML = txt;
}
}
});
</script>
</html>
)=====";
void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) {
const uint8_t* src = (const uint8_t*) mem;
Serial.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len);
for (uint32_t i = 0; i < len; i++) {
if (i % cols == 0) {
Serial.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i);
}
Serial.printf("%02X ", *src);
src++;
}
Serial.printf("\n");
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", num);
break;
case WStype_CONNECTED:
{
int counter = 0;
while (true) {
counter++;
String n = String(counter);
webSocket.sendTXT(num, n);
delay(1000);
}
}
break;
case WStype_TEXT:
case WStype_BIN:
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
break;
}
}
void setup() {
Serial.begin(115200);
WiFi.begin("", "xxx");
Serial.println("");
Serial.print("IP address: " + IP);
index_html.replace("server_ip", IP);
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void handle() {
WiFiClient client = server.available();
if (client.connected() && client.available()) {
client.flush();
client.print(index_html);
client.stop();
}
}
void loop(){
handle();
webSocket.loop();
}