forked from zivillian/esp32-modbus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from soylentOrange/rest-api
Added Rest Api
- Loading branch information
Showing
9 changed files
with
740 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef LOCALMODBUS_H | ||
#define LOCALMODBUS_H | ||
|
||
#include <WiFiManager.h> | ||
#include <ModbusBridgeWiFi.h> | ||
#include <Update.h> | ||
#include "config.h" | ||
|
||
enum SubFunctionCode : uint16_t { | ||
RETURN_QUERY_DATA = 0x00, | ||
RESTART_COMMUNICATION_OPTION = 0x01, | ||
}; | ||
|
||
void setupLocalModbus(uint8_t serverID, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm); | ||
#endif /* LOCALMODBUS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef RESTAPI_H | ||
#define RESTAPI_H | ||
|
||
#include <WiFiManager.h> | ||
#include <ESPAsyncWebServer.h> | ||
#include <ModbusBridgeWiFi.h> | ||
#include <ModbusClientRTU.h> | ||
#include <Update.h> | ||
#include "config.h" | ||
#include "debug.h" | ||
|
||
void setupRestApi(AsyncWebServer* server, ModbusClientRTU *rtu, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm); | ||
#endif /* RESTAPI_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "localmodbus.h" | ||
#define ETAG "\"" __DATE__ "" __TIME__ "\"" | ||
|
||
// FC03: worker do serve Modbus function code 0x03 (READ_HOLD_REGISTER) | ||
ModbusMessage FC03(ModbusMessage request) { | ||
uint16_t address; // requested register address | ||
uint16_t words; // requested number of registers | ||
ModbusMessage response; // response message to be sent back | ||
|
||
LOG_D("WORKER CALLED FC03"); | ||
|
||
// get request values | ||
request.get(2, address); | ||
request.get(4, words); | ||
|
||
// Address and words valid? We assume 10 registers here for demo | ||
if (address && words && (address + words) <= 10) { | ||
// Looks okay. Set up message with serverID, FC and length of data | ||
response.add(request.getServerID(), request.getFunctionCode(), (uint8_t)(words * 2)); | ||
// Fill response with requested data | ||
for (uint16_t i = address; i < address + words; ++i) { | ||
response.add(i); | ||
} | ||
} else { | ||
// No, either address or words are outside the limits. Set up error response. | ||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_ADDRESS); | ||
} | ||
return response; | ||
} | ||
|
||
// FC08: worker do serve Modbus function code 0x08 (DIAGNOSTICS_SERIAL) | ||
ModbusMessage FC08(ModbusMessage request) { | ||
uint16_t subFunctionCode; // Sub-function code | ||
ModbusMessage response; // response message to be sent back | ||
|
||
LOG_D("WORKER CALLED FC08"); | ||
|
||
// get request values | ||
request.get(2, subFunctionCode); | ||
|
||
switch(subFunctionCode) { | ||
case RETURN_QUERY_DATA: | ||
LOG_D("Return Query Data"); | ||
response = request; | ||
break; | ||
case RESTART_COMMUNICATION_OPTION: | ||
LOG_D("Restart Communications Option"); | ||
response = request; | ||
break; | ||
default: | ||
LOG_D("default: ILLEGAL_FUNCTION"); | ||
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_FUNCTION); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
void setupLocalModbus(uint8_t serverID, ModbusBridgeWiFi *bridge, Config *config, WiFiManager *wm) { | ||
String EfuseMac = String(ESP.getEfuseMac(), 16); | ||
dbgln(EfuseMac); | ||
bridge->registerWorker(serverID, READ_HOLD_REGISTER, &FC03); | ||
bridge->registerWorker(serverID, DIAGNOSTICS_SERIAL, &FC08); | ||
//bridge->registerWorker(serverID, REPORT_SERVER_ID_SERIAL, &FC08); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.