Jadilah saya belajar bahasa asing lagi nih...ya untung saja sukses dan langkah belajarnya seperti ini untuk membantu pembaca memahami lebih mudah.
PYTHON + MQTT
import paho.mqtt.client as mqtt #import lib mqtt
led = 0
segmen = 0
############jika ada mqtt yg di sub masuk#########
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
global led
global segmen
client.publish("/test/seg","SEG"+ str(segmen))
print("Publishing message to topic LED & 7 SEG")
print("7 SEGMEN : " + str(segmen))
segmen+=1
if segmen == 10:
segmen = 0
if led == 0:
print("LED ON")
client.publish("/test/led","ON")
led = 1
else :
print("LED OFF")
client.publish("/test/led","OFF")
led = 0
print('')
print('')
########################################
broker_address="soldier.cloudmqtt.com"
broker_port=146xx
broker_user="xxxxxx"
broker_pass="xxxxxx"
print("creating new instance")
client = mqtt.Client("P1") #create new instance
client.on_message=on_message #attach function to callback
client.username_pw_set(broker_user,broker_pass)
print("connecting to broker")
client.connect(broker_address,broker_port) #connect to broker
#start the loop
print("Subscribing to topic","/test/DHT")
client.subscribe("/test/DHT")
client.loop_forever()
PYTHON + MONGODB
import pymongo
import json
from datetime import datetime
myclient = pymongo.MongoClient("mongodb+srv://user:pwd@cluster0-jb06l.mongodb.net/test?retryWrites=true&w=majority")
mydb = myclient["humitemp"]
mycol = mydb["data_humitemp"]
##########simpan DB###########
def save_db(isi):
dateTimeObj = datetime.now()
print('')
print("<===SAVE TO MONGODB===>")
print(dateTimeObj)
x=json.loads(isi)
y={"waktu":dateTimeObj , "suhu": x["suhu"] , "humi": x["humi"] }
z = mycol.insert_one(y)
#print list of the _id values of the inserted documents:
print("database masuk dengan id : ")
print(z.inserted_id)
print("========================")
print('')
###############BACA SUB MESSAGE#################
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
save_db(str(message.payload.decode("utf-8")))
################################
broker_address="soldier.cloudmqtt.com"
broker_port=146xx
broker_user="xxxx"
broker_pass="xxxxxx"
print("creating new instance")
client = mqtt.Client("P1") #create new instance
client.on_message=on_message #attach function to callback
client.username_pw_set(broker_user,broker_pass)
print("connecting to broker")
client.connect(broker_address,broker_port) #connect to broker
#start the loop
print("Subscribing to topic","/test/DHT")
client.subscribe("/test/DHT")
client.loop_forever()
Hasilnya tidak mengecewakan pada MONGODB atlas karena ada fasilitas report CHART grafik sehingga sangat menyingkat waktu coding
Nanti deh kita bahas kapan-kapan setelah pembaca mahir di PYTHON vs ARDUINO...selamat mumet kawan...