Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paho.mqtt.client as mqtt
- import json
- import lcddriver
- import time
- import datetime
- import RPi.GPIO as GPIO
- import SimpleMFRC522
- import keypad
- broker="m11.cloudmqtt.com"
- port=13379
- mqttuser="gmpgtlol"
- mqttpass="g1-QffVy6c_T"
- subtopic="fb"
- pubtopic="ta"
- reader = SimpleMFRC522.SimpleMFRC522()
- display = lcddriver.lcd()
- client = mqtt.Client()
- client.username_pw_set(mqttuser, mqttpass)
- def update_jmd():
- now = datetime.datetime.now()
- jam = '%02d' % now.hour
- menit = '%02d' % now.minute
- detik = '%02d' % now.second
- jmd = str(jam)+":"+str(menit)+":"+str(detik)
- return jmd
- def update_tbt():
- now = datetime.datetime.now()
- tahun = '%02d' % now.year
- bulan = '%02d' % now.month
- tanggal = '%02d' % now.day
- tbt = str(tanggal)+"/"+str(bulan)+"/"+str(tahun)
- return tbt
- def on_connect(client, userdata, flags, rc):
- print("Connected with result code "+str(rc))
- client.subscribe(subtopic)
- jmd = update_jmd()
- tbt = update_tbt()
- display.lcd_display_string("MQTT Telah Terhubung", 3)
- display.lcd_display_string(jmd+" "+tbt, 4)
- # The callback for when a PUBLISH message is received from the server.
- def on_message(client, userdata, msg):
- print(msg.topic+" "+str(msg.payload))
- pl=json.loads(msg.payload) # pl = payload load
- req = pl['req']
- jmd = pl['jmd']
- tbt = pl['tbt']
- if req == 'daftar':
- display.lcd_clear()
- display.lcd_display_string("Registrasi Berhasil ", 1)
- elif req == 'update':
- display.lcd_clear()
- display.lcd_display_string(" Update Berhasil ", 1)
- elif req == 'cek':
- display.lcd_clear()
- display.lcd_display_string(" Pengecekan Saldo ", 1)
- else:
- pass
- display.lcd_display_string("Nama : "+pl['nama'], 2)
- display.lcd_display_string("Saldo : "+pl['saldo'], 3)
- display.lcd_display_string(pl['jmd']+" "+pl['tbt'], 4)
- def on_ktm():
- rfid, nim = reader.read()
- print (rfid)
- print (nim)
- client.publish(pubtopic,nim)
- def start():
- jmd = update_jmd()
- tbt = update_tbt()
- display.lcd_clear()
- display.lcd_display_string(" Charging Station ",1)
- display.lcd_display_string(" Menghubungkan MQTT ",2)
- display.lcd_display_string(jmd+" "+tbt,4)
- client.connect(broker,port,60)
- display.lcd_display_string("MQTT Telah Terhubung",3)
- jmd = update_jmd()
- tbt = update_tbt()
- display.lcd_display_string(jmd+" "+tbt,4)
- time.sleep(1)
- def menu():
- jmd = update_jmd()
- tbt = update_tbt()
- display.lcd_clear()
- display.lcd_display_string("A Cek Saldo",1)
- display.lcd_display_string("B Daftar",2)
- display.lcd_display_string("C Apakek", 3)
- display.lcd_display_string("<<*<< "+jmd+" >>#>>",4)
- key = keypad.baca_char()
- display.lcd_clear()
- if key == "A":
- import ceksaldo
- print("quit ceksaldo.py")
- menu()
- elif key == "B":
- import daftar
- print("quit daftar.py")
- menu()
- start()
- menu()
- client.on_message = on_message
- client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement