Last active
March 11, 2019 03:04
-
-
Save IdrisCytron/2591cf8e0181397b638d4a6c0555901b to your computer and use it in GitHub Desktop.
Displaying temperature and humidity data to Blynk app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gpiozero import LED, Button, Buzzer | |
import BlynkLib | |
import Adafruit_DHT | |
led1 = LED(17) | |
led2 = LED(18) | |
led3 = LED(27) | |
led4 = LED(22) | |
led5 = LED(25) | |
led6 = LED(12) | |
led7 = LED(13) | |
led8 = LED(19) | |
sw1 = Button(21) | |
buzzer = Buzzer(26) | |
sensor_type = Adafruit_DHT.DHT22 | |
sensor_pin = 17 | |
BLYNK_AUTH = 'YourAuthToken' | |
# Initialize Blynk | |
blynk = BlynkLib.Blynk(BLYNK_AUTH) | |
# Register Virtual Pins | |
@blynk.VIRTUAL_WRITE(1) | |
def my_write_handler(value): | |
print('Current V1 value: {}'.format(value[0])) | |
if int(format(value[0])) == 1: | |
led8.on() | |
else: | |
led8.off() | |
def sw1Pressed(): | |
buzzer.beep(0.1, 0.1, 2) | |
print('SW1 is pressed') | |
blynk.notify('SW1 is pressed') | |
def my_user_task(): | |
humidity, temperature = Adafruit_DHT.read_retry(sensor_type, sensor_pin) | |
print('Humidity = {:.2f}%\tTemperature = {:.2f}C'.format(humidity, temperature)) | |
blynk.virtual_write(2, '{:.2f}'.format(temperature)) | |
blynk.virtual_write(3, '{:.2f}'.format(humidity)) | |
blynk.set_user_task(my_user_task, 3000) | |
sw1.when_pressed = sw1Pressed | |
try: | |
while True: | |
blynk.run() | |
except KeyboardInterrupt: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment