Ejemplo n.º 1
0
            "Program Exit. \n")
        exit(-1)


while True:
    try:
        setup()
        #Read value from DHT22
        humi, temp = DHT22_data()
        #test below lines
        humi = '%.2f' % humi
        temp = '%.2f' % temp
        print(humi, temp)

        #Read value from LDR
        LDRvalue = adc.analogRead(0)  # read the ADC value of channel 0
        LDRvoltage = LDRvalue / 255.0 * 3.3  # calculate the voltage value
        print('LDR ADC Value : %d, LDR Voltage : %.2f' %
              (LDRvalue, LDRvoltage))
        #Read value from Moisture Sensor
        MSvalue = adc.analogRead(1)  # read the ADC value of channel 1
        MSvoltage = MSvalue / 255.0 * 3.3  # calculate the voltage value
        print('ADC Value : %d, Voltage : %.2f' % (MSvalue, MSvoltage))

        # If Reading is valid
        """ if isinstance(humi, float) and isinstance(temp, float) and isinstance(temp, float) and isinstance(temp, float):
            # Formatting to two decimal places
            humi = '%.2f' % humi                       
            temp = '%.2f' % temp
            print(temp, humi)
            # Sending the data to thingspeak
    else:
        print(
            "No correct I2C address found, \n"
            "Please use command 'i2cdetect -y 1' to check the I2C address! \n"
            "Program Exit. \n")
        exit(-1)


setup()

while True:
    try:
        if (adc is not None):

            #Read value from LDR
            lightValue = adc.analogRead(0)  # read the ADC value of channel 0
            LDRvoltage = lightValue / 255.0 * 3.3  # calculate the voltage value
            LDRvoltage = '%.2f' % LDRvoltage

            #Read value from Moisture Sensor
            moistureValue = adc.analogRead(
                1)  # read the ADC value of channel 1
            MSvoltage = moistureValue / 255.0 * 3.3  # calculate the voltage value
            MSvoltage = '%.2f' % MSvoltage

            print("Moisture value:", moistureValue, "LDR value:", lightValue)

        else:
            print('Error')
        # DHT22 requires 2 seconds to give a reading, so make sure to add delay of above 2 seconds.
        sleep(0.1)