def meteoroData():

    failed = 0
    try:
        x = mraa.Uart(0)
        #------- Opening port and sending command------
        ser = serial.Serial('/dev/ttyACM0', 115200)
        ser.bytesize = serial.EIGHTBITS
        ser.parity = serial.PARITY_NONE
        ser.stopbits = serial.STOPBITS_ONE
        ser.timeout = 2

        ser.write("LOGIN=0047\r\n")
        counter = "READ\r\n"
        ser.write(counter)
        print("Sent: " + str(counter))

        #-------Lecturas----------
        r = ser.readlines()
        temp = r[4].replace("\xb0C\r\n", "").replace(",", ".")
        hum = r[5].replace("%\r\n", "").replace(",", ".")
        brood = r[6].replace("\xb0C B\r\n", "").replace(",", ".")
        lit = r[7].replace("l/h\r\n", "").replace(",", ".")
        km = r[8].replace("km/h\r\n", "").replace(",", ".")
        print("Weight: " + r[3].replace("kg\r\n", ""))
        print("Temperature: " + temp)
        print("Humidity: " + hum)
        print("Brood: " + brood)
        print("Liters per hour: " + lit)
        print("Kilometers per hour: " + km)
        print("Failed: " + str(failed))

        ser.close()

        #------Comprobacion------
        float(temp)
        float(hum)
        float(brood)
        float(lit)
        float(km)

        #-----Insercion---------
        pubTempI = insert_observation.insert(temp, "Temperature Int")
        pubHum = insert_observation.insert(hum, "Humidity")
        pubBrood = insert_observation.insert(brood, "Brood")
        pubPluvi = insert_observation.insert(lit, "Pluviometer")
        pubAnemo = insert_observation.insert(km, "Anemometer")

        #--------Publicacion a horas---------
        if (tRet.isTimeToSend()):
            mosquitto_publisher.publisher("TemperatureI", pubTempI)
            mosquitto_publisher.publisher("Humidity", pubHum)
            mosquitto_publisher.publisher("Brood", pubBrood)
            mosquitto_publisher.publisher("Pluviometer", pubPluvi)
            mosquitto_publisher.publisher("Anemometer", pubAnemo)

    except:
        print("Something went wrong, comunication or data, nothing done.")
        failed = failed + 1
def write_log(reader_sn, epc_list, log_time):
    """
    Write the tags (bee ids) stored in epc_list variable into a currently active log file.
    :param epc_list: list of bee ids been read by the RFID reader
    :param log_time: reading time
    """
    # Global variable declaration to access global variable active_date
    # and global variable active_log_file_name
    global active_date
    global active_log_file_name

    # Create a new log file is the current system date is different
    # than the date stored in the global variable global active_date
    if active_date != time.strftime("%Y.%m.%d"):
        create_new_log_file()

    epc_log = ''
    epc_list = set(epc_list)
    conn_string = "host='localhost' dbname='iot-sensors' user='******' password='******'"
    conn = psycopg2.connect(conn_string)
    cur = conn.cursor()

    for epc in epc_list:
        info_sendt = ''.join([log_time, ' ', epc[:24], ' ', ID_Agent])
        epc = ''.join([epc[:24], ',', epc[24:]])
        #epc_log = ''.join([epc_log, COMPUTER_SN, ',', reader_sn, ',', log_time, ',', epc, '\n'])
        epc_log = ''.join([
            epc_log, log_time, ' ', epc, ' ', reader_sn, ' ', COMPUTER_SN, '\n'
        ])
        datetime = log_time
        datetime = datetime[0:4] + "-" + datetime[4:6] + "-" + datetime[
            6:8] + " " + datetime[9:11] + ":" + datetime[
                11:13] + ":" + datetime[13:15]

        mosquitto_publisher.publisher("rfid", info_sendt)
        try:
            #Execute SQL query using execute() method
            cur.execute(
                """INSERT INTO bee_control(control_date, fk_id_sensor_rfid, fk_id_bee) VALUES(%s,%s,%s)""",
                (datetime, ID_rfid, epc[:24]))
            conn.commit()
        except psycopg2.Error as error:
            print("Error: {}".format(error))
    cur.close()
    conn.close()
    # Open the currently active log file with append mode
    log_file = open(active_log_file_name, 'a')
    # Append the tags (bee ids) stored in variable epc_log into the active log file
    log_file.write(epc_log)
    # close the active log file
    log_file.close()

    # print('Log: {0}: {1}'.format(active_date, log_time))
    #print('Log: {0}'.format(log_time))
    print(epc_log)
Example #3
0
                if int(value) < 34 or int(value) > 200:

                    countLightAlert += 1
                    #FIXME: put this value as a variable in conf.ini (DONE)
                    if countLightAlert > triggerAlert:
                        #Insert alert into observation table
                        string_observation = insert_observation.insert(
                            value, topic)
                        #print string_observation

                        #Insert into alert table
                        string_observation = string_observation + " " + insert_alert.insert(
                            value)

                        #Publish alert
                        mosquitto_publisher.publisher("alert/" + topic,
                                                      string_observation)

                        countLightAlert = 0

                #print lightDirectory['value'+str(countLight - 1)]
                #FIXME: VERIFY IF IT IS CORRECT
                elif countLightAlert > 0 and lightDirectory[
                        'value' + str(countLight - 1)] != value:
                    countAlert = 0

                #avarage,
                if len(lightDirectory) == dictionary_size:
                    #Create an aux variable
                    average = 0

                    for k in range(readingTime):