rodrigo_rolle
Hello!

I'm using OpenPLC with ESP8266 board (on Windows host). I was able to set up everything, the system is running. My problem is: when I load the Hello World project using the input %IX0.0, the LED output %QX0.0 is always high. It happens because the pin D4 (that corresponds to %IX0.0) is always high, no matter what is connected to the pin. I just moved to another input port and the project worked propperly. Is there something that I have to change in the firmware in order to get normal response from pin D4?

Thanks in advance!
Rodrigo
Reply 0 0
thiagoralves
It depends on your ESP8266 board. What esp8266 board are you using? Because GPIO2 is used to start the bootloader, some board manufacturers put pull up or pull down resistors on that pin which can cause it to be constantly on or off.
Reply 0 0
rodrigo_rolle
I'm using LoLin NodeMcu V3 (ESP8266MOD) with NodeMcu Base ver 1.0
Reply 0 0
thiagoralves
This board looks similar to the one I use, but from a different manufacturer. Check to see if there are any pull up resistor attached to that pin, or any other hardware that could force it to be on logic high all the time. Usually, if there is a pull up on that pin, you could connect it directly to GND to force it to be low. Have you tried to connect GND directly to the pin to see if it goes low?
Reply 0 0
rodrigo_rolle
When I connect the pin directly to GND, I can read "low" at Radzio and the built-in led goes on. According to the documentation, this led is actually attached to pin D4.

EDIT: I used a lower resistor (220 ohm) in my button scheme and it worked. I'm able to use the pin normally.
Reply 0 0
thiagoralves
Your NodeMCU board has two leds. There is one on the ESP-12 module itself that is attached to GPIO2:  https://lowvoltage.github.io/2017/07/09/Onboard-LEDs-NodeMCU-Got-Two

That's probably the one that is turning on when you put GND on that pin, which means that it really has a pull up resistor on your board. To properly use it with a button, you must connect the button to GND and invert the logic on your program (i.e. use negated contacts).
Reply 0 0
rodrigo_rolle
Thanks, Thiago!

Please let me ask you another question. Thinking about the hello world project, is there a way to connect both Simulink and ESP to the same OpenPLC Server? Something like this...
arranjo.png 

My intention is to "reflect" I/O data from the real board to the simulation and vice versa. For instance: if I push the real button, I want to see both the real LED and the simulated LED (in Simulink) blinking. If I push the button on Simulink, my real LED also blinks.

Is there a way to make it work?
Reply 0 0
thiagoralves
Nope, there isn't. You could try to combine both ESP8266 and Simulink drivers into one to achieve something like that, but still, you can't have two devices associated with the same I/O point. If you do that, which one is the right one? If the button is ON on the simulation, but OFF on the board, would it means that the button must actually be ON or OFF? What you can do after you combine both drivers is define a portion of the address space to be allocated for the ESP (i.e. from %IX0.0 to %IX0.4, etc...) and the remaining to be allocated to simulink, like, it would control only %IX0.5 onwards. But still, it would be a lot of work.

Perhaps a better way to do what you want to do is to connect the ESP to Simulink instead and then have Simulink connected to OpenPLC. Then, simulink decides what it wants to send over to OpenPLC.
Reply 0 0
rodrigo_rolle
Well, I got interested in the idea of combining the drivers. Can I "merge" the hardware layer files from Simulink and ESP in a single cpp file? Analyzing the code, it looks that I'd have to copy the libraries, functions and calls. I would also have to change the initializiation function:


void initializeHardware()
{
  pthread_t thread;
  pthread_create(&thread, NULL, startServer_esp, NULL);
}


in order to add a new thread that refers to Simulink. Is it right?
Reply 0 0
thiagoralves
That's right. You will also have to combine the functionality of both drivers for the updateBuffersIn() and updateBuffersOut() functions. Those functions are the ones responsible for updating OpenPLC's internal buffers with I/O data. The only problem is that for both drivers (esp and simulink) the update process happens inside the thread, and not on those functions. So you will have to be careful so that the ESP driver writes on a portion of the address space and the simulink driver writes on another portion. If they both write to the same address portion, they will be constantly overwriting each other.
Reply 0 0
rodrigo_rolle
Great! It worked! I can run two similar "hello world" examples, one based on Simulink and another based on ESP/physical implementation. I set different I/O ports for Simulink and ESP and I got correct results for both implementations.
In order to prevent overwriting, I had to set different bytes for Simulink and ESP (e.g. Simulink ports were I0.0 and Q0.0 while ESP's ones were I1.0 and Q1.0)

I'm attaching the "merged" .cpp hardware layer file that I created to make it work. I'm also attaching a LabView VI for the Hello World project (it's supposed to run on LabView 13 or newer). It works exactly like Simulink but in a different environment (Matlab/Simulink are powerful but they consume a lot of RAM and processor capacity). Please feel free to share these files if you want.

Thank you!
Reply 0 0
rodrigo_rolle
Hello, Thiago!
I'm still testing this 2-device arrangement and I found an issue. Instead of running the Hello World example, I set two square wave generators (1-second on, 1-second off), one for Simulink and another one for ESP. I used one input as enable for each generator and chose different I/O bytes for them. The Simulink side works fine, but when I turn on SimLink in order to get communication with the OpenPLC server, the ESP side starts working incorrectly. I mounted a small circuit (switch and LED) to check the ESP side and it visibly does not generate a square wave as it should.

I tried to slow down the cycle time of the thread on PLCOpen Editor, also tried to separate the generators in two different POUs (in order to run them on different tasks) but the problem persists. When I edited the hardware_layer, I configured two threads to work together. Maybe Simulink's thread is consuming more time than ESP's one when I turn on SimLink. Do you think that this is the real issue? Is there a way to fix it?

Edit: I also debugged it on Radzio. I can see that the ESP "enable" input that I set in my code is randomly read as '0' by the OpenPLC server in some moments, even when I connect it directly to 3V. This misreading happens only while SimLink is enabled (it's not cabling failure).
Reply 0 0
thiagoralves
rodrigo_rolle wrote:
Edit: I also debugged it on Radzio. I can see that the ESP "enable" input that I set in my code is randomly read as '0' by the OpenPLC server in some moments, even when I connect it directly to 3V. This misreading happens only while SimLink is enabled (it's not cabling failure).

This is probably the reason why it is not working. I haven't seen your code, but I can assume that you're getting into a race condition with your two threads. Maybe Simulink is writing that zero on your input, overwriting whatever the ESP has sent.
Reply 0 0
rodrigo_rolle
I assumed that if I didn't write nothing via SimLink on the byte, I would have no overwriting. My SimLink ports are declared like this:


station0.add(digital_in) = "10001"  #IX0.0 -simulink
station0.add(digital_out) = "10002"  #QX0.0 - simulink
station0.add(digital_out) = "11001"  #QX0.1 - unused
station0.add(digital_out) = "11002"
station0.add(digital_out) = "11003"
station0.add(digital_out) = "11004"
station0.add(digital_out) = "11005"
station0.add(digital_out) = "11006"
station0.add(digital_out) = "11007"   #QX0.7 - unused
station0.add(digital_out) = "10003"   #QX1.0 - ESP LED


As you can see, I just read one bit at QX1 and it still overwrites IX1 byte.
Have you ever implemented any task/thread coordination on OpenPLC?
Reply 0 0
thiagoralves
SimLink only sends what is in the config file, but the simulink driver on OpenPLC rewrites the whole buffer up to ANALOG_BUF_SIZE and DIGITAL_BUF_SIZE

  while(1)
  {
    //printf("waiting for data...\n");
    net_len = recvfrom(socket_fd, plc_data, sizeof(*plc_data), 0, (struct sockaddr *) &client, &cli_len);
    //printf("data received!\n");
    if (net_len < 0)
    {
      printf("Error receiving data on socket %d\n", socket_fd);
    }
    else
    {
      pthread_mutex_lock(&bufferLock); //lock mutex
      for (int i = 0; i < ANALOG_BUF_SIZE; i++)
      {
        if (int_input[i] != NULL) *int_input[i] = plc_data->analogIn[i];
        if (int_output[i] != NULL) plc_data->analogOut[i] = *int_output[i];
      }
      for (int i = 0; i < DIGITAL_BUF_SIZE; i++)
      {
        if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = plc_data->digitalIn[i];
        if (bool_output[i/8][i%8] != NULL) plc_data->digitalOut[i] = *bool_output[i/8][i%8];
      }
      pthread_mutex_unlock(&bufferLock); //unlock mutex
      //printf("sending data...\n");
      net_len = sendto(socket_fd, plc_data, sizeof(*plc_data), 0, (struct sockaddr *) &client, cli_len);
      if (net_len < 0)
      {
        printf("Error sending data on socket %d\n", socket_fd);
      }
    }
  }


That's why I said you should be careful in isolating the address spaces for each driver:
thiagoralves wrote:
So you will have to be careful so that the ESP driver writes on a portion of the address space and the simulink driver writes on another portion. If they both write to the same address portion, they will be constantly overwriting each other.
Reply 0 0
Reply