I create a project to read pH water, but my module sensor doesn't fix with raspberry pi 3 so I using Arduino to read pH, then send to raspberry and upload the data to firebase.
but, I have some problem, when raspberry read data from Arduino, the looping doesn't work. the error message is serial.util.serialexception
here's source code on Raspberry
import os
import serial
import time
from firebase import firebase
arduino = serial.Serial('/dev/ttyACM0',9600)
firebase = firebase.FirebaseApplication('https://raspi-ph.firebaseio.com/', None)
def update_firebase():
phair = arduino.readline()
if data is not None:
time.sleep(1)
pieces = data.split("sensor= ")
ph = pieces
print ph
else:
print('Failed to get data. Try Again!')
time.sleep(10)
data = {"Sensor pH": phair}
firebase.post('/sensor/ph', data)
while True:
update_firebase()
time.sleep(5)
and here's source code on Arduino
const int analogInPin = A0;
int sensorValue = 0;
unsigned long int avgValue;
float b;
int buf[10],temp;
void setup() {
Serial.begin(9600);
}
void loop() {
for(int i=0;i<10;i++)
{
buf[i]=analogRead(analogInPin);
delay(10);
}
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
avgValue=0;
for(int i=2;i<8;i++)
avgValue+=buf[i];
float pHVol=(float)avgValue*5.0/1024/6;
float phValue = -5.70 * pHVol + 21.34;
Serial.print("sensor = ");
Serial.println(phValue);
delay(20);
}
last, the error message on raspi
['seor= 0.52\r\n']
Traceback (most recent call last):
File "ard.py", line 27, in <module>
update_firebase()
File "ard.py", line 11, in update_firebase
phair = arduino.readline()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 490, in read
'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
the looping only run once