hairem
Hello

I am fairly new to OpenPLC, and I am trying to use OpenPLC to convert various inputs into Modbus outputs for an array of devices that I use at work for data gathering that is reported to a data logger. My first project is to get information from a ADS1115 from Adafruit to output as Modbus Registers. I do have a question though. I have a "driver" for this circuit, I have one that I can call using Python3 and one that I can use with my Arduino boards. My question is, is it possible to have the Pi read the sensor data directly and just output the channel data or do I have to have a Arduino connected to do this?
Reply 0 0
thiagoralves
You can use the Pi alone if you write a c++ driver to read the ADS1115 and add it to OpenPLC. You can use the Hardware Layer Code Box to write your driver.
Reply 0 0
hairem
Would something like this work? https://github.com/ElektorLabs/ads1115-driver
Reply 0 0
thiagoralves
It would, but then you need to provide the implementation for all the i2c functions, as stated in ads1x1x.h:
// I2C stubs, implementations to be provided by the user.
extern uint8_t ADS1x1x_i2c_start_write(uint8_t i2c_address);
extern uint8_t ADS1x1x_i2c_write(uint8_t x);
extern uint8_t ADS1x1x_i2c_start_read(uint8_t i2c_address, uint16_t bytes_to_read);
extern uint8_t ADS1x1x_i2c_read(void);
extern uint8_t ADS1x1x_i2c_stop(void);

Maybe it would be easier to just get your arduino code that is already working and add it to the OpenPLC arduino firmware, and then connect the arduino to your Pi over USB
Reply 0 0
hairem
I really don't don't really have the physcal space on this project to have much more than the pi and a few connection so having an additional Arduino when I could just use the onboard GPIO doesn't make sense. I can look up the addresses.
Reply 0 0
thiagoralves
A quick google search brought me this:  https://www.raspberrypi.org/forums/viewtopic.php?t=36250

Look for  gordon@drogon.net post (the 6th one). It shows how easy it is to read ADS1115 using WirinPi. OpenPLC uses WiringPi to control Raspberry Pi's GPIO. So basically, all you have to do is modify /webserver/core/hardware_layers/raspberrypi.cpp file with the code shown in the comment to add ADS1115 support. Just put the analog readings inside the void updateBuffersIn() function. You will have to store the analog readings in the int_input[x] buffer. Look for the void updateBuffersIn() in unipi.cpp for an example on how to do this properly. After modifying the file, don't forget to reapply the Raspberry Pi Hardware Layer for your changes to take effect. You do that on OpenPLC web interface -> Hardware -> Select Raspberry Pi from the list -> Save Changes.
Reply 0 0
hairem
Are you reffering to this section?:
//printf("\nAnalog Inputs:");
  for (int i = 0; i < 2; i++)
  {
      if (pinNotPresent(ignored_int_inputs, ARRAY_SIZE(ignored_int_inputs), i))
        if (int_input[i] != NULL) *int_input[i] = mcp_adcRead(i); //printf("[AI%d]: %d | ", i, mcp_adcRead(i));
  }
  //printf("\n");

and adding this:
  ads1115Setup (MY_BASE, 0x48) ;
  int ch0 = analogRead (MY_BASE + 0) ;
  int ch3 = analogRead (MY_BASE + 3) ;

to that section?

With the understanding that I added both
#include <ads1115.h>
#define MY_BASE 2222

to the import and define sections at the top of the file.
Reply 0 0
thiagoralves
Yes, that's right. However it will be more appropriate to call the ads1115Setup() function inside the initializeHardware() function, otherwise you will be initializing the ads1115 hardware every time you read from it.
Reply 0 0
MOH
thiagoralveswhat the rest of this code in void updateBuffersIn()  ?
Reply 0 0
Reply