First, ser.readline looks for \ r \ n before it returns anything, so sending one byte will just go to the buffer.
So you want to always use ser.println for the last part of the line. This works the same on both Pi and Arduin.
In addition, in Minicom you want to press Enter after each line, so ser.readline () will return the line.
..
You do not need to do anything strange to make the serial port work. However, I am using the actual USB connector and not connecting directly to the TX / RX lines. In fact, I use the same cable that you would use to program it from a PC.
I use Rpi as an Arduino programmer and process the results that it sends back from the analog readings.
The python Rpi program that reads the series should be stopped when loading from the IDE into Arduino, but this is the only real consideration. Two programs cannot simultaneously capture a USB port.
On the rpi side, I originally used
ls /dev/ttyUSB*
to find the port that it uses. I even unplugged the cable, and then ran this command again, and it disappeared. Insert it back and it is back. It is very reliable (as opposed to connecting a USB memory).
On the Arduino side, there is always the same serial material that you already have in your program.
You have the right to use minicom for testing.
To find which line to use in shebang, I used this:
which python
So here is what I use on 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 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 special. I just open a terminal window on Rpi and type ssx and it starts to collect. If I need to load something into Nano from the IDE, I use ^ C to stop SSX at boot time. Then I restart it.
Opening a file for writing and then closing it prevents data corruption.