Program read the data from sensor via ADC and Pi send it to database. There is one I2C protocols that communicate between PI and ADC.
Pi sending data to another PC via wifi router with internet connection. I don't understand which type of protocol being used in program when PI talk to other device like PC
Code: Select all
# Author: Tony DiCola
# License: Public Domain
# Import the ADS1x15 module.
import Adafruit_ADS1x15
import MySQLdb
import time
import datetime
import pymssql
# 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.
sensor1 = [0]*4
sensor2 = [0]*4
sensor3 = [0]*4
sensor4 = [0]*4
for i in range(4):
sensor1[i] = adc.start_adc(i, gain=GAIN)
sensor2[i] = adc.start_adc(i, gain=GAIN)
sensor3[i] = adc.start_adc(i, gain=GAIN)
sensor4[i] = adc.start_adc(i, gain=GAIN)
connobj = pymssql.connect(server='192.168.1.240', user='jay', password='jay', database='tempdb', port='1433')
cursor = connobj.cursor()
cursor.execute('SELECT * FROM temp_table')
while True:
for row in cursor:
print('row = %r' % (row,))
print "voltage"
#curs=db.cursor()
cursor.execute("""INSERT INTO temp_table(data1, data2, data3, data4)
values(%s,%s,%s,%s)""",(sensor1[i],sensor2[i],sensor3[i],sensor4[i]))
connobj.commit()
print sensor1[i]
print sensor2[i]
print sensor3[i]
print sensor4[i]
cursor.execute('SELECT * FROM temp_table')
for row in cursor:
print('row = %r' % (row,))
time.sleep(0.5)
connobj.close()