Mudbus library on NodeMCU (ESP8266)

Hello,

I’m struggeling with one problem for two days now, so I decided to ask here for some help :frowning:

I’m playing arround with modbus communication between an raspberry Pi which has Codesys running on it and an NodeMCU ESP8266-12F.

First I tried modbus communication with an arduino Uno + ethernet shield. this worked fine with the Mudbus.h library.
Then I tried using an ESP8266-01 module as wifi component via serial connection on the UNO.
Little changes in the library and it worked perfectly.

Due to it’s enormous power, I now decided to skip the Arduino Uno part and only use NodeMCU ESP8266 module. (Programming with arduino IDE)

And now the problem:

I tried to change the mudbus.h library, but it just won’t work.

Anyone tried this before?

I tried to change the mudbus.h library, but it just won't work.

Thank you for providing details of what you changed and what the symptoms are of it not working.

UKHeliBob:
Thank you for providing details of what you changed and what the symptoms are of it not working.

Thank you for your sarcasm, but you're right.

Basicly the whole library is just client.read() and client write() of modbus data.
So I changed it from using ethernet to 1) WiFiEsp.h and now 2) ESP8266WiFi.h library.

And as I mentioned, using ESP-01 as a "shield" via serial communication, this worked fine.

But not directly on the ESP-12.

What's not working is two things:
a) codesys inicatates a 'not running Bus' and
b) ESP resets all the time automatically

Do you have experience here?

But not directly on the ESP-12.

The code you didn't show does something. You failed to explain what it does.
You expect the code to do something. You failed to explain what you expect.

You can not, then, expect us to help.

Alright, I will Post my code tomorrow Morning when I get back on my computer.
Sorry for that. But just for you to know. I didn't expect you guys to Provide any code to me.

I just wanted to know if someone already did this and would be able to give me a hint...

Good morning all.

Sorry for my insufficient postings yesterday.
I’m gonna start over:

So here’s the code (reduced to the not-working part) I’m having problems with:

#include <ESP8266WiFi.h>
#include <Mudbus_esp.h>

char ssid[]= "my_ssid";
char pass[] = "my_pwd";

Mudbus Mb;

void setup() 
{
Serial.begin(115200);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);  
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) 
      {
      delay(500);
      Serial.print(".");
      }
Serial.println("");
Serial.println("WiFi connected");  
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}

void loop()
{
Mb.Run();
Mb.C[0] = 1;
delay(100);
}

Please find the original Mudbus.h library attached.
The important part looks like:

#include "Mudbus.h"

// For Arduino 0022
// Server MbServer(MB_PORT);
// For Arduino 1.0
EthernetServer MbServer(MB_PORT);

Mudbus::Mudbus()
{
}

void Mudbus::Run()
{  
  Runs = 1 + Runs * (Runs < 999);

  //****************** Read from socket ****************
  // For Arduino 0022
  // Client client = MbServer.available();
  // For Arduino 1.0
  EthernetClient client = MbServer.available();
  if(client.available())
  {
    Reads = 1 + Reads * (Reads < 999);
    int i = 0;
    while(client.available())
    {
      ByteArray[i] = client.read();
      i++;
    }
    SetFC(ByteArray[7]);  //Byte 7 of request is FC
    if(!Active)
    {
      Active = true;
      PreviousActivityTime = millis();
      #ifdef MbDebug
        Serial.println("Mb active");
      #endif
    }
  }
  if(millis() > (PreviousActivityTime + 60000))
  {
    if(Active)
    {
      Active = false;
      #ifdef MbDebug
        Serial.println("Mb not active");
      #endif
    }
  }

  int Start, WordDataLength, ByteDataLength, CoilDataLength, MessageLength;

  //****************** Read Coils **********************
  if(FC == MB_FC_READ_COILS)
  {
    Start = word(ByteArray[8],ByteArray[9]);
    CoilDataLength = word(ByteArray[10],ByteArray[11]);
    ByteDataLength = CoilDataLength / 8;
    if(ByteDataLength * 8 < CoilDataLength) ByteDataLength++;      
    CoilDataLength = ByteDataLength * 8;
    #ifdef MbDebug
      Serial.print(" MB_FC_READ_COILS S=");
      Serial.print(Start);
      Serial.print(" L=");
      Serial.println(CoilDataLength);
    #endif
    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 < ByteDataLength ; i++)
    {
      for(int j = 0; j < 8; j++)
      {
        bitWrite(ByteArray[9 + i], j, C[Start + i * 8 + j]);
      }
    }
    MessageLength = ByteDataLength + 9;
    client.write(ByteArray, MessageLength);
    Writes = 1 + Writes * (Writes < 999);
    FC = MB_FC_NONE;
  }

To work directly on the ESP-12, I changed just three things:

#include "Mudbus.h"                             to	#include "Mudbus_esp.h"

EthernetServer MbServer(MB_PORT); 		to 	WiFiServer MbServer(MB_PORT);

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

Code compiles and programm runs,
but with codesys it get a “not-running bus” and sometimes a restarting ESP.

Can anyone help me?

Mudbus.cpp (7.16 KB)

I have a problem this topic. I saw this problem solved in topic. I want to Raspberry and Arduino-Esp8266 with modbus in codesys. Can you give me detail for mudbus.h change?