【问题标题】:Python 3: Cannot insert values received from Arduino into MySQLPython 3:无法将从 Arduino 接收到的值插入 MySQL
【发布时间】:2020-03-11 21:45:43
【问题描述】:

希望你们一切都好。

我有一个问题,我已经尝试解决了很长时间,但没有成功。

我编写了这个 Arduino 脚本,它读取 DHT11 传感器注册的值并将它们打印到串行。代码如下:


#include <dht11.h>
#define DHT11PIN 2

dht11 DHT11;

void setup()
{
  Serial.begin(115200);

}

void loop()
{
  Serial.println();

  int chk = DHT11.read(DHT11PIN);
  float h = DHT11.humidity; //read humidity
  float t = DHT11.temperature; //read temperature (C)

  Serial.print(h);
  Serial.print("%");
  Serial.print(t);

  delay(2000);

}

我的目的是将上述数据打印在串行上,并将其发送到 python 代码,然后将接收到的数据插入到我创建的 MySQL 数据库中。在接收部分之前一切似乎都运行良好,但是当我尝试将接收到的数据插入我的数据库时,无论我如何处理数据,我都会遇到各种不同的错误。

首先,当我尝试使用下面的代码时:


#!/usr/bin/env python3

import serial
import mysql.connector as connector
import re

#the following have been changed so that I don't expose the real details

__dsn = {
            "host": "localhost",
            "user": "user",
            "passwd": "passwd",
            "db": "sensor"
        }

dbConn = connector.connect(**__dsn)

#open a cursor to the database
cursor = dbConn.cursor()

ser = serial.Serial('/dev/ttyACM0', 115200)
humidity = 0
temperature = 0

while 1:

        #data = ser.read()
        data = ser.read(1)
        n = ser.inWaiting()

        if n:

            data = data + ser.read(n)

        data = data.decode("UTF-8")

        try:

            pieces = data.split('%')  #split the data by the tab

            humidity = pieces[0]
            temperature = pieces[1]



        except IndexError:
            pass

        a = humidity
        b = temperature


        cursor.execute("INSERT INTO environment(humidity,temperature) VALUES (%s,%s)", (a,b))
        dbConn.commit()
        cursor.close()


我收到此错误:

我也尝试将湿度和温度值都更改为浮点类型,但如果这样做,我会收到以下错误:

我不确定这里发生了什么。我尝试以多种方式处理数据。以下是我迄今为止尝试使用的数据处理方法的示例:



        humidity.replace(" ", "")
        temperature.replace(" ", "")

        a = float(humidity).notnull()
        b = float(temperature).notnull()

        float(re.sub(r'[^0-9\.]', '', a))
        float(re.sub(r'[^0-9\.]', '', b))

再一次,没有成功...

如果有人能指出我正确的方向,我将不胜感激。

谢谢

罗伯

【问题讨论】:

  • 您似乎在发送数字数据,但随后您尝试在 python (data.decode("UTF-8")) 中将其视为字符串。这对我来说听起来不对!
  • 您是否还可以添加您尝试插入数据库的值以及环境的创建表
  • 如果您将print(data) 放在data = data.decode("UTF-8") 的正下方,我想您会立即看到问题所在...
  • 嗨。感谢您的所有回答。我一直在尝试根据您的建议修复我的代码。当我从代码中删除 data.decode("UTF-8) 行时,我收到以下错误:“不需要'str'”。Arduino 发送一个“%”符号以及值被视为可能是问题所在的字符串。我将不胜感激任何进一步的建议。非常感谢你们。

标签: python mysql arduino raspberry-pi


【解决方案1】:

我只是“解决”了这个问题。我注意到 Arduino 偶尔会发送一个带有奇怪字符的字符串,该字符串附加到实际数字(如 54.00)或缺少数字的一部分(而不是发送 54.00,而是发送 4.00)。这主要发生在湿度值上,温度读数几乎总是正确的。

我不确定这是为什么(可能是硬件问题),但我解决该问题的方法是在代码中添加以下内容:

'''

    try:
        float(humidity)
    except:
        humidity = 0.0

    try:
        float(temperature)
    except:
        temperature = 0.0

'''

因此,每个错误都将被解释为只是 Arduino 发送了一个“0.0”值。如果有人知道更清洁的修复方法,我仍然可以使用一些建议。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 2012-02-21
    • 1970-01-01
    • 2018-02-01
    • 2018-01-03
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多