4
votes

I am trying to get a raspberry pi to communicate with an arduino using the tx/rx pins. I have the arduino programmed to send back the ASCII code for the letter it received.

code:

byte number = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())  
  {
    number = Serial.read();
    Serial.print("character recieved: ");
    Serial.println(number, DEC);
  }
}

But when I open minicom and it type into it, nothing happens. If I open up the arduino's serial monitor and send a character minicom displays "character recieved: " and the characters ASCII code. I tried creating a python program using py serial,

code:

import serial
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
ser.open()

ser.write("testing")
try:
        while 1:
                response = ser.readline()
                print response
except KeyboardInterrupt:
        ser.close()

but nothing is displayed. I have looked all over the internet, but I have found no solution. Please help, thanks in advance.

2

2 Answers

1
votes

Very late to the party but I ran into the same problem today. What you need to do is to configure minicom not to use hardware flow control. This is what is stopping the input that you give minicom from going to the Arduino.

Press Ctrl+A then O (letter Oh) and choose Serial port setup and there switch Hardware flow control to No.

Once you have done this you may wish to save the new setting as the default: Again Press Ctrl+A then O (letter Oh) but now choose Save setup as dfl

0
votes

First, ser.readline looks for \r\n before it will return anything, so sending a single byte will simply go into a buffer.

So you want to always use ser.println for the last part of the line. That works the same on both the Pi and an Arduino.

Also, in Minicom, you want to press Enter after every line so ser.readline() will return the line.

..

You will not need to do anything strange to make the serial port work. However, I use the actual USB connector rather than hooking directly to the TX/RX lines. In fact, I use the same cable you would use to program it from a PC.

I use the Rpi both as an Arduino programmer and to process the results it sends back from analog readings.

The Rpi python program that reads the serial needs to be stopped while uploading from the IDE to the Arduino, but that is the only real consideration. No two programs can grab the USB port at the same time.

On the Rpi side, I initially used

ls /dev/ttyUSB*

to find the port that it is using.I even unplugged the cable then did that command again and it was gone. Plugged it back in and it was back. It is very reliable (as opposed to plugging in USB memory).

On the Arduino side it is always the same serial stuff you already have in your program.

You are right to use minicom for testing.

To find which string to use in the shebang I used this:

which python

So here is what I use on the Rpi3:

#!/usr/bin/python
import serial
from datetime import datetime
tab = "\t"
ser = serial.Serial("/dev/ttyUSB0",9600)

while True :
    linein = ser.readline()
    if len(linein)<10 : continue
    print "/dev/ttyUSB0 input -->  " + repr(linein)
    date   = str(datetime.now().date())
    date   = date[:10]            
    time   = str(datetime.now().time())
    time   = time[:8]
    outline = date + tab + time + tab + linein
    if not outline.endswith("Inverter\r\n") :
        f = open("htv.dat","a")
        f.write(outline)
        f.close()
        print "htv.dat ----> " + repr(outline)
        print "htv.dat ----> " + outline
    else: print

To make it executable I do this:

cp ss.py ssx
chmod +x ssx
sudo cp ssx /usr/sbin

And here is the code I use on the Nano:

// HTV - Humidity, Temperature, Voltage          2/4/17
//
//  Voltage measurement variables for the voltage divider    A2
float R1 = 1000000.0; 
float R2 =  147000.0; 
float constADC=4.59;   

//  Humidity/temp readings     D2
#include <Adafruit_Sensor.h>
#include "DHT.h"
#define DHTPIN 2    
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  dht.begin();
  }

void loop() { 
  int value=analogRead(A2);
  float vout = value * (constADC/1024.0);  
  float vin = vout / (R2/(R1+R2));  

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float df = t*(9.0/5.0)+32;
  if (isnan(h) || isnan(t)) { h=0; df=0; }

  Serial.print(h);
  Serial.print("\t");
  Serial.print(df);
  Serial.print("\t");
  Serial.println(vin);

  delay(60000);
}

Nothing too fancy. I just open a terminal window on the Rpi and type ssx and it starts collecting. If I have to upload something to the Nano from the IDE I use ^C to stop SSX during the upload. Then I restart it.

Opening the file for the write then closing it afterwards prevents any data corruption.