මම ESP32 device එකකින් websocket හරහා browser එකට sensor data send කරනව. මට ඔනි මේ reading ටික කෙලින්ම Winform app එකකට යවන්න මෙක කරන්නෙ කොහොමද.
මේක තමයි දැනට කරන code එක.
//============================
#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer.h>
#include <driver/i2s.h>
const i2s_port_t I2S_PORT = I2S_NUM_0;
const char* ssid = "SSID";
const char* password = "PWD";
WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
String JSONtxt;
#include "webpage.h"
void handleRoot()
{
server.send(200,"text/html", webpageCont);
}
void setup()
{
Serial.begin(115200);
const i2s_config_t i2s_config = {
// configuration
};
const i2s_pin_config_t pin_config = {
//pin configuration
}
server.on("/", handleRoot);
server.begin(); webSocket.begin();
void loop()
{
webSocket.loop(); server.handleClient();
int32_t sample = 0;
int bytes_read = i2s_pop_sample(I2S_PORT, (char *)&sample, portMAX_DELAY); // no timeout
if (bytes_read > 0) {
String sampleString = String(sample);
JSONtxt = "{\"sample\":\""+sampleString+"\"}";
webSocket.broadcastTXT(JSONtxt);
//============================
webpage.h
//============================
const char webpageCont[] PROGMEM =
R"=====(
<!DOCTYPE HTML>
<html>
<title>ESP32 WebSocket Server</title>
<!---------------------------CSS-------------------------->
<style>
body {background-color:rgba(128,128,128,0.322); font-family:calibri}
h1 {font-size: 40px; color: black; text-align: center}
h2 {font-size: 30px; color: blue}
div.h1 {background-color: whitesmoke;}
</style>
<!--------------------------HTML-------------------------->
<body>
<h1><div class="h1">ESP32 WebSocket Server</div></h1>
<h2>
Level: <span style="color:rgb(216, 3, 3)" id="sample"></span>
</h2>
</body>
<!----------------------JavaScript------------------------>
<script>
InitWebSocket()
function InitWebSocket()
{
websock = new WebSocket('ws://'+window.location.hostname+':81/');
websock.onmessage=function(evt)
{
JSONobj = JSON.parse(evt.data);
document.getElementById('sample').innerHTML = JSONobj.sample;
var sample = parseInt(JSONobj.sample);
}
}
</script>
</html>
)=====";
bump
------ Post added on Feb 9, 2021 at 7:54 AM
මේක තමයි දැනට කරන code එක.
//============================
#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer.h>
#include <driver/i2s.h>
const i2s_port_t I2S_PORT = I2S_NUM_0;
const char* ssid = "SSID";
const char* password = "PWD";
WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
String JSONtxt;
#include "webpage.h"
void handleRoot()
{
server.send(200,"text/html", webpageCont);
}
void setup()
{
Serial.begin(115200);
const i2s_config_t i2s_config = {
// configuration
};
const i2s_pin_config_t pin_config = {
//pin configuration
}
server.on("/", handleRoot);
server.begin(); webSocket.begin();
void loop()
{
webSocket.loop(); server.handleClient();
int32_t sample = 0;
int bytes_read = i2s_pop_sample(I2S_PORT, (char *)&sample, portMAX_DELAY); // no timeout
if (bytes_read > 0) {
String sampleString = String(sample);
JSONtxt = "{\"sample\":\""+sampleString+"\"}";
webSocket.broadcastTXT(JSONtxt);
//============================
webpage.h
//============================
const char webpageCont[] PROGMEM =
R"=====(
<!DOCTYPE HTML>
<html>
<title>ESP32 WebSocket Server</title>
<!---------------------------CSS-------------------------->
<style>
body {background-color:rgba(128,128,128,0.322); font-family:calibri}
h1 {font-size: 40px; color: black; text-align: center}
h2 {font-size: 30px; color: blue}
div.h1 {background-color: whitesmoke;}
</style>
<!--------------------------HTML-------------------------->
<body>
<h1><div class="h1">ESP32 WebSocket Server</div></h1>
<h2>
Level: <span style="color:rgb(216, 3, 3)" id="sample"></span>
</h2>
</body>
<!----------------------JavaScript------------------------>
<script>
InitWebSocket()
function InitWebSocket()
{
websock = new WebSocket('ws://'+window.location.hostname+':81/');
websock.onmessage=function(evt)
{
JSONobj = JSON.parse(evt.data);
document.getElementById('sample').innerHTML = JSONobj.sample;
var sample = parseInt(JSONobj.sample);
}
}
</script>
</html>
)=====";
bump
------ Post added on Feb 9, 2021 at 7:54 AM