Skip to content

Commit 8e65a4e

Browse files
committed
add modbus debug log to debug page
1 parent 06c9505 commit 8e65a4e

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

include/debug.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef DEBUG_H
2+
#define DEBUG_H
3+
4+
#include <Arduino.h>
5+
#include <ESPAsyncWebServer.h>
6+
class WebPrint:public Print{
7+
private:
8+
Print *_serial;
9+
AsyncResponseStream *_response;
10+
public:
11+
WebPrint(Print *serial, AsyncResponseStream *reponse);
12+
size_t write(uint8_t) override;
13+
size_t write(const uint8_t *buffer, size_t size) override;
14+
};
15+
16+
#endif

include/pages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ModbusClientRTU.h>
88
#include <Update.h>
99
#include "config.h"
10+
#include "debug.h"
1011

1112
void setupPages(AsyncWebServer* server, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm);
1213
void sendResponseHeader(AsyncResponseStream *response, const char *title);

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ lib_deps =
1616
https://github.com/tzapu/WiFiManager.git
1717
https://github.com/me-no-dev/ESPAsyncWebServer.git
1818
eModbus
19-
;build_flags = -DLOG_LEVEL=LOG_LEVEL_DEBUG
19+
build_flags = -DLOG_LEVEL=LOG_LEVEL_DEBUG
2020
monitor_speed = 115200

src/debug.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "debug.h"
2+
3+
WebPrint::WebPrint(Print *serial, AsyncResponseStream *response)
4+
:_serial(serial)
5+
,_response(response)
6+
{}
7+
8+
size_t WebPrint::write(uint8_t arg){
9+
_response->print("(char)arg");
10+
return _serial->write(arg);
11+
}
12+
13+
size_t WebPrint::write(const uint8_t *buffer, size_t size){
14+
_response->print((const char*)buffer);
15+
return _serial->write(buffer, size);
16+
}

src/pages.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,15 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
233233
if (request->hasParam("func", true)){
234234
func = request->getParam("func", true)->value();
235235
}
236-
ModbusMessage answer = rtu->syncRequest(0xdeadbeef, slaveId.toInt(), func.toInt(), reg.toInt(), 1);
237236
auto *response = request->beginResponseStream("text/html");
238237
sendResponseHeader(response, "Debug");
238+
response->print("<pre>");
239+
auto previous = LOGDEVICE;
240+
auto debug = WebPrint(previous, response);
241+
LOGDEVICE = &debug;
242+
ModbusMessage answer = rtu->syncRequest(0xdeadbeef, slaveId.toInt(), func.toInt(), reg.toInt(), 1);
243+
LOGDEVICE = previous;
244+
response->print("</pre>");
239245
if (answer.getError() == SUCCESS){
240246
auto count = answer[2];
241247
response->print("<span >Answer: 0x");
@@ -375,6 +381,9 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
375381
".e{"
376382
"color:red;"
377383
"}"
384+
"pre{"
385+
"text-align:left;"
386+
"}"
378387
);
379388
response->addHeader("ETag", __DATE__ "" __TIME__);
380389
request->send(response);

0 commit comments

Comments
 (0)