def temp_command():
	gpio = GPIO(debug=False)
	analogpin = 14

	gpio.pinMode(analogpin, gpio.ANALOG_INPUT)

	print 'Analog reading from pin %d now...' % analogpin
	try:
    		count = 0
    		while(count < 5):
        		# Read the voltage on pin 14
        		value = gpio.analogRead(analogpin)

    			tempc = (5.0 * value * 100.0)
    			tempc = tempc / 1024
    			value = tempc

        		print 'Leitura: %.2f  celsius'   % value
        		time.sleep(0.5)
        		count = count + 1

	# When you get tired of seeing the led blinking kill the loop with Ctrl-C.
	except KeyboardInterrupt:
    	
    		print '\nCleaning up...'
    	# Do 
    		gpio.cleanup()       
Example #2
0
def main(argv):
   print 'Starting up'
   gpio = GPIO(debug=False)
   gpio.pinMode(19,gpio.ANALOG_INPUT)
   for i in range(30):
      x = gpio.analogRead(19)
      print x
      time.sleep(0.5)
   print 'Done\n'
   gpio.cleanup()
Example #3
0
def main(argv):
   print 'Starting up'
   gpio = GPIO(debug=False)
   gpio.pinMode(8,gpio.OUTPUT)
   for i in range(4):
      print 'Hi\n'
      gpio.digitalWrite(8,gpio.HIGH)
      time.sleep(0.5)
      print 'Lo\n'
      gpio.digitalWrite(8,gpio.LOW)
      time.sleep(0.5)
   print 'Done\n'
   gpio.cleanup()
Example #4
0
def main(argv):
   print 'Starting up'
   gpio = GPIO(debug=False)
   for i in range(5):
      gpio.pinMode(8+i,gpio.OUTPUT)
   gpio.pinMode(19,gpio.ANALOG_INPUT)
   for i in range(4100):
      x = gpio.analogRead(19)
      print x
      n = (x-460)/30
      for j in range(5):
         if j<n: 
           s=gpio.HIGH 
         else: 
           s=gpio.LOW
         gpio.digitalWrite(8+j,s)
      time.sleep(0.1)
   print 'Done\n'
   gpio.cleanup()
Example #5
0
print 'Setting up pins %d and %d...' % (pin, button)

# Set pin 13 to be used as an output GPIO pin.
gpio.pinMode(pin, gpio.OUTPUT)

# Set pin 2 to be used as an input GPIO pin.
gpio.pinMode(button, gpio.INPUT)

print 'Reading from pin %d now...' % button
try:
    while(True):
        # Read the state of the button
        state = gpio.digitalRead(button)

        # If the button is pressed turn ON pin 13
        if state == 1:
            gpio.digitalWrite(pin, gpio.HIGH)

        # If the button is not pressed turn OFF pin 13
        else:
            gpio.digitalWrite(pin, gpio.LOW)

# Kill the loop with Ctrl-C.
except KeyboardInterrupt:
    # Leave the led turned off.
    print '\nCleaning up...'
    gpio.digitalWrite(pin, gpio.LOW)

    # Do a general cleanup. Calling this function is not mandatory.
    gpio.cleanup()
class AutomaticWateringSystem(object):
    """
    Main class for the Automatic Watering System (AWS)
    Will initiate the classes needed for a plant
    """

    instances = {}

    def __init__(self, uuid, name, temperature_pin=None, magnetic_valve_pin=None, moisture_pin=None,
                 debug=False, gpio_debug=False):
        """
        Constructor

        :type uuid: str
        :param uuid: The ID from the database

        :type name: str
        :param name: The name of the plant

        :type temperature_pin: int
        :param temperature_pin: Which pin the temperature sensor is attached to

        :type magnetic_valve_pin: int
        :param magnetic_valve_pin: Which pin the magnetic valve is attached to

        :type moisture_pin: int
        :param moisture_pin: Which pin the moisture sensor is attached to

        :type debug: bool
        :param debug:

        :type gpio_debug: bool
        :param gpio_debug: Specify if the debug should be on/off for the GPIO library
        """
        print_debug(debug, currentframe().f_code.co_name, u'Init %s' % name, __name__)
        all_params_str = u'UUID: {uuid}. Name: {name}. TP: {tp}. MVP: {mvp}. MP: {mp}'.format(
            uuid=uuid,
            name=name,
            tp=temperature_pin,
            mvp=magnetic_valve_pin,
            mp=moisture_pin
        )
        print_debug(debug, currentframe().f_code.co_name, all_params_str, __name__)

        # Add the new instance to the instances-list
        AutomaticWateringSystem.instances[uuid] = self

        self.__name = name
        self.__temperature_pin = temperature_pin
        self.__magnetic_valve_pin = magnetic_valve_pin
        self.__moisture_pin = moisture_pin
        self.debug = debug

        # Get a uuid for the class so we can identify easier
        # self.__uuid = str(uuid4())  # type: uuid4
        self.__uuid = uuid

        self.DB = Database.Database()
        self.db_own_plant = self.DB.get_own_plant_from_id(self.get_uuid)
        self.db_plant = self.DB.get_plant_from_id(self.db_own_plant.get('plant_id'))
        self.db_pump = self.DB.get_pump_from_id(self.db_own_plant.get('pump_id'))

        # Get a new instance of the GPIO
        self.gpio = GPIO(debug=gpio_debug)  # type: GPIO

        # It has to be a magnetic valve connected!
        self.MagneticValve = None
        if magnetic_valve_pin is not None:
            print_debug(debug, currentframe().f_code.co_name,
                        'Initializing magnetic valve at pin %i' % magnetic_valve_pin, __name__)
            self.MagneticValve = MagneticValve(self.gpio, self.__uuid, magnetic_valve_pin,
                                               name, self.db_own_plant.get('pump_id'), debug)  # type: MagneticValve
        else:
            error = EnvironmentError
            error.message = 'A magnetic valve is needed!'
            raise error

        self.TemperatureSensor = None
        if temperature_pin is not None:
            print_debug(debug, currentframe().f_code.co_name,
                        'Initializing temperature sensor at pin %i' % temperature_pin, __name__)
            self.TemperatureSensor = Temperature(self.gpio, self.__uuid, temperature_pin,
                                                 name, 30, debug)  # type: Temperature

        self.MoistureSensor = None
        if moisture_pin is not None:
            print_debug(debug, currentframe().f_code.co_name,
                        'Initializing moisture sensor at pin %i' % moisture_pin, __name__)
            self.MoistureSensor = Moisture(self.gpio, self.__uuid, moisture_pin, name,
                                           self.db_plant.get('min_moisture'), self.db_plant.get('max_moisture'),
                                           debug)  # type: Moisture

        self.__run_count = 0
        self.active = True

        # Run a check directly when a new instance is created
        self.__start_new_timer(0)

    def __start_new_timer(self, time=60):
        """
        Starts a new timer

        :type time: int
        :param time: How many seconds the timer should be set to
        """
        new_timer_uuid = str(uuid.uuid4())

        print_debug(self.debug, currentframe().f_code.co_name,
                    u'New timer will start after {time} second(s). AWS name: {name}. Timer UUID: {uuid}'
                    .format(time=time, name=self.get_name, uuid=new_timer_uuid), __name__)

        self.timer = Timer(time, self.__run, [new_timer_uuid])
        self.timer.start()

    def __run(self, timer_id):
        """
        Run the different checks, and open the valve if needed!
        This function should not be called directly! Call __start_new_timer() instead!

        :type timer_id: str
        :param timer_id: A unique identifier for each timer
        """
        self.__run_count += 1
        print_debug(self.debug, currentframe().f_code.co_name,
                    u'Timer executing. AWS name: {name}. Timer UUID: {timer_id}'
                    .format(name=self.get_name, timer_id=timer_id), __name__)
        print_debug(self.debug, currentframe().f_code.co_name, u'Run count: %s' % self.__run_count, __name__)

        temp_exceed = False
        moist_deceed = False

        stats_dict = {Database.TABLE_STATISTICS_RUN_COUNT: self.__run_count}

        if self.TemperatureSensor:
            stats_dict[Database.TABLE_STATISTICS_TEMPERATURE_RAW] = self.TemperatureSensor.get_raw_read()
            stats_dict[Database.TABLE_STATISTICS_TEMPERATURE_MIN_CELSIUS] = self.TemperatureSensor.min_celsius
            stats_dict[Database.TABLE_STATISTICS_TEMPERATURE_MAX_CELSIUS] = self.TemperatureSensor.max_celsius
            stats_dict[Database.TABLE_STATISTICS_TEMPERATURE_CELSIUS] = self.TemperatureSensor.get_celsius()
            temp_exceed = self.TemperatureSensor.has_exceeded_threshold()

        if self.MoistureSensor:
            stats_dict[Database.TABLE_STATISTICS_MOISTURE_RAW] = self.MoistureSensor.get_raw_read()
            stats_dict[Database.TABLE_STATISTICS_MOISTURE_MIN_PERCENT] = self.MoistureSensor.min_percent
            stats_dict[Database.TABLE_STATISTICS_MOISTURE_MAX_PERCENT] = self.MoistureSensor.max_percent
            stats_dict[Database.TABLE_STATISTICS_MOISTURE_PERCENT] = self.MoistureSensor.get_percent_read()
            moist_deceed = self.MoistureSensor.has_deceeded_threshold()

        print_debug(self.debug, currentframe().f_code.co_name, u'Temp exceed: %s' % str(temp_exceed), __name__)
        print_debug(self.debug, currentframe().f_code.co_name, u'Moist deceed: %s' % str(moist_deceed), __name__)

        # Add statistics
        self.DB.add_statistic(self.get_uuid, **stats_dict)

        if moist_deceed:
            # Try to open the valve
            self.MagneticValve.send_open_valve_signal()

        print_debug(self.debug, currentframe().f_code.co_name,
                    u'Timer done. AWS name: {name}. Timer UUID: {timer_id}'
                    .format(name=self.get_name, timer_id=timer_id), __name__)

        self.__start_new_timer(10)
        # self.__start_new_timer()

    def cleanup(self):
        """
        Cleanup all the things!
        """
        print_debug(self.debug, currentframe().f_code.co_name,
                    u'Cleanup  ({name}, {uuid})!'.format(name=self.get_name, uuid=self.get_uuid), __name__)

        if self.TemperatureSensor:
            self.TemperatureSensor.cleanup()

        if self.MoistureSensor:
            self.MoistureSensor.cleanup()

        self.MagneticValve.cleanup()

        self.timer.cancel()

        self.gpio.cleanup()

    @property
    def get_name(self):
        return self.__name

    @property
    def get_uuid(self):
        return self.__uuid
Example #7
0
import time
import subprocess

from wiringx86 import GPIOGalileoGen2 as GPIO

gpio = GPIO(debug=False)

pwm_pin = 11
value = 0
fade = 5

print("Setting up Pins...")
gpio.pinMode(pwm_pin, gpio.OUTPUT)

try:
    while (True):

        for i in xrange(0, 1000):
            gpio.digitalWrite(pwm_pin, gpio.HIGH)
            time.sleep(0.001)
            gpio.digitalWrite(pwm_pin, gpio.LOW)
            time.sleep(0.001)

        time.sleep(2)

except KeyboardInterrupt:
    print("Cleaning up...")
    gpio.digitalWrite(pwm_pin, gpio.LOW)
    gpio.cleanup()