log_ads1x15.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import os, serial,time,socket,sys,json
  4. from meas_data import meas_data
  5. # import the server implementation
  6. sensor="ads1x15"
  7. pathname = os.path.dirname(sys.argv[0])
  8. abspath=os.path.abspath(pathname)
  9. idfile=abspath+"/id.json"
  10. id_conf=0
  11. try:
  12. idf=open(idfile,"r")
  13. id_conf=json.load(idf)['id']
  14. idf.close()
  15. except:
  16. id_conf=0
  17. configfile=abspath+"/config.json"
  18. try:
  19. cf=open(configfile,"r")
  20. except:
  21. cf=open(configfile+".template","r")
  22. log_conf=json.load(cf)
  23. cf.close()
  24. parameter={"device":socket.gethostname(),"deviceid":"FF","mean_count":5,"ring_length":10,"wait":0.5,"cycle":10,"check_last":50,"check_last_shrinking":0.9,"gpg_keyid":"","digits":4,"store_dir":"/home/pi/log/","multiplicator":1,"sensor":sensor,"sigma":3}
  25. for n in parameter:
  26. if n in log_conf:
  27. parameter[n]=log_conf[n]
  28. bsensor=False
  29. if sensor in log_conf:
  30. if "enable" in log_conf[sensor]:
  31. if log_conf[sensor]["enable"] == 1:
  32. bsensor=True
  33. # declare adc
  34. badc=False
  35. channel_info={}
  36. GAIN=1
  37. if bsensor:
  38. badc=True
  39. ads_conf=log_conf['ads1x15']
  40. if "enable" in ads_conf:
  41. for n in parameter:
  42. if n in log_conf['ads1x15']:
  43. parameter[n]=log_conf['ads1x15'][n]
  44. if ads_conf['enable'] == 0:
  45. badc=False
  46. bmqtt=False
  47. if "mqtt" in log_conf:
  48. bmqtt=True
  49. lcmq=log_conf['mqtt']
  50. mbroker=""
  51. if 'broker' in lcmq:
  52. mbroker=lcmq['broker']
  53. else:
  54. bmqtt=False
  55. mport=1883
  56. if 'port' in lcmq:
  57. mport=lcmq['port']
  58. if "sqlserver" in log_conf:
  59. hostname="localhost"
  60. port=8080
  61. if badc:
  62. adc={}
  63. GAIN=1
  64. for n in parameter:
  65. if n in ads_conf:
  66. parameter[n]=ads_conf[n]
  67. if "gain" in ads_conf:
  68. GAIN=ads_conf['gain']
  69. if "adc" in ads_conf:
  70. tsl_sigma=parameter['sigma']
  71. if "sigma" in log_conf['ads1x15']:
  72. tsl_sigma=int(log_conf['ads1x15']['sigma'])
  73. if tsl_sigma < 1:
  74. tsl_sigma=1
  75. parameter["sigma"]=tsl_sigma
  76. from Adafruit_ADS1x15 import ADS1115
  77. from Adafruit_ADS1x15 import ADS1015
  78. adc_count=0
  79. tadc=ads_conf['adc']
  80. for x in tadc:
  81. varpar=parameter
  82. y=tadc[x]
  83. adc_address=0x48
  84. if "address" in y:
  85. adc_address=int(y['address'],16)
  86. abc_bus=1
  87. if "busnum" in y:
  88. adc_bus=y['busnum']
  89. adc_type=1015
  90. if "type" in y:
  91. adc_type=y['type']
  92. adc_assigned=False
  93. if adc_type == 1115:
  94. try:
  95. adc[adc_count]=ADS1115(address=adc_address,busnum=adc_bus)
  96. adc_assigned=True
  97. except:
  98. print("could not assign ADC")
  99. if adc_type == 1015:
  100. try:
  101. adc[adc_count]=ADS1015(address=adc_address,busnum=adc_bus)
  102. adc_assigned=True
  103. except:
  104. print("could not assign ADC")
  105. varpar["sensor"]="ads"+str(adc_type)
  106. varpar["i2c"]=adc_address
  107. if adc_assigned:
  108. for j in range(4):
  109. cnadc="a"+str(adc_count)+"_"+str(j)
  110. cnadcs="a"+"_"+str(j)
  111. # channel_names.append(cnadc)
  112. varrpar=varpar
  113. varrpar["var_name"]=cnadcs
  114. channel_info[cnadc]={"meas_data": meas_data(varrpar)}
  115. if bmqtt and ("mqtt" in log_conf['ads1x15']):
  116. channel_info[cnadc]['meas_data'].set_mqtt(broker=mbroker,port=mport)
  117. if "sqlserver" in log_conf:
  118. channel_info[cnadc]['meas_data'].set_sql(host=hostname,port=port)
  119. adc_count=adc_count+1
  120. else:
  121. badc=False
  122. if badc:
  123. while True:
  124. for i in adc:
  125. cname="a"+str(i)+"_{0}"
  126. for j in range(4):
  127. channel_info[cname.format(j)]['meas_data'].append(adc[i].read_adc(j,gain=GAIN))
  128. time.sleep(parameter['wait'])
  129. # close the client
  130. print("done")