Uploaded by Sarvesh Kapse

iot-lab-assignment

advertisement
Yash Karnik
IT-T14
63
Raspberry Pi program for interfacing with DHT11
# importing required libraries
import time
import board
import adafruit_dht
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D18)
#main program loop
while True:
# Errors happen fairly often, DHT's are hard to read, Just continue program execution if exception occurs
try:
#Obtain temperature value in celcius
temperature_c = dhtDevice.temperature
# Convert temperature to farenheit
temperature_f = temperature_c * (9 / 5) + 32
#Obtain humidity value
humidity = dhtDevice.humidity
# Print the values to the serial port and format temperature values up to 1 decimal accuracy
print(
"Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(
temperature_f, temperature_c, humidity
)
)
#Every error other that Runtime Error causes the board to hang an terminate interface with DHT
#device
except RuntimeError as error:
#Print error argument
print(error.args[0])
#Sleep for 2 seconds
time.sleep(2)
continue
#Halt interface on any other error apart from run time exception
except Exception as error:
dhtDevice.exit()
Yash Karnik
IT-T14
63
raise error
#Sleep for 2 seconds
time.sleep(2)
OUTPUT (Prints below line every 2 seconds)
Temp:<temp_value_farenheit>/<temp_value_celcius> Humidity:<humidity_value>
Eg:
Temp: 73.4 F / 23.0 C Humidity: 40.3%
Temp: 73.4 F / 23.0 C Humidity: 40.3%
Yash Karnik
IT-T14
63
Arduino program for interfacing with DHT11
// Import required libraries
#include <dht11.h>
//Set DHT11 constant as 4
#define DHT11PIN 4
//Initialize variable for DHt11 struct
dht11 DHT11;
//Initial setup loop
void setup()
Serial.begin(9600);
//Infinite program loop
void loop()
{
//Print new line
Serial.println();
//Initialize read pin to pin number 4
int chk = DHT11.read(DHT11PIN);
//Print to stdout a floating humidity value accurate up to 2 decimals
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
//Print to stdout a floating temperature value accurate up to 2 decimals
Serial.print("Temperature (C): ");
Serial.println((float)DHT11.temperature, 2);
//Sleep from 2 seconds
delay(2000);
}
Output:
Humidity <humidity_value (float accuracy=2)>
Temperature <temperature_value (float accuracy=2)>
Yash Karnik
IT-T14
63
Scroll flash LED program for Raspberry Pi
# Import Raspberry Pi GPIO library and sleep function from the time module
import RPi.GPIO as GPIO
from time import sleep
# Ignore warning
GPIO.setwarnings(False)
# Use physical pin numbering
GPIO.setmode(GPIO.BOARD)
# Set pins 6, 7, 8 to be an output pin and set initial value to low (off)
GPIO.setup(6, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)
# Run forever
while True:
GPIO.output(6, GPIO.HIGH) # Turn on
GPIO.output(7, GPIO.HIGH) # Turn on
GPIO.output(8, GPIO.HIGH) # Turn on
sleep(1) # Sleep for 1 second
GPIO.output(6, GPIO.LOW) # Turn off
sleep(1)
GPIO.output(7, GPIO.LOW) # Turn off
sleep(1)
GPIO.output(8, GPIO.LOW) # Turn off
sleep(1)
GPIO.output(8, GPIO.HIGH) # Turn on
sleep(1)
GPIO.output(7, GPIO.HIGH) # Turn on
sleep(1)
GPIO.output(8, GPIO.HIGH) # Turn on
sleep(1)
Yash Karnik
IT-T14
63
Download