andreas.kuning
Greeting.
 
Need help with PCF8574 port extender on ESP32, using arduino sketch and accessing it via MODBUS RTU.
 
I have Kincony KC868-A16 board with ESP32 and two PCF8574 port extenders. I wrote arduino extension to use those extenders and map external variables with %IX %QX addresses to access pins via MODBUS RTU and use the device as Windows Runtime Slave.
 
All works fine... FOR SOME TIME)) I leave the device running at evening and in the morning i have non working port extenders(( Hardware works fine, because i use this board with other firmwares (NanoFramework)
 
What i have in the morning:
 
  1. The device is running, but at first look it do not respont to input (IX) signals and do not set out signals (QX)
  2. Device is accesible via MODBUS RTU using MODBUS RTU client, i can set coils and read the values i had set, but nothing related with real world). So i considered that arduino sketch stop working.
  3. After rebooting all works fine
  4. And while i wrote this post my out leds turns red)) So board reacts to changes, but lag between setting MODBUS value till it could be seen at the board approximetly 20 MINUTES. Some memory leak? Adn, but, i changed them again to FALSE and after 20 minutes they are still ON.
 
Attaching my code, project and screenshots. Thank for help.
 
//////////////////////////////// sketch section ////////////////////////////////

#include <Adafruit_PCF8574.h>
#include <Adafruit_ADS1X15.h>

Adafruit_ADS1115 ADS0, ADS1;
Adafruit_PCF8574 PCFDO0, PCFDO1, PCFDI0, PCFDI1;

void SetPCFDO()
{
    PCFDO0.digitalWrite(0, !PCFOUT15);
    PCFDO0.digitalWrite(1, !PCFOUT14);
    PCFDO0.digitalWrite(2, !PCFOUT13);
    PCFDO0.digitalWrite(3, !PCFOUT12);
    PCFDO0.digitalWrite(4, !PCFOUT11);
    PCFDO0.digitalWrite(5, !PCFOUT10);
    PCFDO0.digitalWrite(6, !PCFOUT9);
    PCFDO0.digitalWrite(7, !PCFOUT8);

    PCFDO1.digitalWrite(0, !PCFOUT7);
    PCFDO1.digitalWrite(1, !PCFOUT6);
    PCFDO1.digitalWrite(2, !PCFOUT5);
    PCFDO1.digitalWrite(3, !PCFOUT4);
    PCFDO1.digitalWrite(4, !PCFOUT3);
    PCFDO1.digitalWrite(5, !PCFOUT2);
    PCFDO1.digitalWrite(6, !PCFOUT1);
    PCFDO1.digitalWrite(7, !PCFOUT0);
}

void ReadPCFDI()
{
    PCFIN15 = !PCFDI0.digitalRead(7);
    PCFIN14 = !PCFDI0.digitalRead(6);
    PCFIN13 = !PCFDI0.digitalRead(5);
    PCFIN12 = !PCFDI0.digitalRead(4);
    PCFIN11 = !PCFDI0.digitalRead(3);
    PCFIN10 = !PCFDI0.digitalRead(2);
    PCFIN9 = !PCFDI0.digitalRead(1);
    PCFIN8 = !PCFDI0.digitalRead(0);

    PCFIN7 = !PCFDI1.digitalRead(7);
    PCFIN6 = !PCFDI1.digitalRead(6);
    PCFIN5 = !PCFDI1.digitalRead(5);
    PCFIN4 = !PCFDI1.digitalRead(4);
    PCFIN3 = !PCFDI1.digitalRead(3);
    PCFIN2 = !PCFDI1.digitalRead(2);
    PCFIN1 = !PCFDI1.digitalRead(1);
    PCFIN0 = !PCFDI1.digitalRead(0);
}

void ReadADC0()
{
  ADC00 = ADS0.readADC_SingleEnded(0);
  ADC01 = ADS0.readADC_SingleEnded(1);
  ADC02 = ADS0.readADC_SingleEnded(2);
  ADC03 = ADS0.readADC_SingleEnded(3);
}

void ReadADC1()
{
  ADC10 = ADS1.readADC_SingleEnded(0);
  ADC11 = ADS1.readADC_SingleEnded(1);
  ADC12 = ADS1.readADC_SingleEnded(2);
  ADC13 = ADS1.readADC_SingleEnded(3);
}

void sketch_setup()
{
    Wire.setPins(4, 5);

    PCFDO0.begin(0x24, &Wire);

    PCFDO0.digitalWriteByte(0xFF);

    PCFDO1.begin(0x25, &Wire);

    PCFDO1.digitalWriteByte(0xFF);

    PCFDI0.begin(0x21, &Wire);
    
    PCFDI0.pinMode(0, INPUT);
    PCFDI0.pinMode(1, INPUT);
    PCFDI0.pinMode(2, INPUT);
    PCFDI0.pinMode(3, INPUT);
    PCFDI0.pinMode(4, INPUT);
    PCFDI0.pinMode(5, INPUT);
    PCFDI0.pinMode(6, INPUT);
    PCFDI0.pinMode(7, INPUT);

    PCFDI1.begin(0x22, &Wire);
    
    PCFDI1.pinMode(0, INPUT);
    PCFDI1.pinMode(1, INPUT);
    PCFDI1.pinMode(2, INPUT);
    PCFDI1.pinMode(3, INPUT);
    PCFDI1.pinMode(4, INPUT);
    PCFDI1.pinMode(5, INPUT);
    PCFDI1.pinMode(6, INPUT);
    PCFDI1.pinMode(7, INPUT);

    ADS0.begin(0x4A, &Wire);
    ADS0.setGain(GAIN_EIGHT);

    ADS1.begin(0x4B, &Wire);
    ADS1.setGain(GAIN_EIGHT);
}

void sketch_loop()
{

    ReadPCFDI();

    SetPCFDO();
    ReadADC0();
    ReadADC1();
}
 
scr1.pngscr2.pngscr3.pngscr4.png
Reply 0 0
thiagoralves
So just selecting "Serial1" from the Modbus RTU configuration on the OpenPLC upload dialog works then? I thought ESP32 was having issues with Modbus RTU on other Serial ports... maybe that was fixed by some Arduino core update.
Reply 0 0
dieter.l
Hi Andreas,
maybe use another PCF8574 libryry, I had a lot of trouble with ESP32 and some libraries in the past.
Currently I using the PCF8574 library from Renzo Mischanti - this seems working well with my KC868-A6 board.
 
Regards,
Dieter
Reply 0 0
kortenbach
Could you show us how you managed to get this board working in OpenPLC? Some screenshots or code snippets would be very helpful and save a lot of time!
Thanks!
 
Bert
Reply 0 0
dieter.l
Hi Bert,
 
 
There is my actual development for this hardware. Please note - this is still in development - display is not working well currently.
Input/Outputs, Modbus/RTU are working.
 
"PCF8574" library from Renzo Mischanti, for the display "ESP8266 and ESP32 OLED Driver" from ThingPulse are needed.
 
When I find time in the next weeks I will finish the development/testing, maybe without display in the first version and raise a pull request.
 
Cheers,
Dieter
Reply 1 0
kortenbach
Thanks for sharing this Dieter!
I just ordered a KC868-A6!
Lets see where this goes...
Reply 0 0
kortenbach
Hi Dieter,
 My board just arrived! (No display-version).
Any luck getting the KC868-A6 working?
As a work-around, could I get the I/O working by just adding some C++ code in the OPENPLC IDE?
The code could implement a simple HAL between the PCF8574's on the board and the ladder-code...
What do you think?
 
Best regards,
Bert
Reply 0 0
dieter.l
Hi Bert,
my code editor/arduino at openplc-master · dieter-l-git/editor is working, only the display is not really usable - but this is optional.
 
the files
KC868-A6.hal
and
KC868-A6.cpp
to your local installation editor\arduino\src\hal\
 
Don't set #define HASDISPLAY in KC868-A6.cpp
 
Add to your local hals.json (editor\arduino\examples\Baremetal) the following entry:
"KC868-A6": {
"core": "esp32:esp32",
"default_ain": "36, 39, 34, 35",
"default_aout": "26, 25",
"default_din": "P0, P1, P2, P3, P4, P5, P6, P7",
"default_dout": "P0, P1, P2, P3, P4, P5",
"define": "BOARD_ESP32",
"last_update": 0,
"platform": "esp32:esp32:esp32:UploadSpeed=921600,PartitionScheme=huge_app,FlashMode=qio,FlashFreq=80",
"source": "KC868-A6.cpp",
"version": "3.0.4"
},
Then you should be able to use the Digital I/O's on the two PCF8574's, Analog I/O's, ModBus/RTU and Wireless in OpenPLC Editor.
 
Please install "PCF8574" library from Renzo Mischanti in your Arduino environment before you use this, just to be sure.
 
Currently OneWire an other special things are not implemented or tested.
Should work with Arduino code in OpenPLC.

Kind Regards
Dieter
Reply 0 0
kortenbach
Thank you Dieter!
This is very helpful. Will try this the day after tomorrow. I'll be back!
Reply 0 0
kortenbach
Hi Dieter. I am trying to do this step:
"Please install "PCF8574" library from Renzo Mischanti in your Arduino environment before you use this, just to be sure."
How can I do that If I don't have the Arduino IDE installed?
I believe that OpenPLC calls the Arduino compiler, so I should be able to add the PCF8574 without using the IDE, but how?
 
Best regards,
Bert
Reply 0 0
fg71
You could use arduino-cli to install libraries.
 
-> arduino-cli lib install <your_library_name>
 
Or
 
You could copy/unzip the library to your sketchbook location
-> arduino-cli config get directories.user
 
You add the header inside .cpp file that you find from your hals.json file.
Reply 0 0
kortenbach
I tried that:
[203128%2F9156367%2Fmceclip0]
But It looks like it installed some other PCF8574 library now.
I am getting compilation errors about missing member "pinmode".
Probably the Renzo Mischanti library has another name?
Will keep you posted...
 
Bert
Reply 0 0
kortenbach
Oh, wait...
[203128%2F9156415%2Fmceclip0]
This did the trick!
Thanks!
Reply 0 0
kortenbach
@andreas.kuning
Sorry for hijacking your thread like that. How is your Kincony KC868-A16 board coming along?
You may try to use @dieter.l his solution for the A6 board and extend it a bit (?)
 
Best regards,
Bert
Reply 0 0
andreas.kuning
@andreas.kuning
Sorry for hijacking your thread like that. How is your Kincony KC868-A16 board coming along?
You may try to use @dieter.l his solution for the A6 board and extend it a bit (?)
 
Best regards,
Bert
I got tired of trying to make all the embedded devices work, the last straw was two boards not working correctly via Modbus RTU((. And I figured the best solution for me would be to run the Windows runtime and use these boards as external port extenders via Modbus TCP, the firmware was written using the Arduino framework, it supports the embedded Ethernet chip.
For now Windows runtime with two extension boards works fine for three months.
Reply 0 0
kortenbach
Thanks for the feedback! Happy to hear that you succeeded (even if this was not the intended solution).
 
Best regards,
Bert
Reply 0 0
Reply