Experiment 7: Introduction To Iot and Web-Based Control
Experiment 7: Introduction To Iot and Web-Based Control
IU2341051286
CE-C A1
Experiment 7
Aim
To develop a simple IoT-based device control system using Flask as a web framework and ngrok for
exposing the local server to the internet, allowing remote control of an LED-like device.
Objectives
Tools Used
Theory
The Internet of Things (IoT) enables physical devices, such as sensors and actuators, to be
controlled remotely via the internet. In this experiment, we simulate an IoT device (an LED) that
can be turned ON or OFF using a Flask web server and control it remotely through ngrok.
APIs allow external systems (e.g., web apps, mobile apps, or other computers) to interact with
our IoT device.
Role of ngrok
Flask runs on a local server (127.0.0.1:5000), which is only accessible on the local machine. To
control the LED from outside the local network, we need ngrok.
ngrok provides:
Create a new file named app.py and copy-paste the following code:
@app.route("/status", methods=["GET"])
def check_status():
return jsonify(device_states)
public_url = ngrok.connect(5002).public_url
print(f"Public URL: {public_url}")
Open Command Prompt (Windows) or Terminal (Linux/Mac) and navigate to the directory where
app.py is saved:
python app.py
Output:
Step 4: Start ngrok
In a new terminal window, run:
ngrok http 5002
It will generate a public URL, e.g.:
https://5ba6-35-229-35-139.ngrok-free.app -> http://127.0.0.1:5002
Step 5: Test the API
Use your browser or Postman to access the following URLs:
Home Page
https://5ba6-35-229-35-139.ngrok-free.app
Expected Output:
<h1>IoT Device Control</h1><p>Use /control?device=LED&state=ON to control
the device.</p>
Turn LED ON
https://5ba6-35-229-35-139.ngrok-free.app/control?device=LED&state=ON
Expected JSON Response:
{"message": "LED turned ON"}
Turn LED OFF
https://5ba6-35-229-35-139.ngrok-free.app/control?device=LED&state=OFF
Expected JSON Response:
{"message": "LED turned OFF"}
Check LED Status
https://5ba6-35-229-35-139.ngrok-free.app/status
Expected JSON Response:
{"LED": "ON"} # or "OFF" depending on last action
● Flask: Used to create the web server and define API routes.
● request: Parses URL parameters for handling API requests.
● jsonify: Returns responses in JSON format.
● pyngrok: Exposes the local Flask app to the internet.
led_state = "OFF"
● The variable led_state stores the current state of the LED, initially set to "OFF".
4. API Endpoints:
Home Page:
@app.route('/')
def home():
@app.route('/control', methods=['GET'])
def control_led():
global led_state
device = request.args.get('device')
state = request.args.get('state')
if device != "LED":
led_state = state
else:
How It Works:
Hunny Mane
IU2341051286
CE-C A1
●Extracts device name (LED) and desired state (ON or OFF) from the query
parameters.
● If valid, it updates the device state and returns a confirmation message.
● If invalid, it returns an error message.
Example Usage:
http://127.0.0.1:5002/control?device=LED&state=ON
Response:
{"message": "LED turned ON"}
http://127.0.0.1:5002/control?device=LED&state=OFF
Response:
{"message": "LED turned OFF"}
Define the Status Route (/status)
@app.route('/status', methods=['GET'])
def device_status():
return jsonify(device_state) # Returns the current state of the LED
● Returns the current state of the LED in JSON format.
● Example Usage:
http://127.0.0.1:5002/status
{"LED": "ON"}
Start the Flask Server
if name == ' main ':
app.run(host='0.0.0.0', port=5002)
● Runs Flask on port 5000.
● host='0.0.0.0' allows access from any network.
Exposing Flask via ngrok
After starting Flask, we need to expose it to the internet:
Run this command in a new terminal window:
ngrok http 5002
Expected output:
Forwarding https://5ba6-35-229-35-139.ngrok-free.app ->
http://127.0.0.1:5002
Now, use the ngrok URL to access the API from anywhere.
Expected Outputs (Attach readable size of screenshots of following 4 pages then remove these all)
Running https://5ba6-35-229-35-139.ngrok-free.app/status should show:
{"LED": "OFF"}
Hunny Mane
IU2341051286
CE-C A1
Sending https://5ba6-35-229-35-139.ngrok-free.app/control?device=LED&state=ON
updates the
LED state:
{"message": "LED turned ON"}
Conclusion:
This experiment successfully demonstrates remote control of an IoT device using Flask and ngrok.
Flask provides a lightweight, easy-to-use API for controlling the LED.ngrok allows secure remote
access without configuring firewalls.This approach can be extended to control real IoT hardware
(Raspberry Pi, Arduino, ESP32, etc.).
Hunny Mane
IU2341051286
CE-C A1
Homework Assigned: Task: Extend the program by adding another virtual IoT device (e.g., a fan) and
allow users to control its state. Hint: Modify the device_state dictionary to include a "Fan" and
update the /control API to handle it.
Program Code
devices = {
"LED": "OFF",
"Fan": "OFF"
}
@app.route('/')
def home():
return "<h1>Device Controller</h1><p>Use /control?device=LED/Fan&state=ON/OFF</p>"
@app.route('/control', methods=['GET'])
def control_device():
device = request.args.get('device')
state = request.args.get('state')
@app.route('/status', methods=['GET'])
def device_status():
return jsonify(devices)
public_url = ngrok.connect(5001).public_url
print(f"Public URL: {public_url}")
Expected Output:
<h1>IoT Device Control</h1><p>Use /control?device=LED&state=ON or
/control?device=Fan&state=ON to control the device.</p>
Turn LED ON
https://223a-35-229-35-139.ngrok-free.app/control?device=LED&state=ON
Expected JSON Response:
{"message": "LED turned ON"}
Turn LED OFF
https://223a-35-229-35-139.ngrok-free.app/control?device=LED&state=OFF
Expected JSON Response:
{"message": "LED turned OFF"}
Check LED Status
https://223a-35-229-35-139.ngrok-free.app/status
Expected JSON Response:
{"LED": "ON"} # or "OFF" depending on last action
https://223a-35-229-35-139.ngrok-free.app/status
Explanation of program :
Flask: Used to create the web server and define API routes.
request: Handles incoming HTTP GET requests and extracts parameters.
● A dictionary devices is created to store the current state (ON/OFF) of each device.
Home Page
@app.route('/')
def home():
return "<h1>Device Controller</h1><p>Use
/control?device=LED/Fan&state=ON/OFF</p>"
● Returns a simple HTML page with instructions on how to use the API.
4. Controlling Devices
@app.route('/control', methods=['GET'])
def control_device():
device = request.args.get('device') # Get the device name from the URL
state = request.args.get('state') # Get the desired state (ON/OFF)
Hunny Mane
IU2341051286
CE-C A1
● The Flask app runs on port 5001, listening for incoming requests.
Outputs :
Running Check :
Conclusion :
This project successfully demonstrates how to build a simple yet effective IoT control system using
Flask and ngrok. By exposing the Flask server through ngrok, users can control their devices remotely
via HTTP requests. The system is highly adaptable and can be expanded to include more devices or
integrate with IoT platforms. It serves as a foundation for building smart home automation or other
IoT-based applications.