예제 #1
0
 def __init__(self):
     # photoresistor connected to adc #0
     self.photo_ch = 2
     GPIO.setwarnings(False)
     GPIO.cleanup()  # clean up at the end of your script
     GPIO.setmode(GPIO.BCM)  # to specify whilch pin numbering system
     # set up the SPI interface pins
     GPIO.setup(WaterLevel.SPIMOSI, GPIO.OUT)
     GPIO.setup(WaterLevel.SPIMISO, GPIO.IN)
     GPIO.setup(WaterLevel.SPICLK, GPIO.OUT)
     GPIO.setup(WaterLevel.SPICS, GPIO.OUT)
     self.mqtt_client = Publisher(self.TOPIC + os.environ['DEVICE_NAME'],
                                  'waterlevel')
     self.collector()
예제 #2
0
    def __init__(self):
        # Setup the GPIO board
        GPIO.setmode(GPIO.BCM)

        # Setup the switch pin
        # SWITCH = 20
        # GPIO.setup(SWITCH, GPIO.OUT)
        self.mqtt_client = Publisher(self.TOPIC + os.environ['DEVICE_NAME'],
                                     'moisture')
        self.mcp = Adafruit_MCP3008.MCP3008(clk=Moisture.CLK,
                                            cs=Moisture.CS,
                                            miso=Moisture.MISO,
                                            mosi=Moisture.MOSI)
        self.collector()
예제 #3
0
class Moisture():
    # Software SPI configuration:
    CLK = 11
    MISO = 9
    MOSI = 10
    CS = 8
    DELAY_INTERVAL = 2
    TOPIC = 'sensor/moisture/'

    def __init__(self):
        # Setup the GPIO board
        GPIO.setmode(GPIO.BCM)

        # Setup the switch pin
        # SWITCH = 20
        # GPIO.setup(SWITCH, GPIO.OUT)
        self.mqtt_client = Publisher(self.TOPIC + os.environ['DEVICE_NAME'],
                                     'moisture')
        self.mcp = Adafruit_MCP3008.MCP3008(clk=Moisture.CLK,
                                            cs=Moisture.CS,
                                            miso=Moisture.MISO,
                                            mosi=Moisture.MOSI)
        self.collector()

    def read(self):
        #        GPIO.output(SWITCH, GPIO.HIGH)
        time.sleep(0.1)
        value = float(self.mcp.read_adc(1))
        #        print("The soil moisture reading is currently at {:.2f}%").format(value / 1023 * 100)
        #        GPIO.output(SWITCH, GPIO.LOW)
        return value

    def collector(self):
        print('Start collecting moisture data')
        try:

            while True:
                value = self.read()
                self.mqtt_client.publish_sensor_data("moisture", value)
                sleep(Moisture.DELAY_INTERVAL)
        except:
            GPIO.cleanup()
예제 #4
0
class DHT:
    DELAY_INTERVAL = 2
    TOPIC = 'sensor/dht/'

    def __init__(self):
        self.mqtt_client = Publisher(self.TOPIC + os.environ['DEVICE_NAME'],
                                     'dht')
        self.collector()

    def read(self):
        h, t = dht.read_retry(dht.DHT22, 4)
        if h is not None and t is not None:
            return '{0:0.1f}*C'.format(t), '{0:0.1f}%'.format(h)
        else:
            print('failed to retrieve the data')

    def collector(self):
        print('start collecting air sensor data')
        while True:
            temp_value, hum_value = self.read()
            self.mqtt_client.publish_sensor_data("dht_temp", temp_value)
            self.mqtt_client.publish_sensor_data("dht_hum", hum_value)
            sleep(DHT.DELAY_INTERVAL)
예제 #5
0
class LDR:
    # change these as desired - they're the pins connected from the
    # SPI port on the ADC to the Cobbler
    SPICLK = 11
    SPIMISO = 9
    SPIMOSI = 10
    SPICS = 8
    DELAY_INTERVAL = 2
    TOPIC = 'sensor/light/'

    def __init__(self):

        # photoresistor connected to adc #0
        self.photo_ch = 0

        GPIO.setwarnings(False)
        GPIO.cleanup()  # clean up at the end of your script
        GPIO.setmode(GPIO.BCM)  # to specify whilch pin numbering system
        # set up the SPI interface pins
        GPIO.setup(LDR.SPIMOSI, GPIO.OUT)
        GPIO.setup(LDR.SPIMISO, GPIO.IN)
        GPIO.setup(LDR.SPICLK, GPIO.OUT)
        GPIO.setup(LDR.SPICS, GPIO.OUT)

        self.publisher = Publisher(self.TOPIC + os.environ['DEVICE_NAME'], 'ldr')
        self.collector()

    # read SPI data from MCP3008(or MCP3204) chip,8 possible adc's (0 thru 7)
    def readadc(self, adcnum, clockpin, mosipin, misopin, cspin):
        if (adcnum > 7) or (adcnum < 0):
            return -1
        GPIO.output(cspin, True)

        GPIO.output(clockpin, False)  # start clock low
        GPIO.output(cspin, False)  # bring CS low

        commandout = adcnum
        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3  # we only need to send 5 bits here
        for i in range(5):
            if commandout & 0x80:
                GPIO.output(mosipin, True)
            else:
                GPIO.output(mosipin, False)
            commandout <<= 1
            GPIO.output(clockpin, True)
            GPIO.output(clockpin, False)

        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
            GPIO.output(clockpin, True)
            GPIO.output(clockpin, False)
            adcout <<= 1
            if (GPIO.input(misopin)):
                adcout |= 0x1

        GPIO.output(cspin, True)

        adcout >>= 1  # first bit is 'null' so drop it
        return adcout

    def collector(self):
        print('start collecting data')
        while True:
            adc_value = self.readadc(self.photo_ch, LDR.SPICLK, LDR.SPIMOSI, LDR.SPIMISO, LDR.SPICS)
            self.publisher.publish_sensor_data("ldr", adc_value)
            sleep(LDR.DELAY_INTERVAL)
예제 #6
0
 def __init__(self):
     self.mqtt_client = Publisher(self.TOPIC + os.environ['DEVICE_NAME'],
                                  'dht')
     self.collector()