-
Notifications
You must be signed in to change notification settings - Fork 0
5 The Script
Kirpi project is based on Python program that collecting sensor data and share them by Telegram, FM radio and FTP. This page can help the user to understand the workflow and the background of the script.
The following libraries are required and added the top of code.
import string
import time
import Adafruit_DHT
import RPi.GPIO as GPIO
import signal
import sys
from gtts import gTTS
from ftplib import FTP
import os
import config
import requests
from datetime import datetime
import jsonSome features of program depend on these variables to debug or run partial of code.
tts_enable = 1
ftp_enable = 1
tgm_enable = 1
delay = 0Setup and measure of distance done by following code:
DIST_TRIGGER = 23
GPIO.setup(DIST_TRIGGER, GPIO.OUT)
DIST_ECHO = 24
GPIO.setup(DIST_ECHO, GPIO.IN)
def distance():
GPIO.output(DIST_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(DIST_TRIGGER, False)
start_time = time.time()
stop_time = time.time()
while GPIO.input(DIST_ECHO) == 0:
start_time = time.time()
while GPIO.input(DIST_ECHO) == 1:
stop_time = time.time()
time_elapsed = stop_time - start_time
return (time_elapsed * 34300) / 2Environmental light level measured by the following code:
ldr_pin = 3
def light():
reading = 0
GPIO.setup(ldr_pin, GPIO.OUT)
GPIO.output(ldr_pin, GPIO.LOW)
time.sleep(.1)
GPIO.setup(ldr_pin, GPIO.IN)
while GPIO.input(ldr_pin) == GPIO.LOW:
reading += 1
return 2000 / readingBoth temperature and humidity level measured by the same sensor. Adafruit library is used to get data from the sensor.
temp_sensor = Adafruit_DHT.DHT11
temp_pin = 4
Adafruit_DHT.read_retry(temp_sensor, temp_pin)The program gets all information from sensors in a loop and create a report in text format by them.
while True:
data = {'date': str(datetime.now()), 'dist': dist, 'temp': temp, 'humid': humid, 'light': light()}report = "Report: \n" \
"Distance is: {0} centimeter. \n" \
"Temperature: {1:1.0f} celsius. \n" \
“Humidity: {2:1.0f}%. \n" \
"Light level: {3}." \
.format(data['dist'], data['temp'], data['humid'], data['light'])
print(report)Program writes the report to a file and save it to program folder. Therefore, program uploads the file to server by using config.py configuration.
file = open("report.html", "w")
file.write(report.replace("\n", "<br>"))
file.close()
if ftp_enable == 1:
# Upload to FTP server
ftp = FTP(config.ftphost)
ftp.login(config.ftpuser, config.ftppass)
with open('report.html', 'r') as f:
ftp.storbinary('STOR %s' % 'kirpi.html', f)
ftp.quit()Telegram is an open-source instant messaging application. Telegram support bots. For this project I created a telegram bot named Kirpi who share the report on its Telegram channel.
You can join Kirpi Telegram channel here: t.me/kirpi
requests.post(url='https://api.telegram.org/bot{0}/sendMessage'.format(config.apikey),
data={'chat_id': config.chatid, 'text': report}).json()Program convert the report text to English speech by using Google’s Text-to-Speech motor (GTTS) and save the speech file as mp3 into file system. Then, program transmitting this speech file in 77.0 FM frequency by using pi_fm_rds program in same directory.
tts = gTTS(text=report, lang='en')
tts.save("report.mp3")os.system('sox -t mp3 report.mp3 -t wav - | sudo PiFmRds/src/pi_fm_rds -audio - -freq 77.0 -ps KIRPI-FM')