#1.a. To interface led/buzzer with Arduino/Raspberry pi and write a program to ‘turn on’ led #for 1 sec after every 2 seconds. import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(18,GPIO.OUT) while True: print ("LED on") GPIO.output(18,GPIO.HIGH) time.sleep(1) print ("LED off") GPIO.output(18,GPIO.LOW) time.sleep(2) #1.b. To interface push button/digital sensor (ir/ldr) with arduino/raspberry pi and write a program #to ‘turn on’ led when push button is pressed or at sensor detection import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17,GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.setup(18,GPIO.OUT) while True: state= GPIO.input(17) if state == False: print('Button pressed') GPIO.output(18,GPIO.HIGH) time.sleep(2) else: print('Button not pressed') GPIO.output(18,GPIO.LOW) time.sleep(1) #2.a import Adafruit_DHT sensor = Adafruit_DHT.DHT11 pin = 18#GPIO 18 of Rpi # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). while True: humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)) #2.b. from board import SCL, SDA import busio from oled_text import OledText import Adafruit_DHT sensor = Adafruit_DHT.DHT11 pin = 18#GPIO 18 of Rpi i2c = busio.I2C(SCL, SDA) # Create the display, pass its pixel dimensions oled = OledText(i2c, 128, 64) for i in range(5): humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)) # Write to the oled oled.text('Temp={0:0.1f}*C'.format(temperature), 1) # Line 1 oled.text('Humid={0:0.1f}%'.format(humidity), 2) # Line 2 ##3 DC Motor interface import time import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(17,GPIO.IN,GPIO.PUD_UP) GPIO.setup(18,GPIO.OUT) while True: state=GPIO.input(17) if state==False: GPIO.output(18,GPIO.HIGH) time.sleep(2) GPIO.output(18,GPIO.LOW) time.sleep(1) else: GPIO.output(18,GPIO.LOW) time.sleep(1) ##4 Bluetooth TX import time import serial ser = serial.Serial('/dev/ttyS0', baudrate = 9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1) counter=0 while True: ser.write(b'Write counter: %d \n'%(counter)) print('Printed') time.sleep(1) counter += 1 ##5 Push the Data to Thingspeak cloud import sys import urllib.request as urllib2 from time import sleep import Adafruit_DHT sensor = Adafruit_DHT.DHT11 pin = 4#GPIO 4 of Rpi myAPI = 'XXXXXX' baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI def DHT11_data(): # Reading from DHT22 and storing the temperature and humidity humi, temp = Adafruit_DHT.read_retry(sensor, pin) return humi, temp while True: try: humi, temp = DHT11_data() print(humi,temp) if isinstance(humi, float) and isinstance(temp, float): humi = '%.2f' % humi temp = '%.2f' % temp conn = urllib2.urlopen(baseURL + '&field1=%s&field2=%s' % (temp, humi)) print(conn.read()) # Closing the connection conn.close() else: print('Error') sleep(20) except: break