iot manual
iot manual
*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:
*Software Requirements:
* Procedure:-
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.
# 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)
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
Procedure
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.
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).
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.
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
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.
import requests
import random
import time
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
}
# Print response
print(f'Status Code: {response.status_code}, Response:
{response.text}')
python3 rest_api.py
PROGRAM 2: Setting Up MQTT
# MQTT settings
broker = 'broker.hivemq.com' # Public broker
port = 1883 # Default MQTT port
topic = 'sensor/data' # Topic to publish data
client = mqtt.Client()
client.on_connect = on_connect
client.connect(broker, port, 60)
while True:
# Simulated sensor data
sensor_data = {
'temperature': random.uniform(20.0, 30.0),
'humidity': random.uniform(30.0, 50.0)
}
RESUILT:
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 :
PROCEDURE:
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
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>
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:
PROCEDURE
1. Update the System: Open a terminal and update your Raspberry Pi:
2. Install Required Libraries: Install the Adafruit DHT library and the requests library for
HTTP requests:
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
while True:
# Read the DHT11 sensor
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
else:
print('Failed to retrieve data from humidity sensor')
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.
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