Sabtu, 04 Februari 2017

Koleksi Program2

import time
import serial

recipient = "+628568452535"
message = "Hello, World!"

phone = serial.Serial("com5",  460800, timeout=5)
try:
    time.sleep(0.5)
    phone.write(b'ATZ\r')
    time.sleep(0.5)
    phone.write(b'AT+CMGF=1\r')
    time.sleep(0.5)
    phone.write(b'AT+CMGS="' + recipient.encode() + b'"\r')
    time.sleep(0.5)
    phone.write(message.encode() + b"\r")
    time.sleep(0.5)
    phone.write(bytes([26]))
    time.sleep(0.5)
finally:
    phone.close()



serial con
import serial

import MySQLdb
from time import gmtime, strftime
from random import randint
from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.rotation = 180

db = MySQLdb.connect("localhost","root","password","db_project1")
cursor = db.cursor()

def mtrim(s):
    if s.endswith(" "): s = s[:-1]
    if s.startswith(" "): s = s[1:]
    return s


read_serial0=""
ser = serial.Serial('/dev/ttyACM0',9600)
#ser = serial.Serial('COM3',9600)

s = [0,1]
while True:
 read_serial=ser.readline()
 read_serial=mtrim(read_serial)
 c=read_serial.split("#")
 print (read_serial)
 print (str(len(c)))
 sleep(5)
 
 if len(c) >2:
     if read_serial!=read_serial0:
         
         read_serial0=read_serial
         idx=c[0]
         ruang=c[1]
         
         print ("idx=",idx)
         print ("ruang=",ruang)
         
         tgl=strftime("%Y-%m-%d", gmtime())
         jam=strftime("%H:%M:%S", gmtime())

         stgl=strftime("%Y%m%d", gmtime())
         sjam=strftime("%H%M%S", gmtime())

         img=   stgl+sjam+".jpg";

         ###########################################


         
         camera.start_preview()

         sleep(1)
         camera.capture('/var/www/html/project/ypathfile/'+img)
         sleep(1)
         camera.stop_preview()

         ###########################################
         sql='INSERT INTO `tb_history` (`tanggal`, `jam`, `ruang`, `ksj`, `image`, `note`) VALUES ("%s", "%s", "%s", "%s", "%s", "%s")' % (tgl, jam, ruang, idx, img,'-')
         try:
           cursor.execute(sql)
           db.commit()
         except:
           db.rollback()
          
     
         print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")


#$sudo -i
#cd /home/pi/Desktop/py
#python Help.py
#ls /dev/tty*


import RPi.GPIO as GPIO
import time

# Variable for the GPIO pin number
buzzer = 26

# Tell the Pi we are using the breakout board pin numbering
GPIO.setmode(GPIO.BCM)

# Set up the GPIO pin for output
GPIO.setup(buzzer, GPIO.OUT)

# Loop to blink our led
while True:
    GPIO.output(buzzer, GPIO.HIGH)
    print("On")
    time.sleep(1)
    GPIO.output(buzzer, GPIO.LOW)
    print("Off")
    time.sleep(1)



camera:
from picamera import PiCamera
from time import sleep

camera = PiCamera()

camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()



gabFinish
import serial

import MySQLdb
from time import gmtime, strftime
from random import randint
from picamera import PiCamera
from time import sleep

camera = PiCamera()
camera.rotation = 180

db = MySQLdb.connect("localhost","root","123456","db_project")
cursor = db.cursor()

db2 = MySQLdb.connect("192.168.1.38","root","","db_project")
cursor2 = db2.cursor()

def mtrim(s):
    if s.endswith(" "): s = s[:-1]
    if s.startswith(" "): s = s[1:]
    return s


read_serial0=""
ser = serial.Serial('/dev/ttyACM0',9600)
#ser = serial.Serial('COM3',9600)

s = [0,1]
while True:
 read_serial=ser.readline()
 read_serial=mtrim(read_serial)
 c=read_serial.split("#")
 print (read_serial)
 print (str(len(c)))
 sleep(5)
 
 if len(c) >2:
     if read_serial!=read_serial0:
         
         read_serial0=read_serial
         idx=c[1]
         ruang=c[2]
         
         print ("idx=",idx)
         print ("ruang=",ruang)
         
         tgl=strftime("%Y-%m-%d", gmtime())
         jam=strftime("%H:%M:%S", gmtime())

         stgl=strftime("%Y%m%d", gmtime())
         sjam=strftime("%H%M%S", gmtime())

         img=   stgl+sjam+".jpg";

         ###########################################


         
         camera.framerate = 15
         camera.start_preview()

         sleep(1)
         camera.capture('/var/www/html/project/ypathfile/'+img)
         sleep(1)
         camera.stop_preview()

         ###########################################
         sql='INSERT INTO `tb_history` (`tanggal`, `jam`, `ruang`, `ksj`, `image`, `note`) VALUES ("%s", "%s", "%s", "%s", "%s", "%s")' % (tgl, jam, ruang, idx, img,'-')
         try:
           cursor.execute(sql)
           db.commit()
         except:
           db.rollback()
          
         try:
           cursor2.execute(sql)
           db2.commit()
         except:
           db2.rollback()
         
         print("++++++++++++++++++++++++++++++++++++++++++++++++++")


#$sudo -i
#cd /home/pi/Desktop/py
#python Help.py
#ls /dev/tty*


Split
import serial

import MySQLdb
from time import gmtime, strftime
from random import randint


db = MySQLdb.connect("localhost","root","123456","monitoring" )
cursor = db.cursor()

def mtrim(s):
    if s.endswith(" "): s = s[:-1]
    if s.startswith(" "): s = s[1:]
    return s



read_serial0=""
ser = serial.Serial('/dev/ttyACM0',9600)
s = [0,1]
while True:
 read_serial=ser.readline()
 print read_serial
 read_serial=mtrim(read_serial)
 
 if len(read_serial) >6:
     if read_serial!=read_serial0:
         c=read_serial.split("#")
         read_serial0=read_serial
         I=c[0]
         V=c[1]
         W=c[2]
         P=c[3]
         Q=c[4]
         R=c[5]
         S=c[6]
         
         print "I=",I
         print "V=",V
         print "P=",W
         print "1=",P
         print "2=",Q
         print "3=",R
         print "4=",S

         tgl=strftime("%Y-%m-%d", gmtime())
         jam=strftime("%H:%M:%S", gmtime())

         sql = 'INSERT INTO `tb_history` ( `tanggal`, `jam`, `arus`, `tegangan`, `daya`,`L1`,`L2`,`L3`,`L4`) VALUES ("%s", "%s", "%s","%s","%s", "%s", "%s", "%s", "%s")' % (tgl, jam, I,V,W,P,Q,R,S)
         try:
           cursor.execute(sql)
           db.commit()
         except:
           db.rollback()
          
     
         print"++++++++++++++++++++++++++++++++++++++++++++++++++++"