commit 69d15450894f2de8b640e0a1bd2e1e129d743968 Author: printfuck Date: Sun Apr 9 22:09:35 2023 +0200 slightly modified diff --git a/arp.py b/arp.py new file mode 100644 index 0000000..d5a8bda --- /dev/null +++ b/arp.py @@ -0,0 +1,52 @@ +from scapy.all import * +import logging as log +import sqlite3 +import signal +import sys + +def signal_handler(sig, frame): + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) + + + +def arp_display(pkt): + if pkt[ARP].op == 1: #who-has (request) + return f"Request from: {pkt[ARP].hwsrc} ({pkt[ARP].psrc}) requesting MAC for {pkt[ARP].pdst}" + #if pkt[ARP].op == 2: #is-at (response) + lookup("mac.db", str(pkt[ARP].hwsrc)) + add("mac.db",str(pkt[ARP].hwsrc),round(time.time())) + return f"Code: {pkt[ARP].op} - Source {pkt[ARP].hwsrc} with address {pkt[ARP].psrc}" + +def init(database): + s = sqlite3.connect(database) + c = s.cursor() + c.execute(''' + CREATE TABLE if not exists mac_table( + id INTEGER PRIMARY KEY AUTOINCREMENT, + mac text NOT NULL, + ts integer NOT NULL + );''') + s.commit() + +def lookup(database, mac): + s = sqlite3.connect(database) + c = s.cursor() + print ("lookup") + c.execute("SELECT ts FROM mac_table where mac = ? ORDER BY ts DESC LIMIT 1;", mac) + r = c.fetchone() + print (r) + if r is not None: + print ("last seen: ", round(time.time()) - r, " ms ago") + +def add(database,mac,time): + s = sqlite3.connect(database) + c = s.cursor() + c.execute("INSERT INTO mac_table (mac, ts) VALUES (?,?)", (mac, time)) + s.commit() + +init("mac.db") +while True: + sniff(prn=arp_display, filter="arp", store=0, iface='wlan0', count=1) + diff --git a/axmpp.py b/axmpp.py new file mode 100644 index 0000000..08abb4b --- /dev/null +++ b/axmpp.py @@ -0,0 +1,92 @@ +import logging + +import slixmpp +from slixmpp.exceptions import IqError, IqTimeout +import os +from dotenv import load_dotenv +import RPi.GPIO as GPIO +import time +from threading import Thread +import asyncio + +GPIO.setmode(GPIO.BCM) +GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) +init = 0 + +class EchoBot(slixmpp.ClientXMPP): + + def __init__(self, jid, password): + slixmpp.ClientXMPP.__init__(self, jid, password) + + self.add_event_handler("session_start", self.session_start) + self.add_event_handler("message", self.message) + loop = asyncio.get_event_loop() + asyncio.run_coroutine_threadsafe(asyncio.to_thread(self.ringLoop), loop) + + def session_start(self, event): + self.send_presence() + self.get_roster() + + # Most get_*/set_* methods from plugins use Iq stanzas, which + # can generate IqError and IqTimeout exceptions + # + # try: + # self.get_roster() + # except IqError as err: + # logging.error('There was an error getting the roster') + # logging.error(err.iq['error']['condition']) + # self.disconnect() + # except IqTimeout: + # logging.error('Server is taking too long to respond') + # self.disconnect() + + def message(self, msg): + global init + if msg['type'] in ('chat', 'normal'): + print (msg['body']) + if msg['body'] == "open": + msg.reply("Buzzing").send() + if init == 0: + GPIO.setup(17, GPIO.OUT) + init = 1 + GPIO.output(17, GPIO.LOW) + time.sleep(2) + GPIO.output(17, GPIO.HIGH) + else: + msg.reply("Not Understood").send() + + + def ringLoop(self): + print("foo1") + #GPIO.setmode(GPIO.BCM) # GPIO Numbers instead of board numbers + recipient = os.getenv("NOTIFY") + #GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) + while True: + print ("GPIO OUTPUT: ", GPIO.input(23)) + time.sleep(0.1) + if (GPIO.input(23) == 0): + self.send_message(mto=recipient, mbody="Klingelt!", mtype='chat') + #self._loop.call_soon_threadsafe(self.send_message(mto=recipient, mbody="Klingelt!", mtype='chat')) + print ("ringing") + print ("ringing") + print ("ringing") + print ("ringing") + print ("ringing") + +if __name__ == '__main__': + # Ideally use optparse or argparse to get JID, + # password, and log level. + load_dotenv() + password = os.getenv("PASS") + jid = os.getenv("JID") + + #GPIO.setup(17, GPIO.OUT) + #GPIO.output(17, GPIO.LOW) + logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s') + + xmpp = EchoBot(jid, password) + #Thread(name="GPIO_INPUT", target=xmpp.ringLoop).start() + #asyncio.run_coroutine_threadsafe(self.waiting_queue.put((data, use_filters)), self.loop) + + xmpp.connect() + xmpp.process(forever=True) diff --git a/bar.py b/bar.py new file mode 100644 index 0000000..83566c1 --- /dev/null +++ b/bar.py @@ -0,0 +1,10 @@ +import RPi.GPIO as GPIO +import time +GPIO.setmode(GPIO.BCM) # GPIO Numbers instead of board numbers +GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) + +while True: + #print (GPIO.input(23)) + time.sleep(0.1) + if (GPIO.input(23) == 0) + print ("ringing") diff --git a/foo.py b/foo.py new file mode 100644 index 0000000..085e8ad --- /dev/null +++ b/foo.py @@ -0,0 +1,25 @@ +import RPi.GPIO as GPIO +import time +GPIO.setmode(GPIO.BCM) # GPIO Numbers instead of board numbers + +RELAIS_1_GPIO = 17 +GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode +#GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # out +GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # on +time.sleep(10) +GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # on +time.sleep(10) diff --git a/fooserver.py b/fooserver.py new file mode 100644 index 0000000..3973a25 --- /dev/null +++ b/fooserver.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3.8 +# +from enum import Enum +from flask import Flask as f +from flask import Response +import RPi.GPIO as GPIO +import os +import time +from threading import Thread + +#import re +#import json +#import req as r + +class TreeState(Enum): + OFF = 1 + BUZZ = 2 + +GPIO.setwarnings(False) +GPIO.setmode(GPIO.BCM) +#GPIO.setup(17, GPIO.OUT) +#GPIO.output(17, GPIO.LOW) +#time.sleep(1) +#GPIO.output(17, GPIO.HIGH) +app = f(__name__) +state = TreeState.OFF + +def loop(): + global state + while True: + time.sleep(1) + print ("loop") + if (state == TreeState.BUZZ): + print ("buzz") + GPIO.output(17, GPIO.LOW) + time.sleep(2) + GPIO.output(17, GPIO.HIGH) + state = TreeState.OFF + +@app.route('/buzz', methods = ['GET']) +def rbuzz(): + #global state + #state = TreeState.BUZZ + #GPIO.setmode(GPIO.BCM) + GPIO.setup(17, GPIO.OUT) + GPIO.output(17, GPIO.LOW) + time.sleep(2) + GPIO.output(17, GPIO.HIGH) + return "{\"status\":0}" + + +@app.route('/') +def index(): + return open("index.html", "r").read() + + +if __name__ == '__main__': + # Run Server + #Thread(name="backgroundLoop", target=loop).start() + app.run(host='0.0.0.0', port=80, debug=True) + diff --git a/index.html b/index.html new file mode 100644 index 0000000..80dad6b --- /dev/null +++ b/index.html @@ -0,0 +1,8 @@ + + + +

Door

+ open + + + diff --git a/waxmpp.py b/waxmpp.py new file mode 100644 index 0000000..087f6cb --- /dev/null +++ b/waxmpp.py @@ -0,0 +1,107 @@ +import logging + +# xmpp +import slixmpp +from slixmpp.exceptions import IqError, IqTimeout +# files +import os +# config +from dotenv import load_dotenv +# gpio +import RPi.GPIO as GPIO +# sleep +import time +# hacking my godamn threads into slixmpp non threadable library +import asyncio +# webserver +from flask import Flask as f +from flask import Response + +GPIO.setmode(GPIO.BCM) +GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) +init = 0 +app = f("web_thread") + +class EchoBot(slixmpp.ClientXMPP): + + def __init__(self, jid, password): + slixmpp.ClientXMPP.__init__(self, jid, password) + + self.add_event_handler("session_start", self.session_start) + self.add_event_handler("message", self.message) + self.add_event_handler("disconnected", self.my_reconnect) + #self.add_event_handler("connection_failed", self._reconnect) + loop = asyncio.get_event_loop() + asyncio.run_coroutine_threadsafe(asyncio.to_thread(app.run,host='0.0.0.0', port=80, debug=False), loop) + asyncio.run_coroutine_threadsafe(asyncio.to_thread(self.ringLoop), loop) + + def session_start(self, event): + self.send_presence() + self.get_roster() + + def message(self, msg): + global init + if msg['type'] in ('chat', 'normal'): + print (msg['body']) + if msg['body'] == "open": + msg.reply("Buzzing").send() + if init == 0: + GPIO.setup(17, GPIO.OUT) + init = 1 + GPIO.output(17, GPIO.LOW) + time.sleep(2) + GPIO.output(17, GPIO.HIGH) + else: + msg.reply("Not Understood").send() + + async def my_reconnect(self, ev): + await asyncio.sleep(5) + while not self.is_connected(): + self.connect() + await asyncio.sleep(20) + self.send_message(mto=recipient, mbody="reconnected!", mtype='chat') + + + def ringLoop(self): + channel = 0 + nya = 0 + while True: + channel = GPIO.wait_for_edge(23, GPIO.FALLING, timeout=5000) + nya = time.time_ns() + if channel is None: + print (time.strftime("%d-%m|%H:%M",time.localtime()),"> GPIO OUTPUT: 1") + else: + channel = GPIO.wait_for_edge(23, GPIO.BOTH, timeout=5000) + msg = "Ringing for " + str((time.time_ns() - nya)/1000000) + " ms" + xmpp.send_message(mto=recipient, mbody=msg, mtype='chat') + print (time.strftime("%d-%m|%H:%M",time.localtime()),"> Ringing!") + + @app.route('/buzz', methods = ['GET']) + def rbuzz(): + global init + if init == 0: + GPIO.setup(17, GPIO.OUT) + init = 1 + GPIO.output(17, GPIO.LOW) + time.sleep(2) # dumb - I know - go on + GPIO.output(17, GPIO.HIGH) + xmpp.send_message(mto=recipient, mbody="opened via web interface", mtype='chat') + return "{\"status\":0}" + + + @app.route('/') + def index(): + return open("index.html", "r").read() + + +if __name__ == '__main__': + load_dotenv() + password = os.getenv("PASS") + jid = os.getenv("JID") + recipient = os.getenv("NOTIFY") + + logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s') + xmpp = EchoBot(jid, password) + + xmpp.connect() + xmpp.process(forever=True) diff --git a/waxmpp_old.py b/waxmpp_old.py new file mode 100644 index 0000000..939c6a4 --- /dev/null +++ b/waxmpp_old.py @@ -0,0 +1,125 @@ +import logging + +# xmpp +import slixmpp +from slixmpp.exceptions import IqError, IqTimeout +# files +import os +# config +from dotenv import load_dotenv +# gpio +import RPi.GPIO as GPIO +# sleep +import time +# hacking my godamn threads into slixmpp non threadable library +import asyncio +# webserver +from flask import Flask as f +from flask import Response + +GPIO.setmode(GPIO.BCM) +GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) +init = 0 +app = f("web_thread") + +class EchoBot(slixmpp.ClientXMPP): + + def __init__(self, jid, password): + slixmpp.ClientXMPP.__init__(self, jid, password) + + self.add_event_handler("session_start", self.session_start) + self.add_event_handler("message", self.message) + self.add_event_handler("disconnect", self.reconnect) + self.add_event_handler("connection_failed", self.reconnect) + loop = asyncio.get_event_loop() + asyncio.run_coroutine_threadsafe(asyncio.to_thread(app.run,host='0.0.0.0', port=80, debug=False), loop) + asyncio.run_coroutine_threadsafe(asyncio.to_thread(self.ringLoop), loop) + + def session_start(self, event): + self.send_presence() + self.get_roster() + + # Most get_*/set_* methods from plugins use Iq stanzas, which + # can generate IqError and IqTimeout exceptions + # + # try: + # self.get_roster() + # except IqError as err: + # logging.error('There was an error getting the roster') + # logging.error(err.iq['error']['condition']) + # self.disconnect() + # except IqTimeout: + # logging.error('Server is taking too long to respond') + # self.disconnect() + + def message(self, msg): + global init + if msg['type'] in ('chat', 'normal'): + print (msg['body']) + if msg['body'] == "open": + msg.reply("Buzzing").send() + if init == 0: + GPIO.setup(17, GPIO.OUT) + init = 1 + GPIO.output(17, GPIO.LOW) + time.sleep(2) + GPIO.output(17, GPIO.HIGH) + else: + msg.reply("Not Understood").send() + + def reconnect(self): + while not self.is_connected(): + self.connect() + time.sleep(5) + + def ringLoop(self): + recipient = os.getenv("NOTIFY") + debug = 0 + while True: + #debug = debug + 1 + #if debug == 50: + # print (time.strftime("%d-%m|%H:%M",time.localtime()),"> GPIO OUTPUT: ", GPIO.input(23)) + # debug = 0 + #time.sleep(0.1) + channel = GPIO.wait_for_edge(23, GPIO.FALLING, timeout=5000) + if channel is None: + print (time.strftime("%d-%m|%H:%M",time.localtime()),"> GPIO OUTPUT: 1") + else: + #if GPIO.input(23) == 0: + self.send_message(mto=recipient, mbody="Klingelt!", mtype='chat') + time.sleep(0.5) + # self._loop.call_soon_threadsafe(self.send_message(mto=recipient, mbody="Klingelt!", mtype='chat')) + # works too without modifying the library but throws errors everytime it's called + print ("ringing") + print ("ringing") + print ("ringing") + print ("ringing") + print ("ringing") + + @app.route('/buzz', methods = ['GET']) + def rbuzz(): + global init + if init == 0: + GPIO.setup(17, GPIO.OUT) + init = 1 + GPIO.output(17, GPIO.LOW) + time.sleep(2) # dumb - I know - go on + GPIO.output(17, GPIO.HIGH) + return "{\"status\":0}" + + + @app.route('/') + def index(): + return open("index.html", "r").read() + + +if __name__ == '__main__': + load_dotenv() + password = os.getenv("PASS") + jid = os.getenv("JID") + + logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s') + xmpp = EchoBot(jid, password) + + xmpp.connect() + xmpp.process(forever=True) diff --git a/xmpp.py b/xmpp.py new file mode 100644 index 0000000..e6ed664 --- /dev/null +++ b/xmpp.py @@ -0,0 +1,98 @@ +import logging + +from slixmpp import ClientXMPP +from slixmpp.exceptions import IqError, IqTimeout +import os +from dotenv import load_dotenv +import RPi.GPIO as GPIO +import time +from threading import Thread +import asyncio + +class EchoBot(ClientXMPP): + + def __init__(self, jid, password): + ClientXMPP.__init__(self, jid, password) + + self.add_event_handler("session_start", self.session_start) + self.add_event_handler("message", self.message) + + # If you wanted more functionality, here's how to register plugins: + # self.register_plugin('xep_0030') # Service Discovery + # self.register_plugin('xep_0199') # XMPP Ping + + # Here's how to access plugins once you've registered them: + # self['xep_0030'].add_feature('echo_demo') + + # If you are working with an OpenFire server, you will + # need to use a different SSL version: + # import ssl + # self.ssl_version = ssl.PROTOCOL_SSLv3 + + def session_start(self, event): + self.send_presence() + self.get_roster() + + # Most get_*/set_* methods from plugins use Iq stanzas, which + # can generate IqError and IqTimeout exceptions + # + # try: + # self.get_roster() + # except IqError as err: + # logging.error('There was an error getting the roster') + # logging.error(err.iq['error']['condition']) + # self.disconnect() + # except IqTimeout: + # logging.error('Server is taking too long to respond') + # self.disconnect() + + def message(self, msg): + if msg['type'] in ('chat', 'normal'): + print (msg['body']) + if msg['body'] == "open": + buzz() + msg.reply("Buzzing").send() + else: + msg.reply("Not Understood").send() + + +def sendRing(): + global xmpp + recipient = os.getenv("NOTIFY") + print ("sent message to ") + print (recipient) + #loop = asyncio.get_event_loop(xmpp) + #asyncio.run_coroutine_threadsafe(asyncio.to_thread(xmpp.send_message(mto=recipient, mbody="Klingelt!", mtype='chat')),loop) + xmpp.send_message(mto=recipient, mbody="Klingelt!", mtype='chat') + +def ringLoop(): + GPIO.setmode(GPIO.BCM) # GPIO Numbers instead of board numbers + GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) + timer = 100 + while True: + print (GPIO.input(23)) + time.sleep(0.1) + timer = timer - 1 + if (GPIO.input(23) == 0 or timer == 0): + sendRing() + print ("ringing") + print ("ringing") + print ("ringing") + print ("ringing") + print ("ringing") + +if __name__ == '__main__': + # Ideally use optparse or argparse to get JID, + # password, and log level. + load_dotenv() + password = os.getenv("PASS") + jid = os.getenv("JID") + + logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s') + + xmpp = EchoBot(jid, password) + Thread(name="GPIO_INPUT", target=ringLoop).start() + #asyncio.run_coroutine_threadsafe(self.waiting_queue.put((data, use_filters)), self.loop) + + xmpp.connect() + xmpp.process(forever=True)