Go Down

Topic: Arduino, RPi & PHP help! (2-way serial comm) (Read 2722 times) previous topic - next topic

xl97

#15
Sep 25, 2018, 02:26 am Last Edit: Sep 25, 2018, 03:53 am by xl97
sorry..

I dont want to confuse with my lack of proper terminology here..  LOL..

Here is is what I have, and is what I use to upload sketches to Pro-Mini's/custom board without USB..etc

* couldnt find an exact pic... but this is close enough:  (CP2102 based)

https://www.ebay.com/itm/CP2102-USB-2-0-to-TTL-UART-Module-6Pin-Serial-Converter-STC-Replace-FT232-Module/381374541932


I am just settling in at home from work (whew!)..

I'll be setting things up again and see if I can get it running without issues..  (might see some more posts here!)  LOL


Question:

if I am using this USB-TTL as a connection from the Arduino RX/TX/GND to the RPi USB port (COM1)..

How should I be powering the Arduino Uno now?  (if not from the RPi USB port?)  I shouldnt power it via USB right?  (Or wont that matter if the OPEN port actions are coming down the RX/TX pins instead?)  (not clear how that would work with RX/TX and USB?.. not very well I'd imagine)

xl97

I tried what I had.. (didnt work.. couldnt get the port open errors over and over)..

I'm guessing I'd need to install some sort of drivers for it?

I think I might have something more similar to your picture now that I think about it?

I had them before started using the product/item at the link  I posted above.

These were just 'cables'.. and either needed to be hacked.. or at best you needed to press reset button manually on the board(s)..

older camera cables or something... (no exposed pcb or options of any kind outside of cutting the end of the cable to expose the wires)

ron_sutherland

#17
Sep 25, 2018, 05:41 am Last Edit: Sep 25, 2018, 05:47 am by ron_sutherland Reason: ADD red LED to level shift
Quote
OPEN port actions are coming down the RX/TX pins instead?
When the 16u2 (USB port /dev/ttyACM0) is open it pulls down a pin (lets call it nDTR)  and that is coupled to the nRESET pin of the AVR (capacitively). Your extra capacitor added is a hulk and overrides the small one used for the reset trick.

If you don't use the handshake line (nDTR or nRTS) to do the reset trick then the AVR will not reset (hulk not needed).

To repeat: when RX/TX is connected the AVR will not reset when the port is open.  

Power is the next matter... when I suggested using the R-Pi serial hardware I totally forgot how dangerous it is since powering one down can damage the other, make sure to put a resistor (like 10k Ohm) between the Pi and the AVR with each line (Pi_RX<-- 10k<--RD_LED<--Uno_TX, Pi_TX -->10k-->Uno_RX) to be safe, and put a jumper on the 16u2's ISP header to hold it in reset.

Power the Uno without going through the R-Pi, sounds like a good idea to me, remember the common ground.

list of serial ports you may need

Code: [Select]
# hardwar serial on R-Pi
/dev/ttyAMA0
# FTDI and some other usb serial
/dev/ttyUSB0
# modem... what? is the ATmega16u2 a modem.
/dev/ttyACM0



Update: red LED to level shift
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

xl97

Ahh... (duh)..

I wasnt even thinking I would need to change the PORT name!!

No wonder I wasnt getting anywhere.. and kept getting PORT NOT OPEN errors!! LOL..

I'll have to try and set things up again that way and update the port name!


On a side note...

I have been reading around.. and stumbled upon talk of disabling the REST through the RPi?

using:

Code: [Select]
stty -F /dev/ttyACM0 -hupcl


How do you RE-ENABLE if it doesnt work?   (Im not a huge fan of doing things I cant un-do when I'm very new to all this! Linux jazz!)  :)

ron_sutherland

#19
Sep 25, 2018, 07:54 am Last Edit: Sep 25, 2018, 08:48 am by ron_sutherland
ignore all that stty stuff, that has to do with mapping the serial port (character device) through the pseudo terminal land for the actual terminal (e.g. xTerm, shell or whatever you are using). It is all a waste of time to learn.  

if you want to open the port at the command line use picocom, it just makes life much simpler (and doses all the tricks needed for xTerm or a shell terminal).

Code: [Select]
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install picocom


just short the Pi_TX to Pi_RX to loop back an echo to test it,  (sorry but I bet an update was needed)

Code: [Select]
picocom -b 38400 /dev/ttyAMA0

then try the AVR.


Update: to exit picocom use [C-a] [C-x], sorry should have thought of that.

Update2: I guess you are looking at stty for PHP, and I don't see how that will help, but I also don't have any ideas for PHP.
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

Robin2

https://www.ebay.com/itm/CP2102-USB-2-0-to-TTL-UART-Module-6Pin-Serial-Converter-STC-Replace-FT232-Module/381374541932
That should work fine. I have some things like that but I find the cable much easier to work with.

On this Linux laptop the regular Arduino USB cable shows up as ttyACM0 and the USB cable shows up as ttyUSB0.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

xl97

#21
Sep 25, 2018, 04:38 pm Last Edit: Sep 25, 2018, 04:50 pm by xl97
Yeah (using an RPi not a Linux laptop to be clear.. although not sure if that changes any advice.)...   I tried to use the cable.. for whatever reason, it didnt dawn on me that I would need to change the PORT name.. so I went back to the USB cable and CAPACITOR..

Same results... ports open fine.. but I cant really get any READ data from it...  in some random scenarios, which I havent been able to pinpoint/nail down as repeatable,....  I -will- actually get a READ response..

But that has been 'triggered' by actions like:

* rebooting the RPi with Arduino connected  and having the cap in and/or out (RESET/GND pins)
* doing the above... but already having the webpage at the 'SUBMIT' state
* leaving RPi powered.. but unplugging the Arduino/USB  (with and without the capacitor connected)..

again.. SOMETIMES... things start working...  but I cant for the like of me find out what combination makes it 'repeatable'..


*** of course.. my director/manager isnt here today.. so of all days, I have the day to 'play'.. but I have NO HARDWARE (anything) with me today!!   (bummer)



I dont want to open the port at command line (per se`)...

I was looking/reading that you can 'disable' the DTR (reset) command on the RPi.. so the USB connected Arduino will NO LONGER reset when the port open command is being sent...

With the second AJAX approach posted above.. I am not clear why this is so difficult now!!  :P

With the cap in place.. I do NOT see the Arduino resetting when the port opens.



Talking out loud here:

Is this really a PORT OPENING thing?  If the SENDING of data works.... the PORT is already opened and working correctly......right?  Especially in the SECOND approach posted above>?  Since the WRITE and then READ is done int he same snippet of code.. under the ONLY port opening line in the app?  SO if the WRITE is working after the port opening...  why isnt the READ portion working?  Its all 'working' under the same PORT OPENING action......right?







Robin2

Talking out loud here:

Is this really a PORT OPENING thing?  If the SENDING of data works.... the PORT is already opened and working correctly......right?  Especially in the SECOND approach posted above>?  Since the WRITE and then READ is done int he same snippet of code.. under the ONLY port opening line in the app?  SO if the WRITE is working after the port opening...  why isnt the READ portion working?  Its all 'working' under the same PORT OPENING action......right?

Talking out loud without details is pointless.

Have you tried my Python Arduino demo with both of your Serial connections - one at a time, of course? If so, what happens?

If it does not work post the exact program that you tried - the devil is in the detail.

How have you connected the USB-TTL adapter to your Arduino? it should be Rx to Tx, Tx to Rx and GND to GND.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

xl97

I am still at work... (hence the communication only so far)

I have NOT tried ANYTHING Python related...  (I'm not clear of the point here? to test the configuration of the PORT or something?  I will not be using Python in the end.. even if it -does- work unfortunately)

"How have you connected the USB-TTL adapter to your Arduino? it should be Rx to Tx, Tx to Rx and GND to GND."

not since last night.... and it didnt work initially.  After that I realized I needed to change the PORT name.. but have been at work still and havent tried with the name change.

That being said.. how should the Arduino be powered then?  If only RX/TX/GND from the USB-TTL adapter...  how is the Arduino getting power?

Robin2

I have NOT tried ANYTHING Python related...
Humble apologies. I was confusing you with another Thread.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

xl97

HA!

No worries...  I just want to make sure I wasnt mis-leading anyone!  I'm a bit overwhelmed at the moment on all of this... and anything Linux/CLI related has me second guessing every step.


Still not clear on how to POWER the Arduino correctly.. if I'm only supposed to use the RX?TX/GND lines from the USB-TTL cable?

For now... My first 'task' will be to try the USB-TTL cable (again).. and update the port names in the php script(s)...

If that doesnt work..... I'm not sure on what top try next.


Take Care
-J

ron_sutherland

Maybe the thing to do is a Python daemon. I was thinking about that the other night.

The daemon would start and look for a file that was provided by PHP that it could send to the Arduino, and gather the Arduino output to another file for PHP. I'm not sure how PHP would use that.

The daemon could keep the port open also.

picocom might do some of this without coding any Python

Code: [Select]
picocom --send-cmd "from_php_I_eat" --receive-cmd "to_php_I_barf" -b 38400 /dev/ttyAMA0

can PHP write the file to eat then run the picocom program and then read the barf file?

The bad news is this will harm the SD card if it is done very much.
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

xl97

I'd rather NOT have to walk down that path unless ultimately deemed necessary or the project dies!  LOL..

I mean others have done what I am doing using PHP...and their projects worked.

I am going to try this USB-TTL adapter approach (although I dont have much faith at this point)..

My last approach to look into is using the php_serial class I see most people using.  See if that (for whatever reason) gives me different results....

My last ditch effort will be to try and use some serial port on the RPi instead of its USB..  :(

Going to eat dinner... (gonna ditch the kids for a bit and give the USB-TTL path a shot!)   :)

xl97

#28
Sep 26, 2018, 04:54 am Last Edit: Sep 26, 2018, 05:19 am by xl97
Update:

I connected USB-TTL adapter..

updated my .php script to reflect the port it was on (confirmed in RPi)

[3.964496] usb 1-1.4: cp210x converter now attached to ttyUSB0


I am NOT getting any Port Not Opened errors any more... but now I cant seem to even be able to send data out TO the Arduino.....(much less RECEIVE anything back!)   LOL


How will the RPi know how to communicate with the cp210x based chip/adapter (without drivers like on a PC?)



So to be clear:

//$fp = fopen("/dev/ttyACM0", "w+"); //w = write w+ = read/write
$fp = fopen("/dev/ttyUSB0", "w+"); //w = write w+ = read/write


* That update was a step backwards as far as results go when attempting to use the USB-TTL
adapter.

I couldnt even SEND out the initial data TO the Arduino.

READ gave no response either (*but if the send wasnt working, there would be no incoming data to read, so expected result there)

Is there anything else that may need to have been done for the RPi to use this USB-TTL adapter to communicate to the Arduino?

Do I need to stty the ttyUSB0 port or anything of that nature? or give it any permissions?

ron_sutherland

use picocom to do a loopback test.

Code: [Select]
picocom -b 38400 /dev/ttyUSB0
I use an R-Pi Zero on an RPUpi shield to have a tool-chain at the network edge.

Go Up