Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';t将传感器读数解析到postgres表中--类型错误_Python_Arduino - Fatal编程技术网

Python Can';t将传感器读数解析到postgres表中--类型错误

Python Can';t将传感器读数解析到postgres表中--类型错误,python,arduino,Python,Arduino,我有一个Arduino,它用以下代码输出一些传感器读数: Serial.print(PostDataTrue) + Serial.print(F(",")) + Serial.print(sensorValue) + Serial.print(F(",")) + Serial.print(hum) + Serial.print(F(",")) + Serial.println(temp); 它在串行监视器中显示如下: 00:04:33.301 -> TRUE,516,25.40,25.3

我有一个Arduino,它用以下代码输出一些传感器读数:

Serial.print(PostDataTrue) + Serial.print(F(",")) + Serial.print(sensorValue) + Serial.print(F(",")) + Serial.print(hum) + Serial.print(F(",")) + Serial.println(temp);
它在串行监视器中显示如下:

00:04:33.301 -> TRUE,516,25.40,25.30
00:04:34.268 -> TRUE,523,25.40,25.20
00:04:35.287 -> TRUE,522,25.40,25.20
我正在尝试将此数据插入postgres表。我首先测试了它——成功了!——通过读取真/假字符串,我得到了下面的Python脚本

然而,现在我已经在混合中引入了数字,我不断地得到这个错误:

reallymemorable:sensor-db-mediator reallymemorable$ python3 sensor_reader.py 
2020-02-21 00:00:15 TRUE 472 25.50 25.10

Traceback (most recent call last):
  File "sensor_reader.py", line 95, in <module>
    insert_data(date_time, sensor1, float(sensor2), float(sensor3), float(sensor4))
  File "sensor_reader.py", line 57, in insert_data
    cur.execute('''INSERT INTO lightbool
TypeError: not all arguments converted during string formatting

我想您需要另一个
%s
(%s,%s,%s)“,(dt,s1,s2,s3,s4)
中,或者从tuple@dcg就是这样。。。总是那样愚蠢
import serial
import psycopg2
import datetime
import time

# Initiate the serial connection to the Arduino device.
try:
    ser = serial.Serial('/dev/cu.usbmodem14101', 115200, timeout=1)
    # Brief pause to establish connection to port.
    time.sleep(3)
except Exception as e:
    print(e)
    exit(1)

def create_table():
    '''Create table in database.'''
    try:
        conn = psycopg2.connect('''
                                connection_string_here
                                ''')
        cur = conn.cursor()
        # Create table for database.
        cur.execute('''CREATE TABLE IF NOT EXISTS lightbool (
            id serial PRIMARY KEY,
            date_time TIMESTAMP NOT NULL,
            sensor1 TEXT NOT NULL,
            sensor2 REAL NOT NULL,
            sensor3 REAL NOT NULL,
            sensor4 REAL NOT NULL
            )''')
    except Exception as e:
        print(e)
        return
    conn.commit()
    conn.close()

create_table()

def insert_data(dt, s1, s2, s3, s4):
    '''Insert data into the database.'''
    conn = psycopg2.connect('''
                            connection_string_here
                            ''')
    cur = conn.cursor()
    cur.execute('''INSERT INTO lightbool
                (date_time, sensor1, sensor2, sensor3, sensor4)
                VALUES (%s, %s, %s, %s)''', (dt, s1, s2, s3, s4))
    conn.commit()
    conn.close()

def read_sensors():
    '''Read the sensors connected to Arduino device.'''
    global sensor1, sensor2, sensor3, sensor4
    # When the Arduino receives the character 'z' it will read the sensor data.
    ser.write(b'z')
    try:
        data = ser.readline().decode('utf-8')
        data = data.split(",")
        sensor1 = data[0]
        sensor2 = data[1]
        sensor3 = data[2]
        sensor4 = data[3]
        print("{} {} {} {}".format(sensor1, sensor2, sensor3, sensor4))
        return (sensor1, float(sensor2), float(sensor3), float(sensor4))
    except Exception as e:
        print(e)
        return

while True:
    # Get the current date and time from the datetime module.
    time_now = datetime.datetime.today()
    date_time = datetime.datetime.strftime(time_now, "%Y-%m-%d %H:%M:%S")
    data = ser.readline().decode('utf-8')
    data = data.split(",")
    sensor1 = data[0]
    sensor2 = data[1]
    sensor3 = data[2]
    sensor4 = data[3]
    print(date_time, sensor1, sensor2, sensor3, sensor4)
    # Save data in a Postgresql database.
    insert_data(date_time, sensor1, float(sensor2), float(sensor3), float(sensor4))
    # Pause a few seconds.
    time.sleep(1)

ser.close()