我在使用ADC ADS1115从树莓派中的加速计传感器中提取数据时遇到问题。
我正在使用此代码将数据插入mysql
# Author: Tony DiCola
# License: Public Domain
# Import the ADS1x15 module.
import Adafruit_ADS1x15
import MySQLdb
import time
import datetime
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()
# Or create an ADS1015 ADC (12-bit) instance.
#adc = Adafruit_ADS1x15.ADS1015()
# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:
#adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)
# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
# - 2/3 = +/-6.144V
# - 1 = +/-4.096V
# - 2 = +/-2.048V
# - 4 = +/-1.024V
# - 8 = +/-0.512V
# - 16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1
time_sensor = time.time()
# Main loop.
x = [1]*4
y = [2]*4
z = [3]*4
for i in range(4):
x[i] = adc.start_adc(i, gain=GAIN)
y[i] = adc.start_adc(i, gain=GAIN)
z[i] = adc.start_adc(i, gain=GAIN)
# Read the specified ADC channel using the previously set gain value.
# Once continuous ADC conversions are started you can call get_last_result() to
db = MySQLdb.connect("localhost", "root", "raspberry", "sensor_log")
curs=db.cursor()
while True:
try:
curs.execute("""INSERT INTO table_sensor_log(time, koordinatx, koordinaty, koordinatz)
values(%s,%s,%s,%s)""",(time_sensor,x[i],y[i],z[i]))
db.commit()
except:
print "Error"
db.rollback()
time.sleep(1)
db.close()
问题是当我运行该脚本时,接收到的数据是重复的,该脚本只从加速计传感器的第一个数据中获取数据,然后重复插入。
这就是我得到的。
+-------+------------+------------+------------+------------+
| id | time | koordinatX | koordinatY | koordinatZ |
+-------+------------+------------+------------+------------+
| 24743 | 1472624612 | 15443 | 20351 | 20454 |
| 24744 | 1472624612 | 15443 | 20351 | 20454 |
| 24745 | 1472624612 | 15443 | 20351 | 20454 |
| 24746 | 1472624612 | 15443 | 20351 | 20454 |
| 24747 | 1472624612 | 15443 | 20351 | 20454 |
| 24748 | 1472624612 | 15443 | 20351 | 20454 |
+-------+------------+------------+------------+------------+
我需要来自传感器的真实数据,如果我使用print,数据将正确显示,但当我将其插入mysql时,数据将是这样的。
答案 0 :(得分:0)
在游标内部移动游标初始化
while True:
try:
curs=db.cursor()
curs.execute("""INSERT INTO table_sensor_log(time, koordinatx, koordinaty, koordinatz)
values(%s,%s,%s,%s)""",(time_sensor,x[i],y[i],z[i]))
db.commit()
curs.close()
except:
print "Error"
db.rollback()
time.sleep(1)
答案 1 :(得分:0)
ads1115只有一个模拟阅读器,您看到4个模拟引脚,但是全部通过一个多路复用器连接到一个模拟阅读器, 因此,当您尝试将pin0读取到pin1时,多路复用器单元需要一定的过渡时间。您必须延迟一点点时间,可能是10微秒。 您可以在数据表上看到ad1115在连续模式下仅读取860。连续模式意味着您只需读取一个图钉。如果您在ads1115上阅读了4针,则必须等待