Created
November 23, 2019 15:03
-
-
Save cutterkom/845287578bfbc8da7d5bd3c015c98103 to your computer and use it in GitHub Desktop.
Measure humidity and temperature with a Raspberry Pi, save the result as a csv file and send a Telegram message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Measure Temperature and Humidity very 30 secs and save it csv file | |
# code https://pimylifeup.com/raspberry-pi-humidity-sensor-dht22/ | |
### Create a Telegram Bot | |
# - create a bot by texting to BotFather `/newbot` and receive a token | |
# - go to `https://api.telegram.org/botYOUR_TOKEN/getUpdates` | |
# - send a text to your bot | |
# - the JSON on `https://api.telegram.org/botYOUR_TOKEN/getUpdates` will update and you get the `chat_id` | |
# send a message to chat: `https://api.telegram.org/botYOUR_TOKEN/sendMessage?chat_id=CHAT_ID&text=I+can+talk+to+you` | |
# link: [Creating a Telegram bot for personal notifications](https://www.forsomedefinition.com/automation/creating-telegram-bot-notifications/) | |
### run on a remote Pi | |
# run forever in background: nohup python3 /home/pi/spielen/feuchtesensor/measure_send.py -i 300 >> nohup.out 2>&1 & | |
# kill process: | |
# kill $(ps aux | grep python | grep measure_send.py | awk '{print $2}') | |
### code: | |
import Adafruit_DHT | |
import os | |
import time | |
import requests | |
import argparse | |
# define constants: | |
# which sensor? here: DHT11 | |
DHT_SENSOR = Adafruit_DHT.DHT11 | |
# which GPIO Pin? | |
DHT_PIN = 2 | |
# path of csv file | |
FILE = "measurements.csv" | |
# telegram | |
TOKEN = "your_telegram_token" | |
CHAT_ID = "your_telegram_chat_id" | |
# initiate argument parser | |
parser = argparse.ArgumentParser(description="This script measures humidity and temperature and sends a telegram message, when humidity is > 60%.") | |
# add long an short argument | |
parser.add_argument("-i", "--intervall", help="interval in which measurement is performed", type=int) | |
# read arguments from the command line | |
args = parser.parse_args() | |
# open csv file | |
# and if there is none (st_size == 0), then create one | |
try: | |
f = open(FILE, 'a+') | |
if os.stat(FILE).st_size == 0: | |
f.write('date,time,temperature,humidity\r\n') | |
except: | |
pass | |
print("start") | |
# measure humidity and temperature | |
# save results to csv file | |
# and if humidity is > 60%, send a message to telegram | |
while True: | |
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) | |
if humidity is not None and temperature is not None: | |
f.write('{0},{1},{2:0.1f},{3:0.1f}\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M:%SS'), temperature, humidity)) | |
print("Luftfeuchtigkeit = {0}, temperatur = {1}".format(humidity, temperature)) | |
if humidity > 60: | |
# newline: %0D%0A | |
message_title = "*Bitte+lüften!*%0D%0ADie+Luftfeuchtigkeit+beträgt+{0}%".format(humidity) | |
send_text = 'https://api.telegram.org/bot' + TOKEN + '/sendMessage?chat_id=' + CHAT_ID + '&parse_mode=Markdown&text=' + message_title | |
requests.get(send_text) | |
else: | |
print("humidity < 60%: {0}%".format(humidity)) | |
else: | |
print("Failed to retrieve data from humidity sensor") | |
time.sleep(args.intervall) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to add on codes if its diplaying the same info on a lcd?