Hihihi...tulisan ini bukan buat jual-beli kaset jadul ya kawan pembaca, ini cuman selingan agar tidak stress digigit ular python dengan scriptnya yang semakin memusingkan. Namun jika diambil untung ruginya ya banyak untungnya karena jika anda ingin bermain I/O menggunakan raspberry pi maka mau tidak mau harus memahami bahasa ULO. Jadi pada pembahasan kali ini kita lanjutkan MQTT pada tulisan sebelumnya, dan saya akan menjelaskan bagaimana caranya mengirim data penari ular yg kepanasan, dengan sensor DHT11 menuju smartphone. Tentunya dengan menggunakan protocol MQTT dan broker kesayangan HIVEMQ.
boot.py
import time from umqttsimple import MQTTClient import ubinascii import machine from machine import Pin import micropython import network import esp import dht esp.osdebug(None) import gc gc.collect() ssid = 'nama wifi' #sesuaikan dengan wifi kamu password = 'password' #broker dan topik yg digunakan mqtt_server = 'broker.hivemq.com' client_id = ubinascii.hexlify(machine.unique_id()) topic_suhu = b'/aisi555/suhu' topic_humi = b'/aisi555/humi' topic_ledesp = b'/aisi555/ledesp' topic_ledsck = b'/aisi555/ledsck' #delay pembacaan last_message = 0 message_interval = 10 #PIN GPIO sensor dan LED sensor = dht.DHT11(Pin(13)) #sesuaikan pin DHT ledESP = Pin(2, Pin.OUT) ledSCK = Pin(14, Pin.OUT) ledESP.value(1) #matikan dulu active low #koneksikan wifi station = network.WLAN(network.STA_IF) station.active(True) station.connect(ssid, password) while station.isconnected() == False: pass print('Koneksi Wifi Siap') ip=station.ifconfig() print('IP : {}'.format(ip[0]))
def sub_cb(topic, msg):
print('Pesan Diterima, Topik : {0} , Pesan :{1}'.format(topic, msg))
if (topic == topic_ledesp and msg == b'on'):
ledESP.value(0)
if (topic == topic_ledesp and msg == b'off'):
ledESP.value(1)
if (topic == topic_ledsck and msg == b'on'):
ledSCK.value(1)
if (topic == topic_ledsck and msg == b'off'):
ledSCK.value(0)
def connect_and_subscribe():
global client_id, mqtt_server, topic_sub
client = MQTTClient(client_id, mqtt_server)
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic_ledesp)
client.subscribe(topic_ledsck)
print('Terhubung ke MQTT broker %s' % mqtt_server)
return client
def read_sensor():
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print ('suhu : {0:3.1f}, humi: {1:3.1f}'.format(temp, hum))
return(temp,hum)
except OSError as e:
print ('dht error')
return(0,0)
def restart_and_reconnect():
print('Gagal Terhubung MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()
try:
client = connect_and_subscribe()
except OSError as e:
restart_and_reconnect()
while True:
try:
client.check_msg()
if (time.time() - last_message) > message_interval:
t,h = read_sensor()
client.publish(topic_suhu, str(t))
client.publish(topic_humi, str(h))
last_message = time.time()
except OSError as e:
restart_and_reconnect()
def connect_and_subscribe():
global client_id, mqtt_server, topic_sub
client = MQTTClient(client_id, mqtt_server)
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic_ledesp)
client.subscribe(topic_ledsck)
print('Terhubung ke MQTT broker %s' % mqtt_server)
return client
def restart_and_reconnect():
print('Gagal Terhubung MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()
def read_sensor():
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print ('suhu : {0:3.1f}, humi: {1:3.1f}'.format(temp, hum))
return(temp,hum)
except OSError as e:
print ('dht error')
return(0,0)
def sub_cb(topic, msg):
print('Pesan Diterima, Topik : {0} , Pesan :{1}'.format(topic, msg))
if (topic == topic_ledesp and msg == b'on'):
ledESP.value(0)
if (topic == topic_ledesp and msg == b'off'):
ledESP.value(1)
if (topic == topic_ledsck and msg == b'on'):
ledSCK.value(1)
if (topic == topic_ledsck and msg == b'off'):
ledSCK.value(0)
try:
client = connect_and_subscribe()
except OSError as e:
restart_and_reconnect()
while True:
try:
client.check_msg()
if (time.time() - last_message) > message_interval:
t,h = read_sensor()
client.publish(topic_suhu, str(t))
client.publish(topic_humi, str(h))
last_message = time.time()
except OSError as e:
restart_and_reconnect()