data-writer.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. rfdevice = None
  17. # pylint: disable=unused-argument
  18. def exithandler(signal, frame):
  19. rfdevice.cleanup()
  20. sys.exit(0)
  21. logging.basicConfig(level=logging.INFO)
  22. parser = argparse.ArgumentParser(description='Receives a decimal code via a 433/315MHz GPIO device')
  23. parser.add_argument('-g', dest='gpio', type=int, default=27,
  24. help="GPIO pin (Default: 27)")
  25. args = parser.parse_args()
  26. signal.signal(signal.SIGINT, exithandler)
  27. rfdevice = RFDevice(args.gpio)
  28. rfdevice.enable_rx()
  29. timestamp = None
  30. logging.info("Listening for codes on GPIO " + str(args.gpio))
  31. while True:
  32. if rfdevice.rx_code_timestamp != timestamp:
  33. timestamp = rfdevice.rx_code_timestamp
  34. code = rfdevice.rx_code
  35. codeType = code & 0xff
  36. value = (code >> 8) / 1000.
  37. U = 0
  38. I = 0
  39. if codeType == VOLTAGE or True:
  40. logging.info("Voltage")
  41. U = value
  42. client.write_points([
  43. {
  44. "measurement": "solar",
  45. "tags": {
  46. "type": "U"
  47. },
  48. "fields": {
  49. "value": U
  50. }
  51. }
  52. ], time_precision='ms')
  53. if codeType == AMPERE:
  54. logging.info("Ampere")
  55. I = value
  56. logging.info(str(code) + "/" + str(codeType) + ", U=" + str(U))
  57. time.sleep(0.01)
  58. rfdevice.cleanup()