为什么ValueError:无法将字符串转换为float:?

时间:2015-10-22 19:07:39

标签: python arduino raspberry-pi

我实际上是想创建一个情节图表, 我有一个Arduino nano通过usb串行输出将DHT22(温度/嗡嗡声传感器)数据发送到Raspberry Pi,python脚本应该读取数据并将其流式传输到plot.ly

Arduino草图:

#include "DHT.h"
#define MAX_ITER 10
#define DHTTYPE DHT22
DHT dht2(2, DHTTYPE);
int startbyte;
int i;
float auxTemp;
float auxHum;
float meanHum[3] = {0.0, 0.0, 0.0};
float meanTemp[3] = {0.0, 0.0, 0.0};
void setup()
{
  Serial.begin(9600);
  dht2.begin();
}
void loop()
{
  if (Serial.available() > 0)
  {
    startbyte = Serial.read();
    if (startbyte == 't')
    {
      meanHum[0] = 0.0;
      meanTemp[0] = 0.0;
      for (i=0;i<MAX_ITER;i++)
      {
        auxTemp = dht2.readTemperature();
        auxHum = dht2.readHumidity();
        meanTemp[0] += auxTemp/(float)MAX_ITER;
        meanHum[0] += auxHum/(float)MAX_ITER;
        delay(500);
      }
      Serial.print(meanTemp[0],4); Serial.print(",");
      Serial.print(meanHum[0],4); Serial.println("");
    }
  }
}

草图似乎没问题,并且在由“&#39;

”触发时返回温度和湿度

但是当我尝试运行这个python脚本时:

#!/usr/bin/env python

import serial
import time
import plotly.plotly as py
from plotly.graph_objs import *


arduinoFile = '/dev/ttyUSB0'
logFile = 'log.csv'
sensorOutput = [0.0, 0.0]

py.sign_in('#####','#####')
stream_ids=['#####','#####']

my_data = Data([
    Scatter(x=[], y=[],
         name='Sensor 1 temperature',
         mode='lines',
         line= Line(color='rgba(250,30,30,0.9)',width=2.5),
         stream=dict(token=stream_ids[0],maxpoints=900)),
    Scatter(x=[], y=[],
         name='Sensor 1 humidity',
         mode='lines',
         line= Line(color='rgba(30,30,250,0.3)',width=2.5),
         stream=dict(token=stream_ids[1],maxpoints=900), yaxis='y2')
    ])

my_layout = Layout(
    title='GrowTemp',
    xaxis=XAxis(
         showline=True,
         linecolor='#bdbdbd',
         title='Time',
         showticklabels=True),
    yaxis=YAxis(
         showline=True,
         linecolor='#bdbdbd',
         title='Temperature [*C]',
         showticklabels=True,
         titlefont=Font(size=18,color='#ee3333')),
    yaxis2=YAxis(
         showline=True,
         linecolor='#bdbdbd',
         title='Relative humidity [%]',
         titlefont=Font(size=18,color='#3366dd'),
         side='right',
         overlaying='y'),
    legend=Legend(x=0,y=1), showlegend=True
    )

my_fig = Figure(data=my_data, layout=my_layout)
py.plot(my_fig, filename='GrowTemp')
time.sleep(3)

s1temp = py.Stream(stream_ids[0])
s1hum = py.Stream(stream_ids[1])

ser = serial.Serial(arduinoFile, baudrate=9600, bytesize=8, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=10)
time.sleep(1)
ser.flushInput()

def gettemp():
    ser.write('t')
    ser.flush()
    return ser.readline().strip('\r').strip('\n')

timeStart = time.time()
while True:
    timeDelay = time.time()-timeStart
    timeStamp = time.strftime("%Y-%m-%d %H:%M:%S")
    sensorOutputString = gettemp()
    sensorOutputRaw = sensorOutputString.split(',')
    sensorOutput[0] = float(sensorOutputRaw[0]) 
    sensorOutput[1] = float(sensorOutputRaw[1]) 
    resultString = str(timeDelay)+','+timeStamp+','+str(sensorOutput[0])+','+str(sensorOutput[1])
    print(resultString)

    my_file = open(logFile,'a')
    my_file.write(resultString+'\n')
    my_file.close()
    s1temp.open()
    s1hum.open()
    s1temp.write(dict(x=timeStamp,y=sensorOutput[0]))
    s1hum.write(dict(x=timeStamp,y=sensorOutput[1]))
    s1temp.close()
    s1hum.close()
    time.sleep(50)

ser.close()

我有一个错误:

Traceback (most recent call last):
  File "plotlyscript.py", line 74, in <module>
    sensorOutput[0] = float(sensorOutputRaw[0])
ValueError: could not convert string to float:

我发现了几篇关于这个ValueError的帖子,但是我仍然无法弄清楚这里有什么错误(我是总共noob;))是因为字符串返回空值还是冲突以前的[0] [1]参数?

0 个答案:

没有答案