log_bme280.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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="bme280"
  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":15,"check_last_shrinking":0.9,"gpg_keyid":"","digits":3,"store_dir":"/home/pi/log/","multiplicator":1000,"sensor":"BME280","sigma":3}
  25. for n in parameter:
  26. if n in log_conf:
  27. parameter[n]=log_conf[n]
  28. bmqtt=False
  29. if "mqtt" in log_conf:
  30. bmqtt=True
  31. lcmq=log_conf['mqtt']
  32. mbroker=""
  33. if 'broker' in lcmq:
  34. mbroker=lcmq['broker']
  35. else:
  36. bmqtt=False
  37. mport=1883
  38. if 'port' in lcmq:
  39. mport=lcmq['port']
  40. # config of bme280 sensor
  41. # use pip install RPi.bme280 for library
  42. meas_types=["temperature","humidity","pressure"]
  43. bbme=False
  44. if sensor in log_conf:
  45. bbme=True
  46. if "enable" in log_conf[sensor]:
  47. if log_conf[sensor]['enable'] == 0:
  48. bbme=False
  49. if bbme:
  50. import smbus2
  51. import bme280
  52. if "port" in log_conf[sensor]:
  53. bme_port=log_conf[sensor]['port']
  54. else:
  55. bme_port=1
  56. if "address" in log_conf[sensor]:
  57. parameter["i2c"]=int(log_conf[sensor]['address'],16)
  58. else:
  59. parameter["i2c"]=0x77
  60. try:
  61. bme_bus=smbus2.SMBus(bme_port)
  62. except:
  63. bbme=False
  64. else:
  65. calibration_params=bme280.load_calibration_params(bme_bus,parameter["i2c"])
  66. bme_sigma=3
  67. for n in parameter:
  68. if n in log_conf[sensor]:
  69. parameter[n]=log_conf[sensor][n]
  70. if "sigma" in log_conf[sensor]:
  71. parameter["sigma"]=int(log_conf[sensor]['sigma'])
  72. if parameter["sigma"] < 1:
  73. parameter["sigma"]=1
  74. bme_cycle=5
  75. if "cycle" in log_conf[sensor]:
  76. bme_cycle=int(log_conf[sensor]['cycle'])
  77. partemp=parameter
  78. partemp["var_name"]="temperature"
  79. bme_temp=meas_data(partemp)
  80. parhum=parameter
  81. parhum["var_name"]="humidity"
  82. bme_hum=meas_data(parhum)
  83. parpress=parameter
  84. parpress["var_name"]="pressure"
  85. bme_press=meas_data(parpress)
  86. if ("mqtt" in log_conf[sensor]) and bmqtt:
  87. bme_temp.set_mqtt(broker=mbroker,port=mport)
  88. bme_hum.set_mqtt(broker=mbroker,port=mport)
  89. bme_press.set_mqtt(broker=mbroker,port=mport)
  90. if "sqlserver" in log_conf:
  91. hostname="localhost"
  92. # if "host" in log_conf['sqlserver']:
  93. # hostname=log_conf['sqlserver']['host']
  94. port=8080
  95. # if "port" in log_conf['sqlserver']:
  96. # port=int(log_conf['sqlserver']['port'])
  97. bme_temp.set_sql(host=hostname,port=port)
  98. bme_hum.set_sql(host=hostname,port=port)
  99. bme_press.set_sql(host=hostname,port=port)
  100. if "opensensemap" in log_conf[sensor]:
  101. osm=log_conf[sensor]['opensensemap']
  102. if "sensebox_id" in osm:
  103. sid=osm['sensebox_id']
  104. if "sensors" in osm:
  105. if "temperature" in osm['sensors']:
  106. bme_temp.set_sensebox(sid,osm['sensors']['temperature'])
  107. if "humidity" in osm['sensors']:
  108. bme_hum.set_sensebox(sid,osm['sensors']['humidity'])
  109. if "pressure" in osm['sensors']:
  110. bme_press.set_sensebox(sid,osm['sensors']['pressure'])
  111. if bbme:
  112. while True:
  113. # read bme280 (temperature, humidity, pressure)
  114. bme_data=bme280.sample(bme_bus,parameter["i2c"],calibration_params)
  115. bme_temp.append(bme_data.temperature)
  116. bme_hum.append(bme_data.humidity)
  117. bme_press.append(bme_data.pressure)
  118. time.sleep(parameter['wait'])
  119. # close the client
  120. print("done")