[SOLVED] Read data serial arduino to raspberry pi

Hello, i still have error while read data serial arduino to raspberry pi. This the error

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 code

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

Hi, @alfendo

The error seems to be on the line:

The third parameter of this line is ‘25.60’ and python can’t convert to integer from a string with a point, so you should change that line by:

int(float(sensorVar[3]))

This will convert the value to float first and then put as integer.

However I see that in the last line of the data Raw receives five parameters, is this okay?

Gustavo.

thanks for reply @woakas , so the error is converting value ? actually in arduino i alr define flowvar value as integer
yes in last line where value 0 in last line come from? bit confused to me

Hi @alfendo,

Yes, the error is converting value. you should replace the line:

 flowvar = int(sensorVar[3])

by

 flowvar = int(float(sensorVar[3]))

The reason is that Python can not convert ‘34.2’ (string) to an integer value you need first convert a float.

@alfendo Actually i am working on the same project you ask the code. but there is a problem arsing i want to send 5 to 6 sensor values to ubidots server so i found you way but when i am trying your method i am only getting Raw value.
what code did you write in arduino because i think the problem is arising there