Help for project approach

Hi everyone and I'm sorry for my bad english.
I'd like to make something that sound like this with yun board:
i have to read some value from a modbus rtu object via RS485 and i want to register them into a mysql database which is in a pc.
I think to do in this way : with a RS485 shield i try to read value with a sketch and i store them in some variables, after that i read these variables with a php script (into linino) and this php script will send them to the mysql database (via ethernet).
For you is the right way or would it better to do all only with a php script (with his own libraries)?

Thank you for all suggestions !!

Hi everyone and I'm sorry for my bad english.

No apologies necessary! Your English is very good, and you got your point across better than some native speakers.

What kind of data rate are you expecting? How often will you be reading and storing data? (Every few seconds, or hundreds of times per second?

If you use an RS-485 shield, you will have to have a sketch that reads the data and sends it to the Linux side for sending to the database. Also, you will have to use SoftwareSerial, you will not be able to use the hardware Serial object on pins 0 and 1 as those pins are dedicated to communicating with the Linux processor. When looking at the SoftwareSerial documentation, note the pin restrictions mentioned for the Leonardo: the Yun uses the same processor as the Leonardo, so the same restrictions apply.

You can use Bridge.put() in the sketch to set the received values, and read them from the Linux side. I don't know of a PHP library to read them on the Linux side, but there is a Python library that makes it easy. Is there a reason to go with PHP? It seems like there may be better support with Python.

A limitation of using Bridge.put() is that the receiving side is not notified when a value has been written, it can only see a difference in values - so if it's necessary to process the same value twice in a row, this is not the best solution. Also, the sending side will not know when the receiving side has actually read the value, so it's possible that the sketch could set two values before the Linux side can read it, and the Linux side will only see the last value.

Using the Bridge's Mailbox class is slightly more involved than Bridge.put(), but it works around these issues: the Linux side will get every value sent, in order, even if there are duplicates.

Another way to send the data to the Linux side is to use a Process object. This can send the data one at a time as parameters to the Linux side script, or it can start the script and then keep sending data by writing to the Process object - the script will receive it as standard input. The fist method is a bit easier to implement, the second gives better throughput and is faster.

If you want to do it all on the Linux (PHP) side, you will have to forget about using a shield - Linux has no direct ability to access any shield pins, only the Arduino sketch processor can access them. This means you will have to use an adapter that allows you to connect to the modbus using USB. Possible, but you may have an easier time finding a modbus library for the Arduino sketch than one for PHP or Python.

If I were starting this project, I would probably use an RS-485 shield, and an Arduino modbus library. Write a sketch to fetch the data needed, and print it out on the Serial port for testing. Then, write a Python script on the Linux side that can read that data and write it to the database. You can test that on the SSH shell by manually running the script and typing in values. Then, tie it all together by having the sketch start up the Python script and run it asynchronously, the use Process.println() to send data to the Linux side script.

Thank you so much for your answer, i try the way you write.
The data are elettric currents, so they are numbers. I want to read the values once a day.

Thank you again.

I'll update this thread when i will try something.

See you.

If only updating once a day, then I would have the sketch read the data, then use the Process class to call a Python or PHP script on the Linux side, passing in the value(s) as parameters. That is such a low data rate that the overhead of starting the Linux process on each call is meaningless - there’s no need to complicate things by running the script asynchronously and keeping it open.

By pass bridge lib complete:

Plan B:

Do every thing at Linux side:

USB to RS485 Max485 Converter Adapter, connect it at Linux side.

cost few $

easy_install minimalmodbus
Finished processing dependencies for minimalmodbus

import minimalmodbus
import serial

m = minimalmodbus.Instrument('/dev/ttyUSB0', 2) # port name, slave address (in decimal)
m.serial.baudrate = 19200
m.serial.bytesize = 8
m.serial.stopbits = 2
m.serial.parity   = serial.PARITY_NONE

data = m.read_registers(0, 2, 3) # 3 = Read holding register

print "Value A = ", data[0]
print "Value B = ", data[1]

if (data[0] < 20):
    send = 1
    send = 0

m.write_register(2, send, 0, 16) # 16 = Write multiple registers