log_mcp9808.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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="mcp9808"
  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":"MCP9808","sigma":3}
  25. for n in parameter:
  26. if n in log_conf:
  27. parameter[n]=log_conf[n]
  28. bmcp9808=False
  29. if sensor in log_conf:
  30. bmcp9808=True
  31. if "enable" in log_conf[sensor]:
  32. if log_conf[sensor]['enable'] == 0:
  33. bmcp9808=False
  34. if bmcp9808:
  35. import Adafruit_MCP9808.MCP9808 as MCP9808
  36. try:
  37. sens_9808 = MCP9808.MCP9808()
  38. except:
  39. bmcp9808 = False
  40. else:
  41. sens_9808.begin()
  42. mcp_sigma=parameter['sigma']
  43. if "sigma" in log_conf[sensor]:
  44. parameter['sigma']=int(log_conf[sensor]['sigma'])
  45. if parameter['sigma'] < 1:
  46. parameter['sigma']=1
  47. parameter["i2c"]=0
  48. parameter["var_name"]="temperature"
  49. mcp_data=meas_data(parameter)
  50. if "sqlserver" in log_conf:
  51. hostname="localhost"
  52. port=8080
  53. mcp_data.set_sql(host=hostname,port=port)
  54. if "opensensemap" in log_conf[sensor]:
  55. osm=log_conf[sensor]['opensensemap']
  56. if "sensebox_id" in osm:
  57. sid=osm['sensebox_id']
  58. if "sensors" in osm:
  59. if "temperature" in osm['sensors']:
  60. mcp_data.set_sensebox(sid,osm['sensors']['temperature'])
  61. bmqtt=False
  62. if "mqtt" in log_conf:
  63. bmqtt=True
  64. lcmq=log_conf['mqtt']
  65. mbroker=""
  66. if 'broker' in lcmq:
  67. mbroker=lcmq['broker']
  68. else:
  69. bmqtt=False
  70. mport=1883
  71. if 'port' in lcmq:
  72. mport=lcmq['port']
  73. if bmqtt:
  74. mcp_data.set_mqtt(broker=mbroker,port=mport)
  75. if bmcp9808:
  76. while True:
  77. # read bme280 (temperature, humidity, pressure)
  78. mcp_data.append(sens_9808.readTempC())
  79. time.sleep(parameter['wait'])
  80. # close the client
  81. print("done")