wemosでwebsocket その2

# 概要
wemosでwebsocket、やってみた。
練習問題、やってみた。

# 練習問題
websocket serverで、俺言語インタープリターを実行せよ。

# 方針
- IPアドレス、192.168.0.104
- ポート、50002

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/9e9b2be5-2c3e-5f03-a0ec-5849db60f2b4.png)

# サンプルコード

```
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "WebSocketsServer.h"

const char * ssid = "****";
const char * password = "****";

ESP8266WebServer server;
WebSocketsServer webSocket = WebSocketsServer(50002);

char webpage[] PROGMEM = R"=====(
<html>
<head>
	<script>
		var Socket;
		function init() {
			Socket = new WebSocket('ws://' + window.location.hostname + ':50002/');
			Socket.onmessage = function(event) {
				document.getElementById("rxConsole").value = event.data;
			}
		}
		function sendText() {
			Socket.send(document.getElementById("txBar").value);
			document.getElementById("txBar").value = "";
		}
	</script>
</head>
<body onload="javascript:init()">
	<center>
	<h1>俺言語</h1>
	<div>
		<input type="text" placeholder="10 a=0" id="txBar" onkeydown="if (event.keyCode == 13) sendText();" />
	</div>
	<br>
	<div>
		<textarea id="rxConsole" rows="13" cols="50"></textarea>
	</div>
	</center>
</body>
</html>
)=====";

char ram[400];
int Pcc = 257;
int Rmd = 258;
int Bnd = 259;
int Lbf = 270;
int Obj = 0;
int Ptr = Lbf;
int Cur;
int Vc;
int Adr;
int Adr2;
char Val;
int i;
const int led = LED_BUILTIN;
String out = "";

char RD(int ado) {
	char v = ram[ado];
	return v;
}
void WR(int ado, char data) {
	ram[ado] = data;
}
void putchr(char c) {
	out += String(c);
}
void putint(int c) {
	out += String(c);
}
void putstr(const char * s) {
	while (* s)
		putchr(* s++);
}
void crlf() {
	putchr('\r');
	putchr('\n');
}
int putl(int pt, char d) {
	while (RD(pt) != d)
	{
		putchr(RD(pt++));
	}
	return pt;
}
void putlp(int d) {
	while (RD(Ptr) != d)
	{
		putchr(RD(Ptr++));
	}
}
boolean num() {
	if (48 <= RD(Ptr) && RD(Ptr) <= 57)
		return true;
	else
		return false;
}
boolean getnm2() {
	char ch;
	if (!num())
		return false;
	int n = 0;
	do
	{
		n *= 10;
		ch = RD(Ptr++);
		n += ch - 48;
	} while (num());
	Val = n;
	return true;
}
int nxtln(int pt) {
	for (pt += 1; RD(pt++) != '\0';)
		;
	return pt;
}
boolean fndln() {
	for (Cur = Obj; Cur != RD(Bnd); Cur = nxtln(Cur))
	{
		if (RD(Cur) == RD(Pcc))
			return true;
	}
	return false;
}
boolean nxline() {
	for (Cur = Obj; Cur != RD(Bnd); Cur = nxtln(Cur))
	{
		if (RD(Cur) > RD(Pcc))
			return true;
	}
	return false;
}
void getvr() {
	Vc = RD(Ptr);
	if (Vc == 'a')
		Adr = 260;
	else if (Vc == '#')
		Adr = Pcc;
	else if (Vc == 'b')
		Adr = 261;
	else if (Vc == 'c')
		Adr = 262;
	else if (Vc == 'd')
		Adr = 263;
	else if (Vc == 'e')
		Adr = 264;
	else if (Vc == 'f')
		Adr = 265;
	else if (Vc == '%')
		Adr = Rmd;
	else
		Adr = 260;
}
void getvr2() {
	char cc = RD(Ptr);
	if (cc == 'a')
		Adr2 = 260;
	else if (cc == '#')
		Adr2 = Pcc;
	else if (cc == 'b')
		Adr2 = 261;
	else if (cc == 'c')
		Adr2 = 262;
	else if (cc == 'd')
		Adr2 = 263;
	else if (cc == 'e')
		Adr2 = 264;
	else if (cc == 'f')
		Adr2 = 265;
	else if (cc == '%')
		Adr2 = Rmd;
	else
		Adr2 = 260;
}
void term() {
	char cc;
	cc = RD(Ptr++);
	char Val2 = Val;
	factr();
	if (cc == '*')
	{
		Val = Val2 * Val;
	}
	else if (cc == '+')
	{
		Val = Val2 + Val;
	}
	else if (cc == '-')
	{
		Val = Val2 - Val;
	}
	else if (cc == '/')
	{
		WR(Rmd, Val2 % Val);
		Val = Val2 / Val;
	}
	else if (cc == '=')
	{
		if (Val2 == Val)
			Val = 1;
		else
			Val = 0;
	}
	else if (cc == '>')
	{
		if (Val2 >= Val)
			Val = 1;
		else
			Val = 0;
	}
	else
	{
		if (Val2 < Val)
			Val = 1;
		else
			Val = 0;
	}
}
void factr() {
	char cc;
	if (RD(Ptr) == '\0')
	{
		Val = 0;
		return;
	}
	if (getnm2())
		return;
	cc = RD(Ptr++);
	if (cc == '?')
	{
	}
	else if (cc == '$')
	{
	}
	else if (cc == '(')
	{
	}
	else
	{
		Ptr--;
		getvr2();
		Val = RD(Adr2);
		Ptr++;
	}
}
void expr() {
	char cc;
	factr();
	while ((cc = RD(Ptr)) != '\0' && cc != ')')
	{
		term();
	}
	Ptr++;
}
void ordr() {//siki hyouka
	getvr();//hidari
	Ptr++;
	Ptr++;
	if (RD(Ptr) == '"')
	{
		Ptr++;
		putlp('"');
	}
	else
	{
		expr();//migi
		if (Vc == '$')
		{
		}
		else if (Vc == '?')//print
		{
			putint(Val);
			putchr('\t');
		}
		else if (Vc == 'l')//led
		{
			digitalWrite(led, Val);
		}
		else if (Vc == '"')//delay
		{
			delay(Val);
		}
		else
		{
			WR(Adr, Val);
		}
	}
}
void run() {
	int ima = 0;
	Ptr = Lbf;
	if (!getnm2())
	{
		Ptr = Lbf;
		WR(Pcc, 0);
		for ( ; ; )
		{
			ima = RD(Pcc);
			ordr();
			if (RD(Pcc) == 0 && ima == 0)
			{
				break;
			}
			else
			{
				if (RD(Pcc) == 0)
					WR(Pcc, ima);
				if (RD(Pcc) == 1)
					ima = 1;
				if (RD(Pcc) == ima)
				{
					if (nxline())
					{
						WR(Pcc, RD(Cur));
						Ptr = Cur + 2;
					}
					else
					{
						break;
					}
				}
				else
				{
					if (fndln())
					{
						WR(Pcc, RD(Cur));
						Ptr = Cur + 2;
					}
					else
					{
						break;
					}
				}
			}
		}
	}
	else
	{
		if (Val == 0) //list
		{
			int pt;
			for (pt = Obj; pt < RD(Bnd); pt++)
			{
				putint(RD(pt));
				pt += 1;
				pt = putl(pt, '\0');
				crlf();
			}
		}
		else
		{
			int src;
			int dst;
			int m;
			int h;
			int f = 0;
			WR(Pcc, Val);
			if (fndln())
			{
				src = nxtln(Cur);
				for (dst = Cur; src != RD(Bnd); dst++, src++)
				{
					WR(dst, RD(src));
				}
				WR(Bnd, dst);
			}
			if (nxline())
			{
				f = 1;
			}
			src = RD(Bnd);
			for (m = 2, h = Ptr; RD(h) != '\0'; m++)
			{
				h++;
			}
			if (m > 2)
			{
				WR(Bnd, src + m);
				if (f == 1)
				{
					Cur--;
					for (dst = RD(Bnd); src != Cur; dst--, src--)
					{
						WR(dst, RD(src));
					}
					src++;
				}
				WR(src, Val);
				src += 1;
				for (i = 1; i < m; i++)
				{
					WR(src++, RD(Ptr++));
				}
			}
		}
	}
	for (i = 0; i < 129; i++)
	{
		WR(i + Lbf, '\0');
	}
	Ptr = Lbf;
	putstr("OK\r\n");
}
void onLine(String line) {
	int len;
	int i;
	char c[130];
	len = line.length();
	line.toCharArray(c, len + 1);
	for (i = 0; i < len; i++)
	{
		char ch = c[i];
		putchr(ch);
		WR(Ptr++, ch);
	}
	putchr('\n');
	WR(Ptr++, '\0');
	run();
}

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
	if (type == WStype_TEXT)
	{
		//for (int i = 0; i < length; i++)
		//	Serial.print((char) payload[i]);
		//Serial.println();
		out = "";
		String text = (char *) payload;
		Serial.println(text);
		onLine(text);
		webSocket.broadcastTXT(out);
	}
}
void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
	digitalWrite(LED_BUILTIN, LOW);
	WiFi.begin(ssid, password);
	Serial.begin(115200);
	while (WiFi.status() != WL_CONNECTED)
	{
		Serial.print(".");
		delay(500);
	}
	Serial.println("");
	Serial.print("IP Address: ");
	Serial.println(WiFi.localIP());
	server.on("/", []() {
		server.send_P(200, "text/html", webpage);
	});
	server.begin();
	webSocket.begin();
	webSocket.onEvent(webSocketEvent);
	Serial.println("ore server started");
	for (i = 0; i < 400; i++)
	{
		WR(i, '\0');
	}
	WR(Bnd, 0);
	putstr("\r\nore server v1.0\r\n");
}
void loop() {
	webSocket.loop();
	server.handleClient();
}




```

以上。