-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpizero_robot.py
executable file
·64 lines (52 loc) · 1.54 KB
/
pizero_robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from time import sleep
import signal
import re
from subprocess import check_output
import telegram
import logging
logging.basicConfig(format='%(name)-8s: %(levelname)-6s %(message)s') # %(asctime)-10s
logger = logging.getLogger('telegram.bot')
logger.setLevel(logging.INFO)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
import telegram_token
from ledControl import ledControl
logger.debug("Turning on LED")
lc=ledControl()
lc.redColorWipe()
# get ip address
attempt = 0
while True:
logger.debug(f"Getting the IP using 'ifconfig' (attempt {attempt})")
wlan0 = str(check_output(["ifconfig", "wlan0"]))
matches = re.findall(r'inet \d+[.]\d+[.]\d+[.]\d+', wlan0)
if matches:
ip = matches[0].split()[1]
logger.debug(f" - got {ip}")
break
elif attempt > 10:
ip = "Can't get ip"
break
attempt+=1
lc.blueColorWipe()
# send it to telegram
logger.debug(f"sending '{ip}' to Telegram")
bot = telegram.Bot(token=telegram_token.BOT_TOKEN)
try:
bot.send_message(chat_id=telegram_token.CHAT_ID, text=f"RaspiTank got IP {ip}")
except Exception as e:
logger.warning(f"Can't send to telegram: {e}")
lc.redColorWipe()
# Termination signals callback
def exit_gracefully(signum, frame):
#print(f"signum: {signum}, frame: {frame}")
logger.info(f"Caught termination signal {signum}. Exiting cleanly :)")
lc.wipeClean()
exit(0)
signal.signal(signal.SIGINT, exit_gracefully) # stop-sigterm
signal.signal(signal.SIGTERM, exit_gracefully)
while True:
logger.debug(".")
sleep(10)