Exemple #1
0
def check_threshold_workshop(client, ctrl_time_dict, curr_pump_dict,
                             thres_dict, data):
    print_threshold(data, thres_dict)

    shelf_id = thres_dict["shelf_id"]
    if (trigger_fertilizer(data, thres_dict)):
        if (int(data["fertilizer"]) == 0):
            print("\nALERT Cannot trigger fertilizer pump")
            mqtt_pub_cloud_msg(
                client,
                prepare_low_water_notification_message_obj("fer", shelf_id))
            return
        else:
            mqtt_pub_cloud_msg(
                client,
                prepare_gpio_pump_notification_message_obj(
                    "fer", "on", shelf_id))
            pump_ctrl("FER", "ON")
            time.sleep(ctrl_time_dict["ctrl_interval"])
            pump_ctrl("FER", "OFF")
Exemple #2
0
def update_pump_status(client, mqtt_msg_str, curr_parameters):
    generate_msg = False
    #-----------#
    #  Interval #
    #-----------#
    if (mqtt_msg_str == "on"):
        print("Activate USB")
        cmd_output = pump_ctrl("PI_USB", "ON")
        print(cmd_output)
        if (cmd_output == ''):
            curr_parameters["pump"]["stat"] = True
            generate_msg = True
            print("Pump is ON")
        else:
            curr_parameters["pump"]["stat"] = True
            print("Pump is already ON")
    #---------------#
    #  Temperature  #
    #---------------#
    elif(mqtt_msg_str == "off"):
        print("Deactivate USB")
        cmd_output = pump_ctrl("PI_USB", "OFF")
        print(cmd_output)
        if (cmd_output == ''):
            curr_parameters["pump"]["stat"] = False
            generate_msg = True
            print("Pump is OFF")
        else:
            curr_parameters["pump"]["stat"] = False
            print("Pump is already OFF")
    if (generate_msg):
        pump_msg = prepare_usb_notification_message_obj(
            curr_parameters["pump"]["stat"], curr_parameters["threshold"]["shelf_id"])
        mqtt_pub_msg(client, PUB_CLOUD_TOPIC['pub_msg'], str(pump_msg))

    return curr_parameters
Exemple #3
0
def check_threshold(client, ctrl_time_dict, curr_pump_dict, thres_dict, data):
    print_threshold(data, thres_dict)
    # if (ctrl_time_dict["last_check"] is not None):
    #     # Check for last check duration
    #     curr_time = get_curr_datetime()
    #     time_elapse = get_time_difference_in_sec(ctrl_time_dict["last_check"], curr_time)
    #     if (time_elapse < ctrl_time_dict["check_interval"]):
    #         print ("    Delay checks for " + str(time_elapse))
    #         return

    # ctrl_time_dict["last_check"] = get_curr_datetime()
    shelf_id = thres_dict["shelf_id"]
    if (trigger_fertilizer(data, thres_dict)):
        if (int(data["fertilizer"]) == 0):
            print("\nALERT Cannot trigger fertilizer pump")
            mqtt_pub_cloud_msg(
                client,
                prepare_low_water_notification_message_obj("fer", shelf_id))
            return
        else:
            mqtt_pub_cloud_msg(
                client,
                prepare_gpio_pump_notification_message_obj(
                    "fer", "on", shelf_id))
            pump_ctrl("FER", "ON")
            time.sleep(ctrl_time_dict["ctrl_interval"])
            pump_ctrl("FER", "OFF")
            # mqtt_pub_cloud_msg(client, prepare_gpio_pump_notification_message_obj("fer", "off", shelf_id))

    elif (trigger_water(data, thres_dict)):
        if (int(data["water"]) == 0):
            print("\nALERT Cannot trigger water pump")
            mqtt_pub_cloud_msg(
                client,
                prepare_low_water_notification_message_obj("water", shelf_id))
            return
        else:
            mqtt_pub_cloud_msg(
                client,
                prepare_gpio_pump_notification_message_obj(
                    "water", "on", shelf_id))
            pump_ctrl("WATER", "ON")
            time.sleep(ctrl_time_dict["ctrl_interval"])
            pump_ctrl("WATER", "OFF")
Exemple #4
0
def mqtt_main(comm_with, topic_list):
    print("\nCommunicate with - " + str(comm_with))
    print("Subscribing MQTT Topic - " + str(topic_list))
    pump_ctrl("RESET", "OFF")

    global SUBSCRIBE_TOPIC
    global COMMUNICATE_WITH
    global PUB_TIME_DICT
    global CTRL_TIME_DICT
    global THRESHOLD_DICT
    global CURR_PUMP_DICT
    global READ_SERIAL
    global SER
    global C_SOCKET

    SUBSCRIBE_TOPIC = topic_list
    COMMUNICATE_WITH = comm_with
    PUB_TIME_DICT = {
        "pub_interval": CFP.PUB_DATA_INTERVAL_IN_SEC,
        "store_interval": CFP.STORE_DATA_INTERVAL_IN_SEC,
        "last_pub": None,
        "last_store": None
    }
    CTRL_TIME_DICT = {
        "check_interval": CFP.CHECK_INTERVAL_IN_SEC,
        "ctrl_interval": CFP.CTRL_INTERVAL_IN_SEC,
        "last_check": None,
        "last_ctrl": None,
        "ctrl_pump": None,
        "ctrl_method": "AUTO",
    }
    THRESHOLD_DICT = CFP.THRESHOLD_DICT
    CURR_PUMP_DICT = CFP.CURR_PUMP_STAT

    print("Total MQTT topic to subscribe : " + str(len(SUBSCRIBE_TOPIC)))
    print("Topic to subscribe : " + str(len(SUBSCRIBE_TOPIC)))

    client = mqtt.Client(client_id="RPi_" + CFP.SEN_SERIAL + "-" +
                         str(comm_with))
    l_client = mqtt.Client(client_id="RPi_" + CFP.SEN_SERIAL + "- local")

    #-----------------------------#
    # MQTT Authentication Method  #
    #-----------------------------#
    if (CPS.MQTT_ENABLE_AUTH):
        if (CPS.MQTT_AUTH_METHOD == "password"):
            client.username_pw_set(CPS.MQTT_USERNAME,
                                   password=CPS.MQTT_PASSWORD)

    #----------------------------------#
    # Attach Callback Function - CLOUD #
    #----------------------------------#
    client.on_connect = on_connect
    client.on_message = on_message
    client.on_publish = on_publish
    client.on_subscribe = on_subscribe
    # client.on_log = on_log
    #----------------------------------#
    # Attach Callback Function - LOCAL #
    #----------------------------------#
    l_client.on_connect = on_local_connect
    l_client.on_message = on_local_message
    l_client.on_publish = on_local_publish
    l_client.on_subscribe = on_local_subscribe

    #----------------------------#
    # Establish MQTT Connection  #
    #----------------------------#
    client.connect(CPS.MQTT_CLOUD_SERVER, CPS.MQTT_PORT, CPS.MQTT_KEEPALIVE)
    l_client.connect(CPS.MQTT_LOCAL_SERVER, CPS.MQTT_PORT, CPS.MQTT_KEEPALIVE)
    # if (CPS.MQTT_SERVER_TYPE == "cloud"):
    #     client.connect(CPS.MQTT_CLOUD_SERVER, CPS.MQTT_PORT, CPS.MQTT_KEEPALIVE)
    # else:
    #     client.connect(CPS.MQTT_LOCAL_SERVER, CPS.MQTT_PORT, CPS.MQTT_KEEPALIVE)

    # Blocking call that processes network traffic, dispatches callbacks and
    # handles reconnecting.
    # Other loop*() functions are available that give a threaded interface and a
    # manual interface.
    if (CFP.APPLICATION_TYPE == "WORKSHOP"):
        schedule.every().minute.at(":00").do(check_thres_pub_sensor_data,
                                             CFP.APPLICATION_TYPE, client,
                                             CTRL_TIME_DICT, CURR_PUMP_DICT,
                                             THRESHOLD_DICT)
    else:
        schedule.every().hour.at(":00").do(check_thres_pub_sensor_data,
                                           CFP.APPLICATION_TYPE, client,
                                           CTRL_TIME_DICT, CURR_PUMP_DICT,
                                           THRESHOLD_DICT)
    #---------------#  Get ShelfID - To ensure it is assigned
    # RUN API CALLS #  Get Threshold - If not set, add one
    #---------------#
    need_resync = sync_cloud_thres_info(CFP.SEN_SERIAL, THRESHOLD_DICT)
    last_resync_check = None

    #--------------------#
    # Endless Publisher  #  https://stackoverflow.com/questions/23909292/publish-a-message-every-10-seconds-mqtt
    #--------------------#
    SER = serial.Serial('/dev/ttyACM0', CFP.ARDUINO_BRAUD_RATE)
    C_SOCKET = {"sid": ""}
    client.loop_start()
    l_client.loop_start()
    read_serial = ""
    READ_SERIAL = {"data": read_serial}
    while True:
        readSerialAndPub(client, l_client)
        schedule.run_pending()
        #data = get_pub_sensor_data(client, PUB_TIME_DICT, CURR_PUMP_DICT, read_serial)

        # check_threshold(client, CTRL_TIME_DICT, CURR_PUMP_DICT,THRESHOLD_DICT, data)
        if (need_resync):  # Check once per 300 seconds
            if (last_resync_check is not None):
                curr_time = get_curr_datetime()
                time_elapse = get_time_difference_in_sec(
                    last_resync_check, curr_time)

                if (time_elapse < 300):
                    continue
            last_resync_check = get_curr_datetime()
            need_resync = sync_cloud_thres_info(CFP.SEN_SERIAL, THRESHOLD_DICT)
Exemple #5
0
def check_threshold_sr(client, ctrl_time_dict, curr_pump_dict, thres_dict,
                       data):
    print_threshold(data, thres_dict)

    shelf_id = thres_dict["shelf_id"]
    if (int(data["water"]) == 0):
        print("\nLow water")
        # if (int(data["fertilizer"]) == 0):
        #     print("\nALERT Cannot trigger water - Low water supply")
        #     mqtt_pub_cloud_msg(client,  prepare_low_water_notification_message_obj("water", shelf_id))
        #     # return  # Disabled to continue checking the threshold
        # else:
        mqtt_pub_cloud_msg(
            client,
            prepare_gpio_pump_notification_message_obj("water", "on",
                                                       shelf_id))
        pump_ctrl("WATER", "ON")
        time.sleep(10)  #For squareRoot
        pump_ctrl("WATER", "OFF")

    if (trigger_fertilizer(data, thres_dict)):
        if (int(data["fertilizer"]) == 0):
            print("\nALERT Cannot trigger fertilizer pump")
            mqtt_pub_cloud_msg(
                client,
                prepare_low_water_notification_message_obj("fer", shelf_id))
            return
        else:
            mqtt_pub_cloud_msg(
                client,
                prepare_gpio_pump_notification_message_obj(
                    "fer", "on", shelf_id))
            pump_ctrl("FER", "ON")
            time.sleep(ctrl_time_dict["ctrl_interval"])
            pump_ctrl("FER", "OFF")
            # mqtt_pub_cloud_msg(client,prepare_gpio_pump_notification_message_obj("fer", "off", shelf_id))

    elif (trigger_water(data, thres_dict)):
        # if (int(data["fertilizer"]) == 0):
        #     print("\nALERT Cannot trigger water pump")
        #     mqtt_pub_cloud_msg(client, prepare_low_water_notification_message_obj("water", shelf_id))
        #     return
        # else:
        mqtt_pub_cloud_msg(
            client,
            prepare_gpio_pump_notification_message_obj("water", "on",
                                                       shelf_id))
        pump_ctrl("WATER", "ON")
        time.sleep(ctrl_time_dict["ctrl_interval"])
        pump_ctrl("WATER", "OFF")