Guest User

Untitled

a guest
Mar 7th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from __future__ import print_function
  3. import argparse
  4. import binascii
  5. import os
  6. import sys
  7. from bluepy import btle
  8. import paho.mqtt.client as mqtt
  9.  
  10. MISCALE_MAC = '88:0f:XX:XX:XX:XX'
  11. MQTT_USERNAME = ''
  12. MQTT_PASSWORD = ''
  13. MQTT_HOST = '192.168.XXX.XXX'
  14. MQTT_PORT = 1883
  15. MQTT_TIMEOUT = 60
  16.  
  17. if os.getenv('C', '1') == '0':
  18. ANSI_RED = ''
  19. ANSI_GREEN = ''
  20. ANSI_YELLOW = ''
  21. ANSI_CYAN = ''
  22. ANSI_WHITE = ''
  23. ANSI_OFF = ''
  24. else:
  25. ANSI_CSI = "\033["
  26. ANSI_RED = ANSI_CSI + '31m'
  27. ANSI_GREEN = ANSI_CSI + '32m'
  28. ANSI_YELLOW = ANSI_CSI + '33m'
  29. ANSI_CYAN = ANSI_CSI + '36m'
  30. ANSI_WHITE = ANSI_CSI + '37m'
  31. ANSI_OFF = ANSI_CSI + '0m'
  32.  
  33.  
  34. class ScanProcessor():
  35.  
  36. def __init__(self):
  37. self.mqtt_client = None
  38. self.connected = False
  39. self._start_client()
  40.  
  41. def handleDiscovery(self, dev, isNewDev, isNewData):
  42. if dev.addr == MISCALE_MAC.lower() and isNewDev:
  43. print (' Device: %s (%s), %d dBm %s. ' %
  44. (
  45. ANSI_WHITE + dev.addr + ANSI_OFF,
  46. dev.addrType,
  47. dev.rssi,
  48. ('' if dev.connectable else '(not connectable)'))
  49. , end='')
  50. for (sdid, desc, data) in dev.getScanData():
  51. if data.startswith('1d18') and sdid == 22:
  52. measunit = data[4:6]
  53. measured = int((data[8:10] + data[6:8]), 16) * 0.01
  54. unit = ''
  55.  
  56. if measunit.startswith(('03', 'b3')): unit = 'lbs'
  57. if measunit.startswith(('12', 'b2')): unit = 'jin'
  58. if measunit.startswith(('22', 'a2')): unit = 'kg' ; measured = measured / 2
  59.  
  60. if unit:
  61. print('')
  62. self._publish(round(measured, 2), unit)
  63. else:
  64. print("Scale is sleeping.")
  65.  
  66. if not dev.scanData:
  67. print ('\t(no data)')
  68. print
  69.  
  70. def _start_client(self):
  71. self.mqtt_client = mqtt.Client()
  72. self.mqtt_client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
  73.  
  74. def _on_connect(client, _, flags, return_code):
  75. self.connected = True
  76. print("MQTT connection returned result: %s" % mqtt.connack_string(return_code))
  77.  
  78. self.mqtt_client.on_connect = _on_connect
  79.  
  80. self.mqtt_client.connect(MQTT_HOST, MQTT_PORT, MQTT_TIMEOUT)
  81. self.mqtt_client.loop_start()
  82.  
  83. def _publish(self, weight, unit):
  84. if not self.connected:
  85. raise Exception('not connected to MQTT server')
  86. prefix = '{}/{}'.format('miscale/weight', unit)
  87. self.mqtt_client.publish(prefix, weight, qos=1, retain=True)
  88. print('\tSent data to topic %s: %s %s' % (prefix, weight, unit))
  89.  
  90.  
  91. def main():
  92.  
  93. scanner = btle.Scanner().withDelegate(ScanProcessor())
  94.  
  95. print (ANSI_RED + "Scanning for devices..." + ANSI_OFF)
  96. devices = scanner.scan(5)
  97.  
  98. if __name__ == "__main__":
  99. main()
Add Comment
Please, Sign In to add comment