Compare commits

..

7 Commits

Author SHA1 Message Date
78503d4349 Update 'raspberry.md' 2022-01-10 12:48:45 +00:00
Marcel Dechert
51fc3fb486 add some doc 2020-12-13 22:33:24 +01:00
Marcel Dechert
1b48ff1e79 swaghausen bot 2020-12-13 22:10:59 +01:00
Marcel Dechert
e9e2913346 telegram bot update 2020-12-13 22:03:35 +01:00
1dd31a32ec update 2020-12-05 21:31:58 +01:00
Marcel Dechert
37f8fe580d telegram bot notifications 2020-12-05 21:00:53 +01:00
Marcel Dechert
28bce212d1 update md 2020-12-05 19:54:40 +01:00
9 changed files with 209 additions and 5 deletions

View File

@@ -6,5 +6,6 @@ dependencies:
- numpy
- scipy
- matplotlib
- requests
- pip:
- sounddevice

View File

@@ -1,7 +1,11 @@
## connect via SSH
## PythonKlingelAnlage
#
- Better USB Micro
https://electronut.in/mico/
# Useful commands
- see connected usb devices
- lsusb
- arecord -l
@@ -24,3 +28,15 @@ pcm.mic {
pcm "hw:1,0"
}
}
44200 sampling rate
5 secs
230000 array length
## Telegram Bot
SwaghausenBot
token:1456847960:AAHF6ZVfPrPedN07-Z0FlbtFzuUgpQM3iO4
chat_id:291512617
https://api.telegram.org/bot1456847960:AAHF6ZVfPrPedN07-Z0FlbtFzuUgpQM3iO4/getUpdates

59
requirements.txt Normal file
View File

@@ -0,0 +1,59 @@
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
blas=1.0=mkl
brotlipy=0.7.0=py36h2bbff1b_1003
ca-certificates=2020.12.8=haa95532_0
certifi=2020.12.5=py36haa95532_0
cffi=1.14.4=py36hcd4344a_0
chardet=3.0.4=py36haa95532_1003
cryptography=3.3.1=py36hcd4344a_0
cycler=0.10.0=py36haa95532_0
freetype=2.10.4=hd328e21_0
icc_rt=2019.0.0=h0cc432a_1
icu=58.2=ha925a31_3
idna=2.10=py_0
intel-openmp=2020.2=254
jpeg=9b=hb83a4c4_2
kiwisolver=1.3.0=py36hd77b12b_0
libpng=1.6.37=h2a8f88b_0
libtiff=4.1.0=h56a325e_1
lz4-c=1.9.2=hf4a77e7_3
matplotlib=3.3.2=0
matplotlib-base=3.3.2=py36hba9282a_0
mkl=2020.2=256
mkl-service=2.3.0=py36h196d8e1_0
mkl_fft=1.2.0=py36h45dec08_0
mkl_random=1.1.1=py36h47e9c7a_0
numpy=1.19.2=py36hadc3359_0
numpy-base=1.19.2=py36ha3acd2a_0
olefile=0.46=py36_0
openssl=1.1.1i=h2bbff1b_0
pillow=8.0.1=py36h4fa10fc_0
pip=20.3.1=py36haa95532_0
pycparser=2.20=py_2
pyopenssl=20.0.0=pyhd3eb1b0_1
pyparsing=2.4.7=py_0
pyqt=5.9.2=py36h6538335_2
pysocks=1.7.1=py36haa95532_0
python=3.6.12=h5500b2f_2
python-dateutil=2.8.1=py_0
qt=5.9.7=vc14h73c81de_0
requests=2.25.0=pyhd3eb1b0_0
scipy=1.5.2=py36h9439919_0
setuptools=51.0.0=py36haa95532_2
sip=4.19.8=py36h6538335_0
six=1.15.0=py36haa95532_0
sounddevice=0.4.1=pypi_0
sqlite=3.33.0=h2a8f88b_0
tk=8.6.10=he774522_0
tornado=6.1=py36h2bbff1b_0
urllib3=1.25.11=py_0
vc=14.2=h21ff451_1
vs2015_runtime=14.27.29016=h5e58377_2
wheel=0.36.1=pyhd3eb1b0_0
win_inet_pton=1.1.0=py36haa95532_0
wincertstore=0.2=py36h7fe50ca_0
xz=5.2.5=h62dcd97_0
zlib=1.2.11=h62dcd97_4
zstd=1.4.5=h04227a9_0

View File

@@ -1,15 +1,15 @@
from numpy import*
from scipy.io.wavfile import read
from scipy.io.wavfile import write
import matplotlib.pyplot as plt
# import matplotlib.pyplot as plt
import scipy
import sounddevice as sd
import time
a = read("/home/jreinking/Projekte/doorbell/raspberry-pi-projects/res/klingel_aufnahme_microphne_cut.wav")
# a = read("/home/jreinking/Projekte/doorbell/raspberry-pi-projects/res/klingel_aufnahme_microphne_cut.wav")
# print(a)
# print(len(a[1]))
a = a[1]
# a = a[1]
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
@@ -48,7 +48,7 @@ SECONDS = (400000 / SAMPLE_RATE) / NUMBER_OF_SNIPPETS
snippets = []
i = 0
while True:
myrecording = sd.rec(int(SECONDS * SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=2)
myrecording = sd.rec(int(SECONDS * SAMPLE_RATE), samplerate=48000, channels=1)
snippets.append(myrecording)
sd.wait()

Binary file not shown.

View File

View File

@@ -0,0 +1,20 @@
import telegram_notifier
import logging
# telegram bot ---
TELEGRAM_TOKEN_ID = '1456847960:AAHF6ZVfPrPedN07-Z0FlbtFzuUgpQM3iO4' # token-id
TELEGRAM_CHAT_ID = '291512617' # chat-id
# ----------------
def send_notification(timestamp):
""" sends Telegram notification
using https://github.com/gvalvano/telegram-notification
"""
formatted_timestamp = timestamp.strftime("%d.%m.%Y, %H:%M:%S")
msg = "<b>Es hat geklingelt! </b> \nTimestamp: {}".format(formatted_timestamp)
telegram_notifier.basic_notifier(logger_name='training_notifier',
token_id=TELEGRAM_TOKEN_ID,
chat_id=TELEGRAM_CHAT_ID,
message=msg,
level=logging.INFO)

View File

@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
import unittest
import telegram_bot
import datetime
class SendTestMessageToSwaghausenBot(unittest.TestCase):
def test_send_notifaction(self):
timestamp = datetime.datetime.now()
telegram_bot.send_notification(timestamp)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,92 @@
# Copyright 2019 Gabriele Valvano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Inspired by: https://www.marcodena.it/blog/telegram-logging-handler-for-python-java-bash/
1) Search the "BotFather" on Telegram. This is the official bot that allows you to create other bots.
2) Create new bot: /newbot
3) Choose a name for your bot: ScriptNotifier
4) Choose a username for your bot that must end with "_bot": script_notifier_bot
5) Once the bot is created, you will have a long string that is the TOKENID
6) The bot will send you messages on a specific chat, that you need to create. Go to Telegram search bar, on your
smartphone, and search your bot. Then, start the bot: /start
7) Now you are ready to use a command line code to send your first notification:
"curl -s -X POST https://api.telegram.org/bot[TOKENID]/sendMessage -d chat_id=[ID] -d text="Hello world" "
- - - - - - -
bot page:
https://api.telegram.org/bot[TOKENID]/getUpdates
- - - - - - -
bot info:
curl -X GET https://api.telegram.org/bot[TOKENID]/getMe
- - - - - - -
send message to the bot:
curl -s -X POST https://api.telegram.org/bot[TOKENID]/sendMessage -d chat_id=[ID] -d text="Hello world"
"""
import requests
from logging import Handler, Formatter
import logging
class RequestsHandler(Handler):
def __init__(self, token_id, chat_id):
super().__init__()
self.token_id = token_id
self.chat_id = chat_id
def emit(self, record):
log_entry = self.format(record)
payload = {
'chat_id': self.chat_id,
'text': log_entry,
'parse_mode': 'HTML'
}
return requests.post("https://api.telegram.org/bot{token}/sendMessage".format(token=self.token_id),
data=payload).content
class LogstashFormatter(Formatter):
def __init__(self):
super(LogstashFormatter, self).__init__()
def format(self, record):
# time = strftime("%d/%m/%Y, %H:%M:%S")
# return "<b>{datetime}</b>\n{message}".format(datetime=time, message=record.msg)
return "{message}".format(message=record.msg)
def basic_notifier(logger_name, token_id, chat_id, message, level=logging.INFO):
logger = logging.getLogger(logger_name)
logger.setLevel(level)
handler = RequestsHandler(token_id=token_id, chat_id=chat_id)
formatter = LogstashFormatter()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(level)
logger.info(message)
if __name__ == '__main__':
l_name = 'trymeApp'
l_msg = 'We have a problem'
t_id = 'insert here your token id'
c_id = 'insert here your chat id'
basic_notifier(logger_name=l_name, token_id=t_id, chat_id=c_id, message=l_msg)