Hi EveryOne
I am trying to read Level Indicator meter (as slave) with master (esp32 ) ,using communication (MAX485).
I have connected PIN 16,17 as RX2,TX2 and using PIN 4 and 2 as a MAX485_DE & MAX485_RE_NEG respectively. I am getting error of "ku8MBResponseTimedOut" on serial print with response of "Inside Timeout Block". I also attached the operation Manual of "KCMR- 84ARS Digital Level Indicator" as well.
using the following
#include <ModbusMaster.h>
#include <ArduinoOTA.h>
#include "credentials.h"
#include<AWS_IOT.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#define CLIENT_ID "payload1"// thing unique ID, this id should be unique among all things associated with your AWS account.
#define MQTT_TOPIC "$aws/things/DHT11-RS485/shadow/update" //topic for the MQTT data
#define AWS_HOST "a4ur6wss9ptht-ats.iot.eu-central-1.amazonaws.com" // your host for uploading data to AWS,
AWS_IOT aws;
#define RXD2 16 //RXD2
#define TXD2 17 //TXD2
#define MAX485_DE 4 //RX 729
#define MAX485_RE_NEG 2 //TX 728
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
float WaterLevel,WaterLevel1,WaterLevel2;
//SoftwareSerial mySerial(RXD2,TXD2); // RX, TX
void setup()
{
Serial.begin(9600);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.setPort(3232); // Port defaults to 3232 // ArduinoOTA.setPort(3232);
ArduinoOTA.setHostname("OTA"); // Hostname defaults to esp3232-[MAC] // ArduinoOTA.setHostname("myesp32");
ArduinoOTA.setPassword("syedamin110"); // No authentication by default // ArduinoOTA.setPassword("admin");
// Password can be set with it's md5 value as well // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
ArduinoOTA.setPasswordHash("9539721a862a4209fb63f3be33cbf505");
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// Modbus slave ID 1
Serial2.begin(9600,SERIAL_8N1,RXD2,TXD2); //SERIAL_8N1
node.begin(1, Serial2);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
//AWS Amazon
Serial.println("\n Initializing connetction to AWS....");
if (aws.connect(AWS_HOST, CLIENT_ID) == 0) { // connects to host and returns 0 upon success
Serial.println(" Connected to AWS\n Done.");
}
else {
Serial.println(" Connection failed!\n make sure your subscription to MQTT in the test page");
}
Serial.println(" Done.\n\nDone.\n");
}
void loop()
{
ArduinoOTA.handle();
yield();
Serial.println("Uploaded10");
uint8_t result;
node.clearResponseBuffer();
result = node.readHoldingRegisters(0x40000,6);
if (result == node.ku8MBSuccess)
{
Serial.println("Reading WaterLeveL values");
Serial.println(node.getResponseBuffer(0)); //This shows humidity Serial.print("%")
float WaterLevel = (node.getResponseBuffer(0));
Serial.println("WaterLevel");
Serial.println(WaterLevel);
Serial.println(node.getResponseBuffer(1)); //This shows humidity Serial.print("%")
float WaterLevel1 = (node.getResponseBuffer(1));
Serial.println("WaterLevel1");
Serial.println(WaterLevel1);
Serial.println(node.getResponseBuffer(2)); //This shows humidity Serial.print("%")
float WaterLevel2 = (node.getResponseBuffer(2));
Serial.println("WaterLevel2");
Serial.println(WaterLevel2);
//Sending Data to AWS
// check whether reading was successful or not
if (WaterLevel == NAN) { // NAN means no available data
Serial.println("Reading failed.");
}
else {
//create string payload for publishing
String payload1 = "WaterLevel: ";
payload1 += String(WaterLevel);
char payload[100]; //40
payload1.toCharArray(payload, 100); //sprintf(payload,"Humidity:%f Temperature:%f'C",h,t); // Create the payload for publishing
Serial.println("Publishing:- ");
Serial.println(payload);
if (aws.publish(MQTT_TOPIC, payload) == 0) { // publishes payload and returns 0 upon success
Serial.println("Success\n");
}
else {
Serial.println("Failed!\n");
}
}
}
if (result == node.ku8MBResponseTimedOut)
{
Serial.println("Inside Timeout block ");
}
if (result == node.ku8MBInvalidCRC)
{
Serial.println("Inside ku8MBInvalidCRC block ");
}
if (result == node.ku8MBInvalidFunction)
{
Serial.println("Inside ku8MBInvalidFunction block ");
}
if (result == node.ku8MBInvalidSlaveID)
{
Serial.println("Inside ku8MBInvalidFunction block ");
}
if (result == node.ku8MBSlaveDeviceFailure)
{
Serial.println("Inside ku8MBSlaveDeviceFailure block ");
}
if (result == node.ku8MBIllegalDataValue)
{
Serial.println("Inside ku8MBIllegalDataValue block ");
}
delay(5000); //delay(4000);
}
KCMRS-84ARS.pdf (696 KB)