Serial comms / GPIO Pins

General programming chat and advice for beginners

9 posts
by mickatcol » Mon Nov 26, 2012 11:46 am
Hi,

I have just started working with the PI and would like to connect one of my Arduino projects to my PI for internet control.

I have connected the Arduino to the Raspberry Pi's GPIO with a CD4050 IC to bring the UART down to 3.3v.

I comment out this line in /etc/inittab
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

I removed this from /boot/cmdline.txt
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

I am using SoftSerial on the arduino to passthrough.
Code: Select all
/*

 Connects Arduino to Raspberry Pi
 Arduino: SoftSerial
 Raspberry Pi: GPIO UART

 This is just a simple passthrough, based on Arduino SoftSerial example

 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() 
{
 // Open serial communications to PC and wait for port to open:
  Serial.begin(57600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println("Connected to PC");

  // set the data rate for the SoftwareSerial port to Raspberry Pi
  mySerial.begin(9600);
}

void loop() // run over and over
{
  // If data is available on Raspberry Pi, print it to PC
  if (mySerial.available())
    Serial.write(mySerial.read());
  // If data is available on PC, print it to Raspberry Pi
  if (Serial.available())
    mySerial.write(Serial.read());
}


Now the problem is when I send Serial data from the PI to the Arduino using the command.
echo -e "Sent from PI \r\n" > /dev/ttyAMA0

I get M•¹Ñ™É½µA%5jRj¤ü on the Arduino. But if I use.
sudo echo -e "Sent from PI \r\n" > /dev/ttyAMA0

I get ÿSent from PI (Not sure why I get ÿ at the start of each line??)

I also did a test sending Serial from php and its the same, I get garbage on the arduino side.
My project is going to use php to send commands to the arduino.

If anyone could help me out that would be great.

Here is the php code.
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include "php_serial.class.php";

$serial = new phpSerial;
$serial->deviceSet("/dev/ttyAMA0");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
$serial->sendMessage("Hello from my PHP script.");

$serial->deviceClose();

echo "I've sended a message! \n\r";
?>


Thanks
User avatar
Posts: 5
Joined: Mon Nov 26, 2012 11:07 am
by gordon@drogon.net » Mon Nov 26, 2012 12:57 pm
The first thing I'd do is remove the Arduino and loop the Tx/Rx pins together and run up minicom on the Pi. you should get what you type.

Then remove the loop and put the Arduino back in.

Remember that you either need to be in the 'dialout' group, or run as root to access the serial port too.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1495
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by mickatcol » Mon Nov 26, 2012 1:00 pm
Did not mention I added www-data and pi to dialout.

Just been playing with NC and found the oddest thing.
If I use NC command
sudo nc -l 999 > /dev/ttyAMA0

Then run my php, I get the correct output "ÿHello from my PHP script."

I would have thought that if NC had the Serial port open then PHP would not be able to use it?
Also ÿ still shows on the opening of ttyAMA0 port.

Thanks
User avatar
Posts: 5
Joined: Mon Nov 26, 2012 11:07 am
by mickatcol » Mon Nov 26, 2012 1:17 pm
Thanks Gordon

I tested the loop with minicom and it works fine. Retested with Arduino and minicom 9600 baud and that also worked fine. Just the same ÿ on connect. What is that anyway?

I am guessing it must be a baud rate problem with the php?

Thanks

Mick
User avatar
Posts: 5
Joined: Mon Nov 26, 2012 11:07 am
by Joe Schmoe » Mon Nov 26, 2012 1:30 pm
Obligatory post to the effect that recommending (or even mentioning in polite company) minicom is cruel and inhuman treatment.

Certainly as long as "screen" exists.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2522
Joined: Sun Jan 15, 2012 1:11 pm
by gordon@drogon.net » Mon Nov 26, 2012 1:39 pm
mickatcol wrote:Thanks Gordon

I tested the loop with minicom and it works fine. Retested with Arduino and minicom 9600 baud and that also worked fine. Just the same ÿ on connect. What is that anyway?

I am guessing it must be a baud rate problem with the php?

Thanks

Mick


It's more likely to be a glitch somewhere in the initial open of the device. Maybe you have to make yor protocol expect some garbage initially...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1495
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by gordon@drogon.net » Mon Nov 26, 2012 1:42 pm
Joe Schmoe wrote:Obligatory post to the effect that recommending (or even mentioning in polite company) minicom is cruel and inhuman treatment.

Certainly as long as "screen" exists.


Minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom minicom

I bet you get fanatical about the differences betwen vi(m) and emacs too.

Lifes too short. deal with it.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1495
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by Joe Schmoe » Mon Nov 26, 2012 1:52 pm
You probably recommend using "cat" to write programs, too.
Never answer the question you are asked. Rather, answer the question you wish you had been asked.

- Robert S. McNamara - quoted in "Fog of War" -
Posts: 2522
Joined: Sun Jan 15, 2012 1:11 pm
by mickatcol » Mon Nov 26, 2012 8:08 pm
I am still unsure why I keep getting garbage from the serial on Arduino side.

When I run the php script it sends garbage to the Arduino.
If I run something like
minicom -b 9600 -o -D /dev/ttyAMA0
OR
nc -l 999 > /dev/ttyAMA0
then run the php, it send it fine.

Can anyone think why this is happening.

Thanks - Mick
User avatar
Posts: 5
Joined: Mon Nov 26, 2012 11:07 am