RaspPyTelebot
RaspPyTelebot (RaspberryPi + Python + TelegramBot)
In this tutorial we are going to use TelegramBot with Python Code to connect to RasberryPi Camera and Send us the picture via Telegram.
* Hardware
- Rasberry Pi
- Camera Pi
* Software
- Python 2.7.x
- TelegramBot (You need to have one, We will used its Token in the script)
We going to use short short to control via telegram:
/ap = ping Access Point IP Address (Up/Down)
/ip = get Raspberry Pi IP Address
/pic = take the picture and send us via Telegram
This is the code script sample (Keep these two files in the same folder) :
- telebot.py
- ip_address.sh
======================= telebot.py =========================
import os
import telepot
import time
from pyping.core import *
import platform, socket, pyping
from picamera import PiCamera
from time import sleep
camera = PiCamera()
CUR_PWD = os.getcwd()
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
if msg['text'] == '/ap':
ap1='192.168.1.1'
ping_ap1 = pyping.ping(ap1)
bot.sendMessage(chat_id, 'Pleas wait...')
if ping_ap1.ret_code == 0:
print(ap1 + " is up")
bot.sendMessage(chat_id, "Access Point " + ap1 + " Up")
else:
print(ap1 + " is down")
bot.sendMessage(chat_id, "Access Point" + ap1 + " Down")
if msg['text'] == '/ip':
ip = os.system("bash ip_address.sh")
bot.sendMessage(chat_id, 'Pleas wait...')
#time.sleep(1)
f = open(CUR_PWD + "/ip_address.txt", "r")
ip_add = f.read()
bot.sendMessage(chat_id, "Pi IP Address: " + ip_add)
print("Pi IP Address: " + ip_add)
f.close()
if msg['text'] == '/pic':
bot.sendMessage(chat_id, 'Pleas wait...')
#time.sleep(2)
#camera.start_preview()
time.sleep(2)
camera.capture(CUR_PWD + '/image.jpg')
#camera.stop_preview()
bot.sendDocument(chat_id, open(CUR_PWD + '/image.jpg', 'rb'))
print('sent image.jpg')
def main():
global bot
bot = telepot.Bot('112233445:AAbbccddeeffgghhii_jjkkllmnopqust')
bot.message_loop(handle, run_forever=True)
main()
==================== ip_address.sh =========================
#!/bin/bash
ip ad |grep wlan |grep inet |cut -d" " -f6 |cut -d"/" -f1 > $(pwd)/ip_address.txt
No comments:
Post a Comment