data-writer.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import time
  2. import random
  3. from influxdb import InfluxDBClient
  4. client = InfluxDBClient(host="localhost", port=8086, username="influxdb", password="influxdbTSGAMES")
  5. client.create_database("influxdb")
  6. client.switch_database('influxdb')
  7. #!/usr/bin/env python3
  8. import argparse
  9. import signal
  10. import sys
  11. import time
  12. import logging
  13. from rpi_rf import RFDevice
  14. VOLTAGE = 0
  15. AMPERE = 1
  16. # min samples in a row stable to be accepted as new value
  17. STABLE_COUNT = 3
  18. # max diff which is considered stable between new values
  19. STABLE_DIFF_THRESHOLD = .1
  20. rfdevice = None
  21. # pylint: disable=unused-argument
  22. def exithandler(signal, frame):
  23. rfdevice.cleanup()
  24. sys.exit(0)
  25. logging.basicConfig(level=logging.INFO)
  26. parser = argparse.ArgumentParser(description='Receives a decimal code via a 433/315MHz GPIO device')
  27. parser.add_argument('-g', dest='gpio', type=int, default=27,
  28. help="GPIO pin (Default: 27)")
  29. args = parser.parse_args()
  30. signal.signal(signal.SIGINT, exithandler)
  31. rfdevice = RFDevice(args.gpio, rx_tolerance=80)
  32. rfdevice.enable_rx()
  33. timestamp = None
  34. logging.info("Listening for codes on GPIO " + str(args.gpio))
  35. previous_values = {}
  36. counts = {}
  37. while True:
  38. if rfdevice.rx_code_timestamp != timestamp:
  39. timestamp = rfdevice.rx_code_timestamp
  40. code = rfdevice.rx_code
  41. codeType = code & 0xff
  42. value = (code >> 8) / 1000.
  43. U = 0
  44. I = 0
  45. if value:
  46. if codeType in previous_values and abs(previous_values[codeType]-value) <= STABLE_DIFF_THRESHOLD:
  47. counts[codeType] += 1
  48. else:
  49. counts[codeType] = 0
  50. previous_values[codeType] = value
  51. logging.info("Data Received: " + str(code) + "/" + str(codeType) + " / " + str(value))
  52. if codeType in counts and counts[codeType] >= STABLE_COUNT:
  53. if codeType == VOLTAGE or True:
  54. U = value
  55. logging.info("Stable Voltage:" +str(U))
  56. client.write_points([
  57. {
  58. "measurement": "solar",
  59. "tags": {
  60. "type": "U"
  61. },
  62. "fields": {
  63. "value": U
  64. }
  65. }
  66. ], time_precision='ms')
  67. if codeType == AMPERE:
  68. logging.info("Ampere")
  69. I = value
  70. time.sleep(0.05)
  71. rfdevice.cleanup()