Advertisement
Guest User

client.py

a guest
Jan 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2. import json
  3. import lcddriver
  4. import time
  5. import datetime
  6. import RPi.GPIO as GPIO
  7. import SimpleMFRC522
  8.  
  9. import keypad
  10.  
  11. broker="m11.cloudmqtt.com"
  12. port=13379
  13. mqttuser="gmpgtlol"
  14. mqttpass="g1-QffVy6c_T"
  15. subtopic="fb"
  16. pubtopic="ta"
  17. reader = SimpleMFRC522.SimpleMFRC522()
  18. display = lcddriver.lcd()
  19. client = mqtt.Client()
  20. client.username_pw_set(mqttuser, mqttpass)
  21.  
  22. def update_jmd():
  23. now = datetime.datetime.now()
  24. jam = '%02d' % now.hour
  25. menit = '%02d' % now.minute
  26. detik = '%02d' % now.second
  27. jmd = str(jam)+":"+str(menit)+":"+str(detik)
  28. return jmd
  29.  
  30. def update_tbt():
  31. now = datetime.datetime.now()
  32. tahun = '%02d' % now.year
  33. bulan = '%02d' % now.month
  34. tanggal = '%02d' % now.day
  35. tbt = str(tanggal)+"/"+str(bulan)+"/"+str(tahun)
  36. return tbt
  37.  
  38. def on_connect(client, userdata, flags, rc):
  39. print("Connected with result code "+str(rc))
  40. client.subscribe(subtopic)
  41. jmd = update_jmd()
  42. tbt = update_tbt()
  43. display.lcd_display_string("MQTT Telah Terhubung", 3)
  44. display.lcd_display_string(jmd+" "+tbt, 4)
  45.  
  46. # The callback for when a PUBLISH message is received from the server.
  47. def on_message(client, userdata, msg):
  48. print(msg.topic+" "+str(msg.payload))
  49. pl=json.loads(msg.payload) # pl = payload load
  50. req = pl['req']
  51. jmd = pl['jmd']
  52. tbt = pl['tbt']
  53.  
  54. if req == 'daftar':
  55. display.lcd_clear()
  56. display.lcd_display_string("Registrasi Berhasil ", 1)
  57. elif req == 'update':
  58. display.lcd_clear()
  59. display.lcd_display_string(" Update Berhasil ", 1)
  60. elif req == 'cek':
  61. display.lcd_clear()
  62. display.lcd_display_string(" Pengecekan Saldo ", 1)
  63. else:
  64. pass
  65. display.lcd_display_string("Nama : "+pl['nama'], 2)
  66. display.lcd_display_string("Saldo : "+pl['saldo'], 3)
  67. display.lcd_display_string(pl['jmd']+" "+pl['tbt'], 4)
  68.  
  69. def on_ktm():
  70. rfid, nim = reader.read()
  71. print (rfid)
  72. print (nim)
  73. client.publish(pubtopic,nim)
  74.  
  75. def start():
  76. jmd = update_jmd()
  77. tbt = update_tbt()
  78. display.lcd_clear()
  79. display.lcd_display_string(" Charging Station ",1)
  80. display.lcd_display_string(" Menghubungkan MQTT ",2)
  81. display.lcd_display_string(jmd+" "+tbt,4)
  82. client.connect(broker,port,60)
  83. display.lcd_display_string("MQTT Telah Terhubung",3)
  84. jmd = update_jmd()
  85. tbt = update_tbt()
  86. display.lcd_display_string(jmd+" "+tbt,4)
  87. time.sleep(1)
  88.  
  89. def menu():
  90. jmd = update_jmd()
  91. tbt = update_tbt()
  92. display.lcd_clear()
  93. display.lcd_display_string("A Cek Saldo",1)
  94. display.lcd_display_string("B Daftar",2)
  95. display.lcd_display_string("C Apakek", 3)
  96. display.lcd_display_string("<<*<< "+jmd+" >>#>>",4)
  97. key = keypad.baca_char()
  98. display.lcd_clear()
  99. if key == "A":
  100. import ceksaldo
  101. print("quit ceksaldo.py")
  102. menu()
  103. elif key == "B":
  104. import daftar
  105. print("quit daftar.py")
  106. menu()
  107. start()
  108. menu()
  109. client.on_message = on_message
  110. client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement