class lm35(object): """ Instatiates a analog read of the GPIO pin from galileo gen2 pin: Valid Analog Pin (14, 15, 16, 17, 18, 19) respectively from A0 to A5 unit: Can be either Celcius, Kelvin or Farenheit [c, k f] """ # Class Initializer def __init__(self, pin, unit="c", debug=False): self.gpio = GPIO(debug=False) self.unit = unit self.pin = pin self.gpio.pinMode(self.pin, self.gpio.ANALOG_INPUT) def get_value(self): self.raw = self.gpio.analogRead(self.pin) self.milivolt = (self.raw / 1024.0) * 5000 self.celcius = self.milivolt / 10 self.farenheit = (self.celcius * 1.8) + 32 self.kelvin = self.celcius + 273.0 if self.unit == "c": return self.celcius if self.unit == "k": return self.kelvin if self.unit == "f": return self.farenheit
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()
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()
class photo_resistor(object): """ Instatiates a analog read of the GPIO pin from galileo gen2 pin: Valid Analog Pin (14, 15, 16, 17, 18, 19) respectively from A0 to A5 """ # Class Initializer def __init__(self, pin, debug=False): self.gpio = GPIO(debug=False) self.pin = pin self.gpio.pinMode(self.pin, self.gpio.ANALOG_INPUT) def get_value(self): self.raw = self.gpio.analogRead(self.pin) self.normalized = (self.raw / 1024.0) self.percent = self.normalized * 100 return self.percent
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()
pin = 13 analogpin = 14 print 'Setting up all pins...' # Set pin 14 to be used as an analog input GPIO pin. gpio.pinMode(analogpin, gpio.ANALOG_INPUT) # Set pin 13 to be used as an output GPIO pin. gpio.pinMode(pin, gpio.OUTPUT) print 'Analog reading from pin %d now...' % analogpin try: while(True): # Read the voltage on pin 14 value = gpio.analogRead(analogpin) # Turn ON pin 13 gpio.digitalWrite(pin, gpio.HIGH) # Sleep for a while depending on the voltage we just read. The higher # the voltage the more we sleep. time.sleep(value / 1023.0) # Turn OFF pin 13 gpio.digitalWrite(pin, gpio.LOW) # Sleep for a while depending on the voltage we just read. The higher # the voltage the more we sleep. time.sleep(value / 1023.0)
time.sleep(0.2) print("Turning Alarm Pin On") gpio.digitalWrite(alarm_pin, gpio.HIGH) time.sleep(0.2) print("Turning User Pin Off") gpio.digitalWrite(user_pin, gpio.LOW) time.sleep(0.2) print("Turning Internet Pin Off") gpio.digitalWrite(internet_pin, gpio.LOW) time.sleep(0.2) print("Turning Mote Pin Off") gpio.digitalWrite(mote_pin, gpio.LOW) time.sleep(0.2) print("Turning Alarm Pin Off") gpio.digitalWrite(alarm_pin, gpio.LOW) time.sleep(0.2) print("Playing a little tune") for i in xrange(1000): gpio.digitalWrite(speaker_pin, gpio.HIGH) time.sleep(0.001) gpio.digitalWrite(speaker_pin, gpio.LOW) time.sleep(0.001) print("There is a User in the room: ", bool(gpio.digitalRead(presence_pin))) print("The Current Temperature is: ", (gpio.analogRead(lm35_pin) / 1024.0) * 500) print("The Current Luminosity is: ", 100 - (gpio.analogRead(photo_pin) * 100 / 1024.0))
lm35_pin = 16 photo_pin = 14 summ = 0 sample = 0 print("Setting Up all pins...") gpio.pinMode(lm35_pin, gpio.ANALOG_INPUT) gpio.pinMode(photo_pin, gpio.ANALOG_INPUT) print("Analog reading from pin A0") try: while (True): raw_value_lm35 = gpio.analogRead(lm35_pin) raw_value_photo = gpio.analogRead(photo_pin) milivolt = (raw_value_lm35 / 1024.0) * 5000 celcius = milivolt / 10 percent = 100 - (raw_value_photo / 1024.0 * 100) # summ += celcius # sample += 1 # avg = summ/sample print(celcius, percent) time.sleep(0.5) except KeyboardInterrupt: print("\nCleaning Up...") gpio.cleanup()