gemi254 / ControlAssist-ESP32-ESP8266

Generate webserver pages on esp devices and control their html elements at runtime using web sockets.
MIT License
8 stars 1 forks source link
arduino controller esp32 esp8266 html http-server javascript multiclient page-generator runtime webpage webserver websocket-client websocket-server websockets

ControlAssist

Multi-platform library for controlling html elements in a esp webpages at runtime using web sockets.

Description

A library allowing linking html elements to sketch variables on pages hosted on esp32/esp8266 devices. It uses a web socket server on the esp device and a JavaScript web socket client implementation on the web page allowing bi-directional real-time communication between device and page.

In a typical webpage, html elements like input box, textbox, range, checkbox can be binded with ControlAssist internal variables using their unique html ids in order to associate their values. A vectors list will be generated to hold all associated element keys and their values.

How it works

Every time an element is changing its value in the web page, a web socket message will be send to server and ControlAssist will update its internal value. Also if you change a ControlAssist value inside your sketch, a message will be automatically send to client and the value of the associated html element will be updated.

ControlAssist will automatically add JavaScript onChange handlers to the web page html code, so the binded elements will transmit their changes automatically to the server. It will also add JavaScript code to handle incoming web sockets messages so the values of the binded html elements can be updated.

ESP device will transmit changes to all connected web socket clients. This will make possible to have one ESP device and multiple Web pages opened from different places inside the network, receiving and sending changes.

Features

How to use

Define yours internal page html code sections at Program mem.

PROGMEM const char HTML_HEADERS[] = R"=====(<!DOCTYPE HTML>)=====";
PROGMEM const char HTML_BODY[] = R"=====(<body></body>)=====";
PROGMEM const char HTML_FOOTER[] = R"=====(</htmll>)=====";

You can also upload page html code sections to spiffs as a html files

ControlAssist init functions

Define and initialize you class

ControlAssist control functions

Controlling your elements inside you loop function

JavaScript handlers inside your webpage

A JavaScript event wsChange will be automatically send to each html element when the esp device changes it's value. You can add a JavaScript event listener on this event at your web page. So it will be possible to perform custom tasks when elements value is updated by web sockets.

html_id.addEventListener("wsChange", (event) => {
    //Get the changed value
    value = event.target.value
    event.preventDefault();
    return false;
});

See example ControlAssist-Gauge.ino

Logging and debugging with log level

In you application you use LOG_E, LOG_W, LOG_I, LOG_D macros instead of Serial.prinf to print your messages. ControlAssist displays these messages with timestamps

You can define log level for each module #define LOGGER_LOG_LEVEL 4

#define _LOG_LEVEL_NONE      (0)
#define _LOG_LEVEL_ERROR     (1)
#define _LOG_LEVEL_WARN      (2)
#define _LOG_LEVEL_INFO      (3)
#define _LOG_LEVEL_DEBUG     (4)
#define _LOG_LEVEL_VERBOSE   (5)

Compile

Download library files and place them on ./libraries directory under ArduinoProjects Then include the ControlAssist.h in your application and compile..

If you get compilation errors on arduino-esp32 you need to update your arduino-esp32 library in the IDE using Boards Manager