0% found this document useful (0 votes)
3 views10 pages

iot manual

Uploaded by

Deeksha Mekala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

iot manual

Uploaded by

Deeksha Mekala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

EXPERIMENT 3

*Aim: To send recorded values of temperature and humidity to the internet using a GSM module
with a Raspberry Pi, you'll typically follow these steps:

*Hardware Required:

1. Raspberry Pi (any model with GPIO)


2. GSM Module (like SIM800L or SIM900)
3. DHT11 or DHT22 sensor for temperature and humidity
4. Breadboard and Jumper Wires
5. Power supply for GSM module (if needed)

*Software Requirements:

1. Raspbian OS on your Raspberry Pi


2. Python (with necessary libraries)
3. GSM Module library (like pySerial for serial communication)
4. DHT sensor library (like Adafruit_DHT)

* Procedure:-

1. Connect the Components

 DHT Sensor:
o Connect the VCC pin to 3.3V or 5V on the Raspberry Pi.
o Connect the GND pin to Ground.
o Connect the DATA pin to a GPIO pin (e.g., GPIO4).
 GSM Module:
o Connect the TX pin of the GSM module to the RX pin on the Raspberry Pi
(GPIO15).
o Connect the RX pin of the GSM module to the TX pin on the Raspberry Pi
(GPIO14).
o Connect the module’s power and ground.

 Ensure your GSM module has a SIM card with SMS capabilities.
 Replace "YOUR_PHONE_NUMBER" with your actual phone number in the script.
 The script sends SMS messages containing temperature and humidity data every minute.
Adjust the interval as needed.

2. Install Required Libraries Open a terminal and run:

sudo apt-get update


sudo apt-get install python3-pip
pip3 install pyserial Adafruit_DHT

3. Write the Python Script

Create a new Python script (e.g., send_data.py):


import time
import Adafruit_DHT
import serial

# Setup
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 # GPIO pin where the sensor is connected
GSM_PORT = '/dev/ttyS0' # Update according to your connection
GSM_BAUDRATE = 9600

def send_sms(message):
with serial.Serial(GSM_PORT, GSM_BAUDRATE, timeout=1) as ser:
ser.write(b'AT\r')
time.sleep(1)
ser.write(b'AT+CMGF=1\r') # Set SMS mode
time.sleep(1)
ser.write(b'AT+CMGS="YOUR_PHONE_NUMBER"\r') # Replace with your
number
time.sleep(1)
ser.write((message + '\x1A').encode()) # Send the message
time.sleep(1)

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:


message = f'Temperature: {temperature:.1f}°C, Humidity: {humidity:.1f}
%'
print(message)
send_sms(message)
else:
print('Failed to retrieve data from humidity sensor')

time.sleep(60) # Send data every minute

4. Run the Script

Make the script executable and run it:

chmod +x send_data.py
python3 send_data.py

Troubleshooting:

 If the GSM module isn't responding, check the power supply and connections.
 Ensure the baud rate matches the GSM module's settings.

Result: This setup will allow you to monitor temperature and humidity remotely via SMS.
EXPERIMENT 5

 Aim: Setting up a voice-based home automation system for switching lights on and off
using Google Assistant, IFTTT, and MQTT is a great project! Here’s a step-by-step guide
to help you get started:
 Requirements

1. Google Assistant (on a smartphone or smart speaker)


2. IFTTT account (if you don’t have one, sign up at ifttt.com)
3. MQTT broker (like Mosquitto, hosted locally or on a cloud service)
4. MQTT client (to communicate with your lights)
5. Smart light bulbs or relay that can be controlled via MQTT

 Procedure

Step 1: Set Up Your MQTT Broker

1. Install MQTT Broker: If you're using Mosquitto, you can install it on a Raspberry Pi,
PC, or a cloud service.
2. Configure Your Broker: Ensure your broker is running and note the broker address, port,
and any authentication details.

Step 2: Connect Your Smart Lights

1. Choose Smart Lights/Relay: Ensure they support MQTT. Some examples include:
o ESP8266/ESP32 with relays
o Smart bulbs like Philips Hue (with a bridge)
2. Configure the Lights: Connect them to your MQTT broker. You'll need to set the topic
for controlling them (e.g., home/livingroom/light).

Step 3: Set Up IFTTT

1. Create an IFTTT Account: Sign in to your account.


2. Create Applets:
o For Turning On Lights:
 Trigger: Google Assistant
 Choose "Say a simple phrase" (e.g., "Turn on the living room
light").
 Action: Webhooks
 URL: http://your-mqtt-broker-ip:port/publish
 Method: POST
 Content Type: application/json
 Body:

json
Copy code
{
"topic": "home/livingroom/light",
"payload": "ON"
}
o For Turning Off Lights: Repeat the steps above but change the phrase and
payload to "OFF".

Step 4: Testing

1. Test IFTTT Triggers: Use Google Assistant to say your phrases. IFTTT should trigger
the corresponding action.
2. Check MQTT Client: Use an MQTT client (like MQTT.fx or Mosquitto client) to
monitor the messages being sent to your broker.

Step 5: Integrate Google Assistant with IFTTT

1. Voice Commands: Test the voice commands to ensure they work seamlessly.
2. Adjust IFTTT Settings: If necessary, adjust the applet settings or refine your voice
commands for clarity.

 Troubleshooting Tips

Check Network Settings,Debugging with MQTT,Google Assistant Responsiveness:

 RESULT :You now have a basic voice-controlled home automation system using
Google Assistant, IFTTT, and MQTT. You can expand this setup by adding more devices
and actions, such as dimming lights, changing colors, or integrating other smart home
devices.

EXPERIMENT 6
 Aim :Interfacing a Raspberry Pi with the cloud using REST API and MQTT involves
several steps. Below, I'll guide you through setting up a simple example using both
methods.
 Hardware And Software Requirements:

1. Raspberry Pi: Make sure it's set up with Raspbian or another suitable OS.
2. Internet Connection: Ensure your Pi is connected to the internet.
3. MQTT Broker: You can use a public broker like Eclipse Mosquitto or set up your own.
4. Python: Ensure Python is installed on your Pi. You can check this by running python3 --
version.

 PROCEDURE AND STEPS

PROGRAM 1: Setting Up a REST API

1. Install Required Libraries:

sudo apt-get update


sudo apt-get install python3-requests

2. Create a Python Script for REST API:


o This script will send sensor data to a REST API endpoint.
o Create a file named rest_api.py.

import requests
import random
import time

# Example API endpoint


url = 'https://your-api-endpoint.com/data' # Replace with your API
endpoint

while True:
# Simulated sensor data
sensor_data = {
'temperature': random.uniform(20.0, 30.0), # Simulated
temperature
'humidity': random.uniform(30.0, 50.0) # Simulated humidity
}

# Send data to the REST API


response = requests.post(url, json=sensor_data)

# Print response
print(f'Status Code: {response.status_code}, Response:
{response.text}')

# Wait for 10 seconds


time.sleep(10)

3. Run the Script:

python3 rest_api.py
PROGRAM 2: Setting Up MQTT

1. Install Required Libraries:

sudo apt-get install python3-paho-mqtt

2. Create a Python Script for MQTT:


o This script will publish sensor data to an MQTT broker.
o Create a file named mqtt_client.py.

import paho.mqtt.client as mqtt


import random
import time

# MQTT settings
broker = 'broker.hivemq.com' # Public broker
port = 1883 # Default MQTT port
topic = 'sensor/data' # Topic to publish data

# Callback function when the client connects


def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))

client = mqtt.Client()
client.on_connect = on_connect
client.connect(broker, port, 60)

client.loop_start() # Start the loop

while True:
# Simulated sensor data
sensor_data = {
'temperature': random.uniform(20.0, 30.0),
'humidity': random.uniform(30.0, 50.0)
}

# Publish sensor data


client.publish(topic, str(sensor_data))
print(f'Published: {sensor_data}')

# Wait for 10 seconds


time.sleep(10)

client.loop_stop() # Stop the loop

3. Run the Script: python3 mqtt_client.py

RESUILT:

You now have two scripts running on your Raspberry Pi:

 REST API Script: Sends simulated temperature and humidity data to a REST API
endpoint every 10 seconds.
 MQTT Client Script: Publishes the same simulated data to an MQTT broker every 10
seconds.
EXPERIMENT 5

 Aim: To remotely control lights using a Raspberry Pi, you can use a relay module
connected to the Raspberry Pi GPIO pins. Here's a basic guide to help you set up a simple
project to switch lights on and off remotely.
 Components Requirement :

1. Raspberry Pi (any model with GPIO)


2. Relay module (1-channel or multi-channel, depending on how many lights you want to
control)
3. Jumper wires
4. Light (e.g., a lamp with a plug)
5. Power source for the light
6. A web server software (like Flask)

 PROCEDURE:

1. Connect the Relay to the Raspberry Pi:


o Connect the VCC pin of the relay module to the 5V pin on the Raspberry Pi.
o Connect the GND pin of the relay module to a GND pin on the Raspberry Pi.
o Connect the IN pin of the relay module to a GPIO pin (e.g., GPIO17).
2. Connect the Light:
o Cut the live wire of the light’s power cord and connect one end to the COM
terminal of the relay.
o Connect the other end to the NO (Normally Open) terminal. This way, when the
relay is activated, the circuit will close and power the light.

 Setting Up the Raspberry Pi

1. Install Flask: Open a terminal on your Raspberry Pi and install Flask:

sudo apt update


sudo apt install python3-flask

2. Create a Python Script: Create a new Python script (e.g., light_control.py) and open
it in your favorite editor:

nano light_control.py

Here’s a simple example of the script:

from flask import Flask, render_template, redirect, url_for


import RPi.GPIO as GPIO

app = Flask(__name__)

# Set up GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

@app.route('/')
def index():
return render_template('index.html')
@app.route('/on')
def turn_on():
GPIO.output(17, GPIO.HIGH)
return redirect(url_for('index'))

@app.route('/off')
def turn_off():
GPIO.output(17, GPIO.LOW)
return redirect(url_for('index'))

if __name__ == '__main__':
try:
app.run(host='0.0.0.0', port=5000)
except KeyboardInterrupt:
GPIO.cleanup()

3. Create a Simple HTML Template: Create a folder named templates in the same
directory as your Python script, then create a file named index.html inside it:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Light Control</title>
</head>
<body>
<h1>Light Control</h1>
<a href="/on">Turn On</a>
<a href="/off">Turn Off</a>
</body>
</html>

 Running the Project

1. Run the Python Script: In your terminal, run:

python3 light_control.py

2. Access the Web Interface: Open a web browser on any device connected to the same
network and enter the Raspberry Pi's IP address followed by :5000. For example:

arduino
Copy code
http://192.168.1.100:5000

3. Control the Light: You should see links to turn the light on and off. Clicking these links
will activate the relay, switching the light accordingly.

EXPERIMENT 5

 Aim: Interfacing a DHT11 sensor with a Raspberry Pi and uploading temperature and
humidity data to the cloud is a great project. Here’s a step-by-step guide to help you set
this up using Python and a cloud service like ThingSpeak.
 Components Required:

1. Raspberry Pi (any model)


2. DHT11 temperature and humidity sensor
3. 10kΩ resistor (for pull-up on the data line)
4. Breadboard and jumper wires
5. Internet connection for the Raspberry Pi

 PROCEDURE

1. Connect the DHT11 Sensor:


o VCC to the 5V pin on the Raspberry Pi.
o GND to a GND pin on the Raspberry Pi.
o DATA to a GPIO pin (e.g., GPIO17).
o Place a 10kΩ resistor between the VCC and DATA pin (this acts as a pull-up
resistor).

Setting Up the Raspberry Pi

1. Update the System: Open a terminal and update your Raspberry Pi:

sudo apt update


sudo apt upgrade

2. Install Required Libraries: Install the Adafruit DHT library and the requests library for
HTTP requests:

sudo apt install python3-pip


pip3 install Adafruit-DHT requests

3. Create a Python Script: Create a new Python script (e.g., dht11_to_cloud.py):

nano dht11_to_cloud.py

Here’s an example script to read the DHT11 data and send it to ThingSpeak:

python
Copy code
import Adafruit_DHT
import requests
import time

# Set the GPIO pin where the DHT11 is connected


DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 17 # GPIO pin number

# ThingSpeak API details


THINGSPEAK_API_KEY = 'YOUR_API_KEY' # Replace with your ThingSpeak API
Key
THINGSPEAK_URL = 'https://api.thingspeak.com/update'

while True:
# Read the DHT11 sensor
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

# Check if the reading was successful


if humidity is not None and temperature is not None:
print(f'Temperature: {temperature}°C, Humidity: {humidity}%')

# Send data to ThingSpeak


payload = {
'api_key': THINGSPEAK_API_KEY,
'field1': temperature,
'field2': humidity
}
response = requests.post(THINGSPEAK_URL, params=payload)
print('Data sent to ThingSpeak:', response.status_code)

else:
print('Failed to retrieve data from humidity sensor')

# Wait for 60 seconds before the next read


time.sleep(60)

4. Replace the API Key: Sign up at ThingSpeak and create a channel. Obtain your Write
API Key and replace YOUR_API_KEY in the script with your actual key.

 Running the Script

1. Run the Script: In the terminal, execute:

python3 dht11_to_cloud.py

2. Verify Data in ThingSpeak: After a minute, check your ThingSpeak channel. You should
see the temperature and humidity data being uploaded.

Important Notes

 Ensure your Raspberry Pi is connected to the internet.


 You can modify the time.sleep(60) line to change the frequency of readings.
 You may want to handle exceptions more robustly in a production environment.
 You can visualize the data using the ThingSpeak dashboard.

You might also like