doorbot.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #! /usr/bin/env python
  2. # This is an example of an IRC bot which :
  3. # * Sends on IRC a message when a door is opened / closed
  4. # * Answers to command "porte" by returning door state
  5. #
  6. # Door state is read from Raspberry PI GPIO using RPi.GPIO
  7. # GPIOs are set with GPIO.BOARD numbering scheme to be interoperable with various hardware versions
  8. #
  9. # WARNING : this a far away from state of art peace of code and
  10. # Example program using irc.bot.
  11. #
  12. # Joel Rosdahl <joel@rosdahl.net>
  13. import RPi.GPIO as GPIO
  14. import irc.bot
  15. import irc.strings
  16. from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
  17. GPIO.setmode(GPIO.BOARD)
  18. GPIO.setup(21, GPIO.IN)
  19. GPIO.setup(19, GPIO.IN)
  20. server_hostname = "irc.geeknode.net"
  21. server_port = 6667
  22. irc_chan = "#faimaison"
  23. bot_name = "didier"
  24. def get_door_state():
  25. state = []
  26. state.append("[+] backdoor is opened" if GPIO.input(19) == 0 else "[ ] backdoor is closed")
  27. state.append("[+] front door is opened" if GPIO.input(21) == 0 else "| ] front door is closed")
  28. return state
  29. class TestBot(irc.bot.SingleServerIRCBot):
  30. def __init__(self, channel, nickname, server, port=6667):
  31. irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
  32. self.channel = channel
  33. def on_nicknameinuse(self, c, e):
  34. c.nick(c.get_nickname() + "_")
  35. def on_welcome(self, c, e):
  36. c.join(self.channel)
  37. def on_privmsg(self, c, e):
  38. self.do_command(e, e.arguments[0])
  39. def on_pubmsg(self, c, e):
  40. a = e.arguments[0].split(":", 1)
  41. if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(self.connection.get_nickname()):
  42. self.do_command(e, a[1].strip())
  43. return
  44. def on_dccmsg(self, c, e):
  45. # non-chat DCC messages are raw bytes; decode as text
  46. text = e.arguments[0].decode('utf-8')
  47. c.privmsg("You said: " + text)
  48. def on_dccchat(self, c, e):
  49. if len(e.arguments) != 2:
  50. return
  51. args = e.arguments[1].split()
  52. if len(args) == 4:
  53. try:
  54. address = ip_numstr_to_quad(args[2])
  55. port = int(args[3])
  56. except ValueError:
  57. return
  58. self.dcc_connect(address, port)
  59. def send_msg(self, msg):
  60. c = self.connection
  61. print("ok msg %s" % msg)
  62. c.privmsg(irc_chan, msg)
  63. def do_command(self, e, cmd):
  64. nick = e.source.nick
  65. c = self.connection
  66. # if cmd == "disconnect":
  67. # self.disconnect()
  68. if cmd == "porte":
  69. for s in get_door_state():
  70. c.privmsg(irc_chan, "%s : %s" % (nick, s))
  71. # elif cmd == "die":
  72. # self.die()
  73. # elif cmd == "stats":
  74. # for chname, chobj in self.channels.items():
  75. # c.notice(nick, "--- Channel statistics ---")
  76. # c.notice(nick, "Channel: " + chname)
  77. # users = sorted(chobj.users())
  78. # c.notice(nick, "Users: " + ", ".join(users))
  79. # opers = sorted(chobj.opers())
  80. # c.notice(nick, "Opers: " + ", ".join(opers))
  81. # voiced = sorted(chobj.voiced())
  82. # c.notice(nick, "Voiced: " + ", ".join(voiced))
  83. # elif cmd == "dcc":
  84. # dcc = self.dcc_listen()
  85. # c.ctcp("DCC", nick, "CHAT chat %s %d" % (
  86. # ip_quad_to_numstr(dcc.localaddress),
  87. # dcc.localport))
  88. else:
  89. c.privmsg(irc_chan, nick + " : Hein ? je comprend pas cette commande. Mes commandes sont : 'porte'")
  90. # !/usr/bin/env python3
  91. import time
  92. from time import sleep
  93. import RPi.GPIO as GPIO
  94. pin_back = 19
  95. pin_front = 21
  96. GPIO.setmode(GPIO.BOARD)
  97. GPIO.setup(pin_back, GPIO.IN) # , pull_up_down=GPIO.PUD_DOWN)
  98. previous_state = None
  99. def main():
  100. import sys
  101. server = server_hostname
  102. port = server_port
  103. channel = irc_chan
  104. nickname = bot_name
  105. bot = TestBot(channel, nickname, server, port)
  106. bot.connect(server, port, nickname)
  107. bot.send_msg("hello world")
  108. def my_callback(channel):
  109. global previous_state
  110. # time.sleep(0.2) # confirm the movement by waiting 1.5 sec
  111. pin_val = GPIO.input(pin_back)
  112. if pin_val != previous_state:
  113. previous_state = pin_val
  114. if pin_val: # and check again the input
  115. bot.send_msg("[ ] CapsLock : DOOR CLOSED !")
  116. else:
  117. bot.send_msg("[+] CapsLock : DOOR OPENED !")
  118. GPIO.add_event_detect(pin_back, GPIO.BOTH, callback=my_callback)
  119. bot.start()
  120. if __name__ == "__main__":
  121. main()