2

i have a problem extracting data from accelerometer sensor in raspberry pi using ADC ADS1115.

I'm using this code for inserting the data to 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()

The problem is when I run that script, the received data was duplicated, that script only getting data from the first data of accelerometer sensor and then insert it repeatedly.

This is what i get.

+-------+------------+------------+------------+------------+
| 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 |
+-------+------------+------------+------------+------------+

I need the real data from the sensor, if i use print, the data will correctly displaying, but when i inserting it to mysql, the data will be like that.

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48

2 Answers2

0

Move cursor initialization inside loop

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)
sr3z
  • 400
  • 1
  • 2
  • 10
  • could you add printf before curs.execute(..) and check result ? – sr3z Sep 05 '16 at 06:51
  • the result is duplicate, but when i try to run the script manually using command python script.py, the value on mysql is different, not duplicated. i try to using watch command and crontab to auto running script every second, but it slowing down the server. @sr3z – Andy Dufrenes Sep 05 '16 at 08:00
0

ads1115 just have one analog reader, you see 4 analog pin but all connected one analog reader by a multiplexer, so when you are trying to read pin0 to pin1 multiplexer unit needs time for transition. for little bit you must delay, may be 10 microseconds. you can see on datasheet ads1115 just read 860 on continious mode. continious mode mean that just you read one pin. if you read 4 pin on ads1115 YOU MUST HAVE wait