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:
float R1 = 1000000.0;
float R2 = 147000.0;
float constADC=4.59;
#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.