My raspberry pi is not receiving or not reading serial data from a connected Arduino. My Arduino is set to collect pressure and temperature data at 1-hour intervals and send it over serial port. The raspberry pi should listen for the data and write it to a file. I have it setup so that if the raspberry pi does not receive any data within an hour, it resets itself and writes error code to a file. My current setup resets itself every hour. Before I added the reset it would just not save anything for hours on end. The Arduino is connected to the raspberry pi via usb port.
Arduino Code
#include <LiquidCrystal_I2C.h> //allows interfacing with LCD screens
#include "max6675.h"
int ktcSO = 4;
int ktcCS = 5;
int ktcCLK = 6;
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
const int pressureInput0 = A0, pressureInput1 = A1, pressureInput2 = A2, pressureInput3 = A3 ; //Select the analog input pin for the pressure transducer
float pressureValue0 = 0, pressureValue1 = 0, pressureValue2 = 0, pressureValue3 = 0 ; //Variable to store the value coming from the pressure transducer
const int pressureZero = 102.3; //Analog reading of pressure transducer at 0psi
const int pressureMax = 920.7; //Analog reading of pressure transducer at MAX psi
const int pressuretransducermaxPSI = 2500; //psi value of transducer being used
const int sensorreadDelay = 1000; //Constant integer to set the sensor read delay in milliseconds
long Timer = 0;
LiquidCrystal_I2C lcd(0x27, 20, 4); //Sets the LCD I2C communication address; format(address, columns, rows)
void setup() {
Serial.begin(9600);
lcd.init(); // Initiate the LCD:
lcd.backlight(); // Initiate the LCD:
lcd.clear();
delay(3500); // Wait for RasPi
}
void loop() {
lcd.setCursor(13, 0);
lcd.print(ktc.readCelsius());
lcd.print(" C");
float Pressure0 = printPressure(pressureValue0, pressureInput0, 0, 9.17);
float Pressure1 = printPressure(pressureValue1, pressureInput1, 1, 9.17);
float Pressure2 = printPressure(pressureValue2, pressureInput2, 2, 9.17);
float Pressure3 = printPressure(pressureValue3, pressureInput3, 3, 9.17);
if (Timer <= 0) { //If time to next read
Serial.print(Pressure0); Serial.print(","); Serial.print(Pressure1); Serial.print(","); Serial.print(Pressure2);
Serial.print(","); Serial.print(Pressure3); Serial.print(","); Serial.println(ktc.readCelsius());
Timer = 3600000; //Wait 1 hour
}
delay(sensorreadDelay); //Delay in milliseconds between read values
Timer -= sensorreadDelay;
lcd.clear();
}
float printPressure (float pressureValue, int pressureInput, int cursorLocation, float offSet) {
lcd.setCursor(0, cursorLocation);
pressureValue = analogRead(pressureInput); //Reads value from input pin and assigns to variable
pressureValue = ((pressureValue - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero) + offSet;
lcd.print(pressureValue);
lcd.print("psi");
return pressureValue;
}
Raspberry Pi Python Code
#!/usr/bin/env python3
import serial
import datetime
import time
import os
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.flush()
date = datetime.datetime.now()
delay = 3610
waitTime = delay
while True:
try:
if ser.in_waiting > 0:
file = open("/home/pi/MEGA/Python_Script/Save.txt", "a")
date = datetime.datetime.now()
line = ser.readline().decode('utf-8').rstrip()
file.write(date.strftime("%m-%d-%Y,%H:%M:%S," + line + '\n'))
file.close()
print(date.strftime("%m-%d-%Y,%H:%M:%S," + line + '\n'))
waitTime = delay
#break
if waitTime == 0:
file = open("/home/pi/MEGA/Python_Script/ErrorLogSB.txt", "a")
file.write(date.strftime("Time Out " + "%m-%d-%Y,%H:%M:%S," + '\n'))
file.close()
os.system('sudo shutdown -r now')
time.sleep(1)
waitTime -= 1
except:
file = open("/home/pi/MEGA/Python_Script/ErrorLogSB.txt", "a")
file.write(date.strftime("Other Error " + "%m-%d-%Y,%H:%M:%S," + '\n'))
file.close()
os.system('sudo shutdown -r now')```