AndrejB
Posts: 2
Joined: Wed Feb 20, 2019 8:30 am

ADS1115+GPIO+SQL

Thu Feb 21, 2019 6:59 pm

Hello!

I use following code to read ADS1115 chip and write data to SQL table. This works well. Maybe someone can help me to add GPIO pin status logging function to code above? Just read logic state (high or low) of GPIO pin and write number 1 (high) or 0 (low) to SQL column named pin1, pin2, etc. For GPIO access we can use gpiozero or RPi.GPIO libraries.

Many Thanks
Andrej

Code: Select all

######################################
#
# readADS1115Data 
#
#
######################################

import gc
import sys
from datetime import datetime
import MySQLdb as mdb

from MADS1x15 import ADS1x15

ADS1115tableName = 'archive'

# Initialise the ADC using the default mode (use default I2C address)
ADS1115 = 0x01  # 16-bit ADC
ads1115 = ADS1x15(ic=ADS1115)


def readADS1115Data(password):
    	print('readADS1115Data - The time is: %s' % datetime.now())


	# open database
	con = mdb.connect('localhost', 'weewx', password, 'weewx' )
	cur = con.cursor()



        gain = 6144  # +/- 6.144V

        # Select the sample rate
        sps = 250  # 250 samples per second


  	print "------------------------------"
	Voltage = []

 	for x in range (0,4):
	    	Voltage.append(ads1115.readADCSingleEnded(x, gain, sps)/500)

  
	#
	# Now put the data in MySQL
	# 
        # Put record in MySQL

        print "writing SQLdata ";


        # write record
        units = 1
	gap = 5 
        query = 'INSERT INTO '+ADS1115tableName+('(dateTime, usUnits, `interval`, ads_ch1, ads_ch2, ads_ch3, ads_ch4) VALUES(UNIX_TIMESTAMP(),  %i, %i, %.3f, %.3f, %.3f, %.3f)' %( units, gap, Voltage[0], Voltage[1], Voltage[2], Voltage[3] ))
        print("query=%s" % query)

        cur.execute(query)	
	con.commit()
	

Return to “Python”