import paho.mqtt.client as mqtt
import datetime
import time

def on_connect(client, userdata,flags,rc):
    if rc == 0:

         print("Connected successfully.")
    else:
         print("Connection failed. rc= "+str("rc"))

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribe with mid "+str(mid)+" received.")

    
def on_message(client, userdata, msg):
    print("Message received on topic "+msg.topic+" with QoS "+str(msg.qos) )
    chemin="/home/pi/Desktop/New/enregi/"
    fa= time.strftime( '%Y %m %d %H:%M:%S')
    f=open(chemin+fa +".jpg","wb")
    f.write(msg.payload)
    f.close()
    

SNAP_Image=0

mqttclient = mqtt.Client()

# Assign event callbacks
mqttclient.on_connect = on_connect
mqttclient.on_subscribe=on_subscribe
mqttclient.on_message = on_message


# Connect
mqttclient.connect("broker.hivemq.com", 1883,60)

# Start subscription
mqttclient.subscribe("Projiut/Images/Eau")
mqttclient.subscribe("Eau/image")

mqttclient.loop_start()
