Created
February 4, 2020 03:08
-
-
Save IdrisCytron/54ec9baf009a532b42560935bb18ec86 to your computer and use it in GitHub Desktop.
Control LED through Blynk V0 and read sensor data from Blynk V1 and V2.
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 | |
import Adafruit_DHT | |
import BlynkLib | |
from time import time | |
# Initialize Blynk | |
BLYNK_AUTH = 'YourBlynkAuthToken' | |
blynk = BlynkLib.Blynk(BLYNK_AUTH) | |
led8 = LED(19) | |
sensor_type = Adafruit_DHT.DHT22 | |
sensor_pin = 17 | |
# Register Virtual Pins | |
@blynk.VIRTUAL_WRITE(0) | |
def my_write_handler(value): | |
v0 = int(format(value[0])) | |
print('Current V0 value: {}'.format(v0)) | |
led8.value = v0 | |
def sendSensorData(): | |
humidity, temperature = Adafruit_DHT.read_retry(sensor_type, sensor_pin) | |
print('Humidity = {:.2f}%\tTemperature = {:.2f}C'.format(humidity, temperature)) | |
blynk.virtual_write(1, '{:.2f}'.format(temperature)) | |
blynk.virtual_write(2, '{:.2f}'.format(humidity)) | |
prevTime = 0 | |
interval = 2 | |
try: | |
while True: | |
blynk.run() | |
if time() - prevTime > interval: | |
sendSensorData() | |
prevTime = time() | |
except KeyboardInterrupt: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment