log_veml6070.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # import ADC
  7. sensor="veml6070"
  8. pathname = os.path.dirname(sys.argv[0])
  9. abspath=os.path.abspath(pathname)
  10. idfile=abspath+"/id.json"
  11. id_conf=0
  12. try:
  13. idf=open(idfile,"r")
  14. id_conf=json.load(idf)['id']
  15. idf.close()
  16. except:
  17. id_conf=0
  18. configfile=abspath+"/config.json"
  19. try:
  20. cf=open(configfile,"r")
  21. except:
  22. cf=open(configfile+".template","r")
  23. log_conf=json.load(cf)
  24. cf.close()
  25. parameter={"device":socket.gethostname(),"deviceid":"FF","mean_count":5,"ring_length":10,"wait":0.5,"cycle":10,"check_last":15,"check_last_shrinking":0.9,"gpg_keyid":"","digits":3,"store_dir":"/home/pi/log/","multiplicator":1000,"sensor":"VEML6070","sigma":3}
  26. for n in parameter:
  27. if n in log_conf:
  28. parameter[n]=log_conf[n]
  29. bveml=False
  30. if sensor in log_conf:
  31. bveml=True
  32. if "enable" in log_conf[sensor]:
  33. if log_conf[sensor]['enable'] == 0:
  34. bveml=False
  35. if bveml:
  36. import veml6070
  37. parameter["i2c"]=1
  38. if "port" in log_conf[sensor]:
  39. parameter["i2c"]=int(log_conf[sensor]['port'])
  40. try:
  41. veml = veml6070.Veml6070(i2c_bus=parameter["i2c"])
  42. except:
  43. bveml=False
  44. else:
  45. tsl_sigma=parameter['sigma']
  46. for n in parameter:
  47. if n in log_conf:
  48. parameter[n]=log_conf[n]
  49. if "sigma" in log_conf[sensor]:
  50. parameter['sigma']=int(log_conf[sensor]['sigma'])
  51. if parameter['sigma'] < 1:
  52. parameter['sigma']=1
  53. parameter["var_name"]="uv"
  54. tsl_data=meas_data(parameter)
  55. bmqtt=False
  56. if "mqtt" in log_conf:
  57. bmqtt=True
  58. lcmq=log_conf['mqtt']
  59. mbroker=""
  60. if 'broker' in lcmq:
  61. mbroker=lcmq['broker']
  62. else:
  63. bmqtt=False
  64. mport=1883
  65. if 'port' in lcmq:
  66. mport=lcmq['port']
  67. if bmqtt:
  68. tsl_data.set_mqtt(broker=mbroker,port=mport)
  69. if "sqlserver" in log_conf:
  70. hostname="localhost"
  71. port=8080
  72. tsl_data.set_sql(host=hostname,port=port)
  73. if "opensensemap" in log_conf[sensor]:
  74. osm=log_conf['veml6070']['opensensemap']
  75. if "sensebox_id" in osm:
  76. sid=osm['sensebox_id']
  77. if "sensors" in osm:
  78. if "uv" in osm['sensors']:
  79. tsl_data.set_sensebox(sid,osm['sensors']['uv'])
  80. if bveml:
  81. while True:
  82. # read bme280 (temperature, humidity, pressure)
  83. veml.set_integration_time(veml6070.INTEGRATIONTIME_1T)
  84. tsl_data.append(veml.get_uva_light_intensity())
  85. time.sleep(parameter['wait'])
  86. # close the client
  87. print("done")