arduino-daemon.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #! /usr/bin/python3
  2. # haha, "daemon" :D
  3. # aquí iré metiendo la lectura de datos y guardado en algún sitio
  4. # alias arduinousb='sudo chmod a+rw /dev/ttyACM0'
  5. import serial
  6. import influxdb
  7. import time
  8. import os.path
  9. while True:
  10. try:
  11. influx = influxdb.InfluxDBClient("localhost", 8086, "root", "root", "hometv")
  12. ser = serial.Serial("/dev/ttyACM0" if os.path.exists("/dev/ttyACM0") else "/dev/ttyACM1", 9600) # 9600 bauds
  13. while True:
  14. line = ser.readline()
  15. parts = line.decode('ascii').strip().split(" ")
  16. print(time.ctime(), line, parts)
  17. if parts[0] != "D":
  18. continue
  19. influx.write_points([
  20. {
  21. "measurement": "humedad",
  22. "tags": {
  23. "location": "home"
  24. },
  25. "fields": {
  26. "v": float(parts[1][2:])
  27. }
  28. },
  29. {
  30. "measurement": "temperatura",
  31. "tags": {
  32. "location": "home"
  33. },
  34. "fields": {
  35. "real": float(parts[2][2:]),
  36. "sensacion": float(parts[3][2:])
  37. }
  38. },
  39. {
  40. "measurement": "luminosidad",
  41. "tags": {
  42. "location": "home"
  43. },
  44. "fields": {
  45. "avg": float(parts[4][3:]),
  46. "min": float(parts[5][3:]),
  47. "max": float(parts[6][3:])
  48. }
  49. }
  50. ])
  51. except Exception as e:
  52. print(e)
  53. time.sleep(60)