Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Thursday, August 13, 2020

RaspPyTelebot

 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


Thursday, November 10, 2016

Telnet Switch And Shutdown Port - Python

Telnet Switch And Shutdown Port - Python

In this Example is going to shutdown and no shutdown on port: FastEthernet0/1

SHUTDOWN

#!/usr/bin/python
import getpass
import sys
import telnetlib

HOST = "123.123.123.155"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

telnet = telnetlib.Telnet(HOST)

telnet.read_until("username: ",3)
telnet.write(user + "\n")
if password:
   telnet.read_until("password:",3)
   telnet.write(password + "\n")

telnet.write("show ip int br\n")
telnet.write("configure terminal\n")
telnet.write("interface FastEthernet0/1\n")
telnet.write("shutdown\n")
telnet.write("end\n")
telnet.write("wr\n")
telnet.write("exit\n")
print "###### ALL COMMAND RUNNING #####"

print telnet.read_all()



NO SHUTDOWN

#!/usr/bin/python
import getpass
import sys
import telnetlib

HOST = "123.123.123.155"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

telnet = telnetlib.Telnet(HOST)

telnet.read_until("username: ",3)
telnet.write(user + "\n")
if password:
   telnet.read_until("password:",3)
   telnet.write(password + "\n")

telnet.write("show ip int br\n")
telnet.write("configure terminal\n")
telnet.write("interface FastEthernet0/1\n")
telnet.write("no shutdown\n")
telnet.write("end\n")
telnet.write("wr\n")
telnet.write("exit\n")
print "###### ALL COMMAND RUNNING #####"
print telnet.read_all()



Wednesday, November 9, 2016

Loop In Turtle

Loop In Turtle

# SQUARE
python2.7
import turtle
t = turtle.Pen()
for i in range(0,4):
    t.forward(50)
    t.left(90)

# 8 SQUARE
python2.7
import turtle
t = turtle.Pen()
for i in range(0,8):
    t.forward(50)
    t.left(45)

# STAR
python2.7
import turtle
t = turtle.Pen()
for i in range(0,5):
    t.forward(100)
    t.right(144)

# S.TH
python2.7
import turtle
t = turtle.Pen()
for i in range(1,76):
    t.forward(222)
    t.left(175)

# S.TH
python2.7
import turtle
t = turtle.Pen()
for i in range(1,144):
    t.forward(333)
    t.left(175)

# S.TH
python2.7
import turtle
t = turtle.Pen()
for i in range(0,30):
    t.forward(333)
    t.right(156)

Saturday, November 5, 2016

Guessing Number Python Game

Guessing Number Python Game

#!/usr/bin/python
import random
computerGuess=random.randint(0,100)
while True:
    userGuess=int(input("Guess a number between 0 - 100: "))
    if userGuess > computerGuess:
print ("Guess Lower")
    elif userGuess < computerGuess:
print ("Guess Higher")
    else:
print ("Congrate, You are correct.")
break