Skip to content

Commit

Permalink
Legacy Pins mappings for D1 mini
Browse files Browse the repository at this point in the history
  • Loading branch information
soylentOrange committed Oct 11, 2024
1 parent 399567f commit 6ca2812
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
14 changes: 7 additions & 7 deletions include/localmodbus.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#ifndef LOCALMODBUS_H
#define LOCALMODBUS_H
#define LOCALMODBUS_H

#include <WiFiManager.h>
#include <ModbusBridgeWiFi.h>
#include <ModbusClientRTU.h>
#include <Update.h>
#include "config.h"
#include <WiFiManager.h>
#include <ModbusBridgeWiFi.h>
#include <ModbusClientRTU.h>
#include <Update.h>
#include "config.h"

// sub-function codes for FC08 (DIAGNOSTICS_SERIAL)
enum SubFunctionCode : uint16_t {
RETURN_QUERY_DATA = 0x00,
RESTART_COMMUNICATION_OPTION = 0x01,
Expand All @@ -15,7 +16,6 @@ enum SubFunctionCode : uint16_t {
RETURN_BUS_MESSAGE_COUNT = 0x0b,
RETURN_BUS_COMMUNICATION_ERROR_COUNT = 0x0c,
RETURN_SLAVE_MESSAGE_COUNT = 0x0e,

};

void setupLocalModbus(uint8_t serverID, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm);
Expand Down
48 changes: 48 additions & 0 deletions include/pins_d1_mini.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef PINS_D1MINI_H
#define PINS_D1MINI_H

#include <stdint.h>

// Legacy Pins mappings for compatibility with D1 mini
// D0..D8
#ifndef D0
#define D0 26
#endif

#ifndef D1
#define D1 22
#endif

#ifndef D2
#define D2 21
#endif

#ifndef D3
#define D3 17
#endif

#ifndef D4
#define D4 16
#endif

#ifndef D5
#define D5 18
#endif

#ifndef D6
#define D6 19
#endif

#ifndef D7
#define D7 23
#endif

#ifndef D8
#define D8 15
#endif

#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif

#endif /* PINS_D1MINI_H */
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
upload_port = COM9
monitor_port = COM9

[env:d1mini]
board = wemos_d1_mini32
[env:mhetesp32minikit]
board = mhetesp32minikit
build_flags = ${env.build_flags}
6 changes: 3 additions & 3 deletions src/localmodbus.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "localmodbus.h"
#include <esp_task_wdt.h>
#include <pins_arduino.h>
#include "pins_d1_mini.h"
#define ETAG "\"" __DATE__ "" __TIME__ "\""

static ModbusClientRTU* _rtu = NULL;
Expand Down Expand Up @@ -43,7 +44,7 @@ ModbusMessage FC03(ModbusMessage request) {
ModbusMessage FC08(ModbusMessage request) {
uint16_t subFunctionCode; // Sub-function code
ModbusMessage response; // response message to be sent back
uint16_t resultWord = 0;
uint16_t resultWord = 0; // response data to be sent back

LOG_D("Worker for FC08\n");

Expand Down Expand Up @@ -109,5 +110,4 @@ void setupLocalModbus(uint8_t serverID, ModbusClientRTU *rtu, ModbusBridgeWiFi *
_bridge = bridge;
bridge->registerWorker(serverID, READ_HOLD_REGISTER, &FC03);
bridge->registerWorker(serverID, DIAGNOSTICS_SERIAL, &FC08);
//bridge->registerWorker(serverID, REPORT_SERVER_ID_SERIAL, &FC08);
}
18 changes: 2 additions & 16 deletions src/pages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,13 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
response->print("</pre>");
auto error = answer.getError();
if (error == SUCCESS){
auto count = answer.size() - 2;
auto count = answer.size() - 4;
if(count < 0) {
count = 0;
}
response->print("<span >Answer: 0x");
for (size_t i = 0; i < count; i++) {
response->printf("%02x", answer[i + 2]);
response->printf("%02x", answer[i + 4]);
}
response->print("</span>");
}
Expand All @@ -533,20 +533,6 @@ void setupPages(AsyncWebServer *server, ModbusClientRTU *rtu, ModbusBridgeWiFi *
sendButton(response, "Back", "debug");
sendResponseTrailer(response);
request->send(response);

// // reset when requested
// if(config->getLocalModbusEnable()) {
// if(slaveId.toInt() == config->getLocalModbusAddress()) {
// if(answer.getFunctionCode() == DIAGNOSTICS_SERIAL) {
// uint16_t subFunctionCode; // Sub-function code
// answer.get(2, subFunctionCode);
// if(subFunctionCode == RESTART_COMMUNICATION_OPTION) {
// dbgln("[webserver] rebooting...");
// ESP.restart();
// }
// }
// }
// }
});

server->on("/update", HTTP_GET, [](AsyncWebServerRequest *request){
Expand Down

0 comments on commit 6ca2812

Please sign in to comment.