9Views0Replies

Author Options:

Write output on registery inside ESP8266 Answered

Could any one help me please?

I have the following code to make MODBUS tcp/ip(its work good) the other issue in the code is ultrasonic sensor (trig on D1& echo on D2 and output distance on serial port)I want to write distance on one of memory register say (3). I use the command "digitalWrite(distance, MBHoldingRegister[3] )" its fail then I use analogWrite also fail what's the solution?

#include

#define trigPin D1

#define echoPin D2

#define analog_output D5

float distance;

long duration;

int dataIn;

int m = 0;

int h = 0;

int t = 0;

const char* ssid = "MH_Ext";

const char* password = "74375325";

int ModbusTCP_port = 502;

//////// Required for Modbus TCP / IP /// Requerido para Modbus TCP/IP /////////

#define maxInputRegister 20

#define maxHoldingRegister 20

#define MB_FC_NONE 0

#define MB_FC_READ_REGISTERS 3 //implemented

#define MB_FC_WRITE_REGISTER 6 //implemented

#define MB_FC_WRITE_MULTIPLE_REGISTERS 16 //implemented

/

/ MODBUS Error Codes

//

#define MB_EC_NONE 0

#define MB_EC_ILLEGAL_FUNCTION 1

#define MB_EC_ILLEGAL_DATA_ADDRESS 2

#define MB_EC_ILLEGAL_DATA_VALUE 3

#define MB_EC_SLAVE_DEVICE_FAILURE 4

/

/ MODBUS MBAP offsets

//

#define MB_TCP_TID 0

#define MB_TCP_PID 2

#define MB_TCP_LEN 4

#define MB_TCP_UID 6

#define MB_TCP_FUNC 7

#define MB_TCP_REGISTER_START 8

#define MB_TCP_REGISTER_NUMBER 10

byte ByteArray[260];

unsigned int MBHoldingRegister[maxHoldingRegister];

//////////////////////////////////////////////////////////////////////////

WiFiServer MBServer(ModbusTCP_port);

void setup() {

pinMode(14, OUTPUT);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

Serial.begin(9600);

delay(100) ;

WiFi.begin(ssid, password);

delay(100) ;

Serial.println(".");

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

MBServer.begin();

Serial.println("Connected ");

Serial.print("ESP8266 Slave Modbus TCP/IP ");

Serial.print(WiFi.localIP());

Serial.print(":");

Serial.println(String(ModbusTCP_port));

Serial.println("Modbus TCP/IP Online");

}

void loop() {

distance = getDistance();

Serial.println(distance);

delay(1000);

// Check if a client has connected // Modbus TCP/IP

WiFiClient client = MBServer.available();

if (!client) {

return;

}

boolean flagClientConnected = 0;

byte byteFN = MB_FC_NONE;

int Start;

int WordDataLength;

int ByteDataLength;

int MessageLength;

// Modbus TCP/IP

while (client.connected()) {

if(client.available())

{

flagClientConnected = 1;

int i = 0;

while(client.available())

{

ByteArray[i] = client.read();

i++;

}

client.flush();

///// code here --- codigo aqui

///////// Holding Register [0] A [9] = 10 Holding Registers Escritura

///////// Holding Register [0] A [9] = 10 Holding Registers Writing

/// Enable Output 14

digitalWrite(14, MBHoldingRegister[14] );

digitalWrite(distance, MBHoldingRegister[12] );

//// end code - fin

//// rutine Modbus TCP

byteFN = ByteArray[MB_TCP_FUNC];

Start = word(ByteArray[MB_TCP_REGISTER_START],ByteArray[MB_TCP_REGISTER_START+1]);

WordDataLength = word(ByteArray[MB_TCP_REGISTER_NUMBER],ByteArray[MB_TCP_REGISTER_NUMBER+1]);

}

// Handle request

switch(byteFN) {

case MB_FC_NONE:

break;

case MB_FC_READ_REGISTERS: // 03 Read Holding Registers

ByteDataLength = WordDataLength * 2;

ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.

ByteArray[8] = ByteDataLength; //Number of bytes after this one (or number of bytes of data).

for(int i = 0; i < WordDataLength; i++)

{

ByteArray[ 9 + i * 2] = highByte(MBHoldingRegister[Start + i]);

ByteArray[10 + i * 2] = lowByte(MBHoldingRegister[Start + i]);

}

MessageLength = ByteDataLength + 9;

client.write((const uint8_t *)ByteArray,MessageLength);

byteFN = MB_FC_NONE;

break;

case MB_FC_WRITE_REGISTER: // 06 Write Holding Register

MBHoldingRegister[Start] = word(ByteArray[MB_TCP_REGISTER_NUMBER],ByteArray[MB_TCP_REGISTER_NUMBER+1]);

ByteArray[5] = 6; //Number of bytes after this one.

MessageLength = 12;

client.write((const uint8_t *)ByteArray,MessageLength);

byteFN = MB_FC_NONE;

break;

case MB_FC_WRITE_MULTIPLE_REGISTERS: //16 Write Holding Registers

ByteDataLength = WordDataLength * 2;

ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.

for(int i = 0; i < WordDataLength; i++)

{

MBHoldingRegister[Start + i] = word(ByteArray[ 13 + i * 2],ByteArray[14 + i * 2]);

}

MessageLength = 12;

client.write((const uint8_t *)ByteArray,MessageLength);

byteFN = MB_FC_NONE;

break;

}

}

}

//===== getDistance - Custom Function

int getDistance() {

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance = duration * 0.034 / 2; // distance in cm

return distance;

}

Discussions