ubluepy_temp.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # This file is part of the MicroPython project, http://micropython.org/
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) 2017 Glenn Ruben Bakke
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE
  24. from board import LED
  25. from machine import RTCounter, Temp
  26. from ubluepy import Service, Characteristic, UUID, Peripheral, constants
  27. def event_handler(id, handle, data):
  28. global rtc
  29. global periph
  30. global serv_env_sense
  31. global notif_enabled
  32. if id == constants.EVT_GAP_CONNECTED:
  33. # indicated 'connected'
  34. LED(1).on()
  35. elif id == constants.EVT_GAP_DISCONNECTED:
  36. # stop low power timer
  37. rtc.stop()
  38. # indicate 'disconnected'
  39. LED(1).off()
  40. # restart advertisment
  41. periph.advertise(device_name="micr_temp", services=[serv_env_sense])
  42. elif id == constants.EVT_GATTS_WRITE:
  43. # write to this Characteristic is to CCCD
  44. if int(data[0]) == 1:
  45. notif_enabled = True
  46. # start low power timer
  47. rtc.start()
  48. else:
  49. notif_enabled = False
  50. # stop low power timer
  51. rtc.stop()
  52. def send_temp(timer_id):
  53. global notif_enabled
  54. global char_temp
  55. if notif_enabled:
  56. # measure chip temperature
  57. temp = Temp.read()
  58. temp = temp * 100
  59. char_temp.write(bytearray([temp & 0xFF, temp >> 8]))
  60. # start off with LED(1) off
  61. LED(1).off()
  62. # use RTC1 as RTC0 is used by bluetooth stack
  63. # set up RTC callback every 5 second
  64. rtc = RTCounter(1, period=50, mode=RTCounter.PERIODIC, callback=send_temp)
  65. notif_enabled = False
  66. uuid_env_sense = UUID("0x181A") # Environmental Sensing service
  67. uuid_temp = UUID("0x2A6E") # Temperature characteristic
  68. serv_env_sense = Service(uuid_env_sense)
  69. temp_props = Characteristic.PROP_NOTIFY | Characteristic.PROP_READ
  70. temp_attrs = Characteristic.ATTR_CCCD
  71. char_temp = Characteristic(uuid_temp, props = temp_props, attrs = temp_attrs)
  72. serv_env_sense.addCharacteristic(char_temp)
  73. periph = Peripheral()
  74. periph.addService(serv_env_sense)
  75. periph.setConnectionHandler(event_handler)
  76. periph.advertise(device_name="micr_temp", services=[serv_env_sense])