Good morning
Been trying to get the LILYGO® TTGO T-CAN485 ESP32 CAN RS-485 to talk to the sdm230 port 5-6
Currently have a r485 radio master slave from ebay on 433 MHZ and that works fine
but looking to move the data over wifi using 2 of the above
to start with looking to get the data on IO21/22 (rs485) to the SDM port 4/5 and read the modbus data
out put to a web page
here my code
tried different example and codes but never seems to work
(the second stage would be to move this via wifi to the other esp and send on to the invertor ) any ideas
Any help or other code or link or help appreciated
Been trying to get the LILYGO® TTGO T-CAN485 ESP32 CAN RS-485 to talk to the sdm230 port 5-6
Currently have a r485 radio master slave from ebay on 433 MHZ and that works fine
but looking to move the data over wifi using 2 of the above
to start with looking to get the data on IO21/22 (rs485) to the SDM port 4/5 and read the modbus data
out put to a web page
here my code
tried different example and codes but never seems to work
(the second stage would be to move this via wifi to the other esp and send on to the invertor ) any ideas
Any help or other code or link or help appreciated
Code:
#include <WiFi.h>
#include <WebServer.h>
#include <ModbusRTU.h> // Modbus-ESP8266 library
// Define Modbus communication details
ModbusRTU mb;
const int slaveID = 1; // SDM230 default Modbus ID
// Wi-Fi credentials
const char* ssid = "downstairsNG";
const char* password = "charliebear";
// Fixed IP configuration
IPAddress local_IP(192, 168, 2, 100);
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
// Web server on port 80
WebServer server(80);
// Variables for sensor values
float voltage, current, power, powerFactor, frequency, importEnergy, exportEnergy;
void setupWiFi() {
WiFi.config(local_IP, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWi-Fi connected. IP address:");
Serial.println(WiFi.localIP());
}
// Function to read values from SDM meter
bool readSDM() {
bool success = true;
success &= mb.readHreg(slaveID, 0x0000, (uint16_t*)&voltage, 2); // Voltage
success &= mb.readHreg(slaveID, 0x0006, (uint16_t*)¤t, 2); // Current
success &= mb.readHreg(slaveID, 0x000C, (uint16_t*)&power, 2); // Active Power
success &= mb.readHreg(slaveID, 0x0012, (uint16_t*)&powerFactor, 2); // Power Factor
success &= mb.readHreg(slaveID, 0x0046, (uint16_t*)&frequency, 2); // Frequency
success &= mb.readHreg(slaveID, 0x0048, (uint16_t*)&importEnergy, 2); // Import Active Energy
success &= mb.readHreg(slaveID, 0x004A, (uint16_t*)&exportEnergy, 2); // Export Active Energy
return success;
}
// HTML generation for web page display
String generateHTML() {
String html = "<html><body><h1>SDM Meter Monitor</h1>";
html += "<p><strong>Voltage:</strong> " + String(voltage) + " V</p>";
html += "<p><strong>Current:</strong> " + String(current) + " A</p>";
html += "<p><strong>Power:</strong> " + String(power) + " W</p>";
html += "<p><strong>Power Factor:</strong> " + String(powerFactor) + "</p>";
html += "<p><strong>Frequency:</strong> " + String(frequency) + " Hz</p>";
html += "<p><strong>Imported Energy:</strong> " + String(importEnergy) + " kWh</p>";
html += "<p><strong>Exported Energy:</strong> " + String(exportEnergy) + " kWh</p>";
html += "</body></html>";
return html;
}
// Function to handle HTTP requests
void handleRoot() {
if (readSDM()) {
server.send(200, "text/html", generateHTML());
} else {
server.send(500, "text/html", "<html><body><h1>Error Reading SDM Data</h1></body></html>");
}
}
void setup() {
Serial.begin(115200);
// Initialize Serial2 for Modbus RTU communication
Serial2.begin(9600, SERIAL_8N1, 21, 22); // RX 21, TX 22 for ESP32
mb.begin(&Serial2, -1); // Initialize Modbus on Serial2
mb.master(); // Set Modbus as master
// Connect to Wi-Fi
setupWiFi();
// Configure web server
server.on("/", handleRoot); // Display SDM data on root
server.begin();
Serial.println("Web server started. Access at http://192.168.2.100/");
}
void loop() {
mb.task(); // Process Modbus tasks
server.handleClient(); // Process web server requests
delay(1000); // Refresh data every second
}
Last edited by a moderator: