Kamis, 01 Maret 2018

AT COMMAND

Versi Python2

SEND:

import serial
import os, time
import RPi.GPIO as GPIO

port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)


#port.write('ATZ\r'.encode('UTF-8'))
port.write('ATZ\r')
rcv = port.read(10)
print(rcv)
time.sleep(1)

port.write('ATE0'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('AT+CMGF=1'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('AT+CNMI=2,1,0,0,0'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('AT+CMGS="085781062193"'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('Hello RIANN'+'\r')
rcv = port.read(10)
print (rcv)

#phone.write(bytes([26]))

port.write("\x1A")
for i in range(10):
    rcv = port.read(10)
    print (rcv)

=========================
CALL:

import serial
import os, time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)


port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)

port.write('AT'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('AT+CMGF=1'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('AT+CMGD=1'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('AT+CMGR=1'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('ATD085781062193;'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

while(1):
    rcv = port.read(10)
    print (rcv)


====================================
READ:
import serial
import os, time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)


port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)

port.write('AT'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

port.write('AT+CMGF=1'+'\r')
rcv = port.read(10)
print (rcv)
time.sleep(1)

#port.write(b'AT+CMGD=1'+b'\r') #VERSI PYTHON3
#rcv = port.read(10)
#print (rcv)
#time.sleep(1)

#port.write(b'AT+CMGR=1'+b'\r')
#rcv = port.read(10)
#print (rcv)
#time.sleep(1)

port.write('AT+CMGL="ALL"'+'\r')
rcv = port.read(20)
print (rcv)
time.sleep(1)

gab=""
#while(1):
for i in range(10):
    rcv = port.read(200)
    gab=gab+str(rcv)

print (gab)


===================
Python PYQT
import sys
from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import serial
import os, time
import RPi.GPIO as GPIO

class MainForm(QWidget):

   def __init__(self):
      super(MainForm, self).__init__()
      self.setupUi()

   def buttonClick(self):
      global gab
      global port
      global kirim

      print("Status RX=" + str(kirim))
      cmd= self.txtcmd.text()
      cmd2=cmd
      gab2=""
      
      if kirim==1:
         port.write(cmd+'\r')
         rcv = port.read(10)
         print (rcv)
         port.write("\x1A")
         for i in range(10):
            rcv = port.read(10)
            gab2=gab2+str(rcv)
            print (rcv)
            kirim=2   

      if (cmd.find('AT+CMGS')>=0):
         port.write(cmd+'\r')
         self.label1.setText('Tulis Pesan Anda')
         print("Send SMS")
         rcv = port.read(10)
         print (rcv)
         time.sleep(1)
         kirim=1
      elif (cmd.find('AT+CMGL')>=0):
         port.write(cmd+'\r')
         print("Read SMS")
         rcv = port.read(20)
         print (rcv)
         time.sleep(1)
         #while(1):
         for i in range(10):
             rcv = port.read(200)
             print(rcv)
             gab2=gab2+str(rcv)
             kirim=2
             
      elif kirim==0:
         cmd=cmd+"\r"
         port.write(cmd)
         rcv = port.read(200)
         print(rcv)
    

      if kirim==2:
         gab=gab+gab2
         kirim=0
         self.label1.setText('Command')
      else:
         gab=gab+cmd2+"="+str(rcv)+'\n'
         print(gab)

      self.txtresult.setText(gab)
      self.txtcmd.setText('')

   
   def bersih(self):
      self.txtcmd.setText('')
      self.txtresult.setText('')
      
   def setupUi(self):
      self.resize(400, 200)
      self.move(300, 300)
      self.setWindowTitle('AT COMMAND STUDY')
      
      self.label1 = QLabel()
      self.label1.setText('Command')
      
      self.txtcmd = QLineEdit()
      vbox1 = QVBoxLayout()
      vbox1.addWidget(self.label1)
      vbox1.addWidget(self.txtcmd)
      
      self.label2 = QLabel()
      self.label2.setText('Result')      
      self.txtresult = QTextEdit()
      
      vbox2 = QVBoxLayout()
      vbox2.addWidget(self.label2)
      vbox2.addWidget(self.txtresult)    
      
      vbox3 = QVBoxLayout()
      vbox3.addLayout(vbox1)
      vbox3.addLayout(vbox2)
      
      self.sendButton = QPushButton('&EXE')
      self.sendButton.clicked.connect(self.buttonClick)
      
      self.cancelButton = QPushButton('&CLEAR')
      self.cancelButton.clicked.connect(self.bersih)
      
      hbox = QHBoxLayout()
      hbox.addStretch()
      hbox.addWidget(self.sendButton)
      hbox.addWidget(self.cancelButton)
      
      layout = QVBoxLayout()      
      layout.addLayout(vbox3)
      horizontalLine = QFrame();
      horizontalLine.setFrameShape(QFrame.HLine)
      horizontalLine.setFrameShadow(QFrame.Sunken)
      layout.addWidget(horizontalLine)
      layout.addLayout(hbox)
      self.setLayout(layout)

global gab
gab=""
global port
port = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
global kirim
kirim=0
   
if __name__ == '__main__':
   
   a = QApplication(sys.argv)
   form = MainForm()
   form.show()
   a.exec_()

==================================
THREADING:
from PyQt5 import QtCore, QtGui, QtWidgets
import serial
import RPi.GPIO as GPIO
import signal
import MFRC522
import MySQLdb


import time, threading
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtWidgets

from picamera import PiCamera
from time import sleep

GPIO.setwarnings(False)

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

         

class Ui_MainWindow(object):
     
    def setupUi(self,MainWindow):
        desktop = QDesktopWidget()
     
        screenwidth = desktop.screen().width()+100
        screenheight = desktop.screen().height()+100
        top=screenheight/7
        bottom=screenheight-top-top-100
   
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(screenwidth, screenheight)
        MainWindow.setStyleSheet("background-color: #003366;")
     
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.lbl = QtWidgets.QLabel(self.centralwidget)
        self.lbl.setGeometry(QtCore.QRect(-5, top-100, screenwidth, 30))
        self.lbl.setStyleSheet("color: #FFFFFF;font:Bold")
        self.lbl.setFont(QFont('Xtreme Chrome',25))
        self.lbl.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl.setObjectName("lbl")

        self.lblFoto = QtWidgets.QLabel(self.centralwidget)
        self.lblFoto.setGeometry(QtCore.QRect(-5, top+30, screenwidth, 300))
        self.lblFoto.setStyleSheet("background-color: #000000; color: #FFFF00;font:Bold")
        self.lblFoto.setFixedHeight(300)
        self.lblFoto.setAlignment(QtCore.Qt.AlignCenter)
        self.lblFoto.setFont(QFont('Xtreme Chrome',250))
        #self.lblFoto.resize(1700,1700) #pixmap.width(),pixmap.height()


        self.lblKaryawan = QtWidgets.QLabel(self.centralwidget)
        self.lblKaryawan.setGeometry(QtCore.QRect(-5, bottom, screenwidth, 100))
        self.lblKaryawan.setStyleSheet("background-color: #cccccc; color: #ff0000;font:Bold")
        self.lblKaryawan.setFixedHeight(110)
        self.lblKaryawan.setAlignment(QtCore.Qt.AlignCenter)
        self.lblKaryawan.setFont(QFont('Xtreme Chrome',110))
        self.lblKaryawan.setObjectName("lblKaryawan")
     
    ##################
     
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 259, 21))
        self.menubar.setObjectName("menubar")
     
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "   "))
        #self.lblFoto.setText(_translate("MainWindow", "IMAGE KARYAWAN"))
        pixmap = QPixmap('avatar.jpg')
        self.lblFoto.setPixmap(pixmap)
        #self.lblFoto.resize(pixmap.width()*2,pixmap.height()*2)

        self.lblKaryawan.setText(_translate("MainWindow", "NAMA KARYAWAN (NIPXXXXX)"))
        self.lbl.setText(_translate("MainWindow", "APLIKASI ABSENSI"))
     
        # User Code
        self.timeout = 0
        self.check_serial_event()
     
     
    def check_serial_event(self):
        global camera
        serial_thread = threading.Timer(1, self.check_serial_event)
        serial_thread.start()
     
       # signal.signal(signal.SIGINT, 1)
        MIFAREReader = MFRC522.MFRC522()
        print ("...")
        time.sleep(1)
        (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
        if status == MIFAREReader.MI_OK:
            print ("...")
            (status,uid) = MIFAREReader.MFRC522_Anticoll()
         
         
        if status == MIFAREReader.MI_OK:
            AL=str(uid[0])+" "+str(uid[1])+" "+str(uid[2])+" "+str(uid[3])
            print ("UID: "+AL)
            self.lblKaryawan.setText(AL)

            #camera.start_preview(alpha=200)
            #camera.start_preview()
            #sleep(1)
            AL='/home/pi/Desktop/image.jpg'
            camera.capture(AL)
            #camera.stop_preview()

            pixmap = QPixmap(AL)
            self.lblFoto.setPixmap(pixmap)
         

if __name__ == "__main__":
    import sys

    camera = PiCamera()
    #camera.rotation = 180
    camera.resolution = (300, 300)
    #camera.framerate = 15

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())