sekilomahal
Posts: 9
Joined: Fri Jun 17, 2016 9:27 am

Read serial data arduino to raspberry pi get an error

Fri Jul 15, 2016 1:58 am

Hello, i want to read serial data from arduino uno to raspberry pi. I have 4 variables to read, but while read data, i got an error. this the error

Code: Select all

humidity: 61.000000 , temp: 25.800000 , ph: 7.670000 , flow: 0
Raw: 61.00 25.80 7.67 0 

humidity: 61.000000 , temp: 25.800000 , ph: 7.670000 , flow: 0
Raw: 61.00 25.80 7.67 0 

humidity: 61.000000 , temp: 25.800000 , ph: 7.670000 , flow: 0
Raw: 61.00 25.80 7.67 0 

humidity: 61.000000 , temp: 25.800000 , ph: 7.670000 , flow: 0
Raw: 61.00 25.80 7.67 0 

humidity: 61.000000 , temp: 25.800000 , ph: 7.670000 , flow: 0
Raw: 61.00 25.80 7.67 0 

humidity: 61.000000 , temp: 25.800000 , ph: 7.670000 , flow: 0
Raw: 61.00 0 25.60 7.81 0 

No Input Data
 
this my raspberry code

Code: Select all

import serial
import time
from ubidots import ApiClient

serPort = serial.Serial('/dev/ttyACM0', 9600)
time.sleep(0.2)
serPort.flush()
serPort.readline()

try:
 print "Connect to Ubidots"
 api = ApiClient("43936d6645f2b8455336df48b448edc945fa3c07")
except:
 print "Error Connecting"

humidity = api.get_variable('575ecb1d7625426ce1af47fb')
temperature = api.get_variable('575ecb1f7625426c8bd7476e')
ph = api.get_variable('575ecb207625426c8bd74774')
flow = api.get_variable('575ecb217625426c8bd7477a')

try:
  while True :
    sensorRead = serPort.read()
    if sensorRead == '$':
       sensorRead = serPort.readline()
       print "Raw: "+sensorRead
       sensorVar = sensorRead.split()
       if len(sensorVar) >= 4 :         
        humidityvar = float(sensorVar[0])
        tempvar = float(sensorVar[1])
        phvar = float(sensorVar[2])
        flowvar = int(sensorVar[3])
        sensor = 'humidity: %f , temp: %f , ph: %f , flow: %d'  % (humidityvar, tempvar, phvar, flowvar)
        print sensor
        humidity.save_value({'value':humidityvar})
        temperature.save_value({'value':tempvar})
        ph.save_value({'value':phvar})
        flow.save_value({'value':flowvar})
except KeyboardInterrupt:
  serPort.close()
except ValueError:
  print "No Input Data"
any solution? thanks

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Read serial data arduino to raspberry pi get an error

Fri Jul 15, 2016 6:50 am

Change the except Value error line and below to be.

Code: Select all

except ValueError as err:
    print "No Input Data: ", err
That will tell us what the real error is located in the code.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

sekilomahal
Posts: 9
Joined: Fri Jun 17, 2016 9:27 am

Re: Read serial data arduino to raspberry pi get an error

Sat Jul 16, 2016 7:36 am

Hello, actually the error is i have 4 values readable from arduino, but in serial monitor in raspberry i got 5 values in last line raw data i got 5 values. i got value 0 beside hum value. so i think the error

ghp
Posts: 1518
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Read serial data arduino to raspberry pi get an error

Sat Jul 16, 2016 6:26 pm

Hello,
the arduino send functions are limited in throughput. The serial line is limited by baud rate, and the send buffer inside the arduino software libs is very limited. So it is very likely that 'wrong chars' occur if you send very often or with long record length. Try to slow down the frequency of send operations on arduino side. Also keep in mind that also on the receiver side the buffering is limited and overflows are possible. And there are possible errors 'on the line'. Scratch itself tries from time to time to open usb serial connections to check for scratchboard available (which is a mess, but Time Rowledge does not see a a chance to switch this off), this causes data problems in some environments.
In total, create your receiver side in away that you check the received data for correctness, and throw away not valid data. Regular expression pattern are very handy for checking the data, if you want even more reliability then send a record length field with the data and a checksum.
Regards,
Gerhard

sekilomahal
Posts: 9
Joined: Fri Jun 17, 2016 9:27 am

Re: Read serial data arduino to raspberry pi get an error

Sun Jul 17, 2016 3:22 am

Hello thanks for reply gerhard, here my arduino code

Code: Select all

#include <DHT.h>
#include <Wire.h>
#include <BH1750.h>

unsigned long phSamplingTick, dispTick, flowTick, dhtTick, lightTick;
unsigned int phSensor, phSensorSum, flowSensor, flowml = 0;
volatile unsigned int flowCount = 0;
unsigned char sample;
float voltage, phSensorValue, phValue, Offset = 0.0, humidity, temperature, flowrate = 0.0;
uint16_t light;

#define DHTPIN 10     
#define DHTTYPE DHT22
#define DEBUG

DHT dht(DHTPIN, DHTTYPE);
BH1750 lightsensor;

void flowInt()
{
  flowCount++;
  digitalWrite(13,!digitalRead(13));
}

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  attachInterrupt(0,flowInt,FALLING);
  dht.begin();
}

void loop()
{
  /* if ( millis() - lightTick > 1000 ) 
   {
      light = lightsensor.readLightLevel();
      lightTick = millis();
   } */
   
   if ( millis() - dhtTick >= 2000 )
   {
      humidity = dht.readHumidity();
      temperature = dht.readTemperature();  
      dhtTick = millis();  
   }
  
   if ( millis() - flowTick >= 1000 )
   { 
     // if ( flowCount > 0 ) 
      flowSensor = flowCount;
      flowrate = ((1000.0 / (millis() - flowTick)) * flowSensor) / 4.5;
      flowml = (flowrate / 60) *1000;
      flowCount = 0;
      flowTick = millis();
   }
  
   if ( millis() - phSamplingTick >= 20 ) {
      phSensor = analogRead(0);
      phSensorSum += phSensor;
      sample++;
      if ( sample >= 40 ) {
        phSensorValue = phSensorSum / 40;
        voltage = phSensorValue * 5.0 / 1024.0;
        phValue = 3.5 * voltage + Offset;
        phSensorSum = 0;
        sample = 0;
      }
      phSamplingTick = millis();
   }
#ifdef DEBUG
   if ( millis() - dispTick >= 2000 )
   {
      Serial.print("$");
     // Serial.print(light);
     // Serial.print(" ");
      Serial.print(humidity);
      Serial.print(" ");
      Serial.print(temperature);
      Serial.print(" ");
      Serial.print(phValue);
      Serial.print(" ");
      Serial.print(flowml);
      Serial.print(" \n");
      dispTick = millis();
   }
#endif
   
}
sorry im new in python also arduino
so i have to change the baud rate? you said receiver side the buffering is limited and overflows are possible, can you give example code? and throw away not valid data i just pass it?
thanks

ghp
Posts: 1518
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Read serial data arduino to raspberry pi get an error

Sun Jul 17, 2016 8:58 am

Hello,
at one point I am wrong: sending from arduino is not suspect to overflow. Just doublechecked and lost no data. But on receiver side, this can happen. But your data rate is quite low, so unlikely.
The data received in a line are like
$2094.42 2364.42 2127.73 2041.16
which is checked by a regular expression like

Code: Select all

import re

expr = '[$]([0-9]+[.][0-9]+) ([0-9]+[.][0-9]+) ([0-9]+[.][0-9]+) ([0-9]+[.][0-9]+)'
p = re.compile (expr)

# just a sample
line = '$2094.42 2364.42 2127.73 2041.16'
m = p.match(line)
if m != None:
    
    print m.group(1)
    print m.group(2)
    print m.group(3)
    print m.group(4)
else:
    print "no match"    
Regards,
Gerhard

Return to “Python”