-2

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

Mavisa9
  • 144
  • 4
  • 14
  • 3
    Can you put the *exact* error message in your question? – larsks Jun 30 '18 at 14:43
  • 1
    The problem is not in PH. The problem is in communication over `UART`. Please, write program on rasperry that ONLY reads data from `UART` and prints. On arduino: write program that ONLY prints "hello from arduino\n". – RedEyed Jun 30 '18 at 15:03
  • I just wanna know how are the two communicating with each other are they using a USB connection or are they running on the same pi? – BigZee Jun 30 '18 at 15:57
  • using USB connection – Mavisa9 Jul 01 '18 at 06:28
  • @larsk i put the error message on the question. help me please :(( – Mavisa9 Jul 01 '18 at 14:05
  • Have a look at [Serial Receiving from Arduino to Raspberry Pi with PySerial stops after a while](https://stackoverflow.com/q/20107700). Also please consider giving your question a title that makes it likely to be searched by someone else. May be something like _Python SerialException: Device reports readiness to read but returned no data (device disconnected?)_ (and btw. there is already a [question with that title](https://stackoverflow.com/q/28343941). So, may be this one is interesting for you, too.) – Adrian W Jul 01 '18 at 14:47
  • Possible duplicate of [Serial Receiving from Arduino to Raspberry Pi with PySerial stops after a while](https://stackoverflow.com/questions/20107700/serial-receiving-from-arduino-to-raspberry-pi-with-pyserial-stops-after-a-while) – Adrian W Jul 01 '18 at 14:50

1 Answers1

0

Looking at the source code, this exception is based on the assumption that ready for reading plus empty data implies a disconnected device.

The way to avoid this behaviour is to specify a timeout when creating a Serial instance.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94