I connected a Raspberry Pi and an Arduino via USB. Arduino is getting data from the world via sensors (EC and temperature sensor) and writing this data to serial. The Raspberry is writing this data into a database.
The Arduino sketch:
#include <OneWire.h>
#include <DallasTemperature.h>
int R1= 500;
int Ra=25; //Resistance of powering Pins
int ECPin= A0;
int ECGround=A1;
int ECPower =A4;
float PPMconversion=0.7;
float TemperatureCoef = 0.019;
float K=2.88;
#define ONE_WIRE_BUS 10 // Data wire For Temp Probe is plugged into pin 10 on the Arduino
const int TempProbePossitive =8; //Temp Probe power connected to pin 9
const int TempProbeNegative=9; //Temp Probe Negative connected to pin 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
float Temperature=10;
float EC=0;
float EC25 =0;
int ppm =0;
float raw= 0;
float Vin= 5;
float Vdrop= 0;
float Rc= 0;
float buffer=0;
void setup()
{
Serial.begin(9600);
pinMode(TempProbeNegative , OUTPUT ); //seting ground pin as output for tmp probe
digitalWrite(TempProbeNegative , LOW );//Seting it to ground so it can sink current
pinMode(TempProbePossitive , OUTPUT );//ditto but for positive
digitalWrite(TempProbePossitive , HIGH );
pinMode(ECPin,INPUT);
pinMode(ECPower,OUTPUT);//Setting pin for sourcing current
pinMode(ECGround,OUTPUT);//setting pin for sinking current
digitalWrite(ECGround,LOW);//We can leave the ground connected permanantly
delay(100);// gives sensor time to settle
sensors.begin();
delay(100);
R1=(R1+Ra);// Taking into acount Powering Pin Resitance
};
void loop()
{
GetEC();
PrintReadings(); // Cals Print routine [below main loop]
delay(20000);
}
void GetEC(){
sensors.requestTemperatures();// Send the command to get temperatures
Temperature=sensors.getTempCByIndex(0); //Stores Value in Variable
digitalWrite(ECPower,HIGH);
raw= analogRead(ECPin);
raw= analogRead(ECPin);// This is not a mistake, First reading will be low beause if charged a capacitor
digitalWrite(ECPower,LOW);
Vdrop= (Vin*raw)/1024.0;
Rc=(Vdrop*R1)/(Vin-Vdrop);
Rc=Rc-Ra; //acounting for Digital Pin Resitance
EC = 1000/(Rc*K);
EC25 = EC/ (1+ TemperatureCoef*(Temperature-25.0));
ppm=(EC25)*(PPMconversion*1000);
}
void PrintReadings(){
Serial.print("Rc: ");
Serial.print(Rc);
Serial.print(" EC: ");
Serial.print(EC25);
Serial.print(" Simens ");
Serial.print(ppm);
Serial.print(" ppm ");
Serial.print(Temperature);
Serial.println(" *C ");
Serial.print("Vdrop: ");
Serial.println(Vdrop);
Serial.print("Rc: ");
Serial.println(Rc);
Serial.print(EC);
Serial.println("Siemens");
};
code on Raspberry Pi:
import serial
import time
import re
import sqlite3
for com in range(0,4):
try:
PORT = '/dev/ttyACM'+str(com)
BAUD = 9600
board = serial.Serial(PORT,BAUD)
board.close()
break
except:
pass
DEVICE = '/dev/ttyACM'+str(com)
BAUD = 9600
s = serial.Serial(DEVICE, BAUD)
conn=sqlite3.connect('mydatabase.db')
cursor=conn.cursor()
time.sleep(5) # der Arduino resettet nach einer Seriellen Verbindung, daher muss kurz gewartet werden
try:
while True:
try:
response = s.readline()
numbers = re.findall(r"[-+]?\d*\.\d+|\d+", response)
if len(numbers) == 4:
temp = numbers[3]
ec = numbers[1]
result = cursor.execute("INSERT INTO sensordata (temp, ec) VALUES ({temp}, {ec})".format(temp=temp, ec=ec))
conn.commit()
print response
except Exception as e:
print e
pass
except KeyboardInterrupt:
s.close()
conn.close()
Data is written for about 24 hours on Raspberry side, then I get no serial output from Arduino any more. Same problem when I restart the python script again. When I restart the python script and serial communication is started again, the Arduino resets. I did not change this default behaviour. The fact that I still do not get data via serial shows me that it is no memory problem on the Arduino side. One more hint, that it must be a problem with the Raspberry, do I get from the fact that rebooting Raspberry solves the problem and the data is logged for another 24 hours.
So this is an Arduino forum and no Raspberry. Anyway, is anybody curious enough to give me any hint, how to establish an solid communication?