Imagine you have a smart lamp connected to your Raspberry Pi. Why is a web server useful to control this lamp from anywhere?
Think about how devices talk to each other over the internet.
A web server listens for requests from anywhere on the internet and sends responses. This allows you to control your IoT device remotely using web protocols.
Look at this Python code running a web server on Raspberry Pi. What will it print when accessed at the root URL?
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'IoT device is online' if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
The route '/' returns a simple string.
The Flask app defines a route '/' that returns the string 'IoT device is online'. Accessing this URL will show that text.
This code runs a web server on Raspberry Pi but cannot be accessed from other devices on the network. What is the problem?
from http.server import HTTPServer, BaseHTTPRequestHandler class SimpleHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(b'Hello from Pi') server = HTTPServer(('127.0.0.1', 8080), SimpleHandler) server.serve_forever()
Check the IP address the server listens on.
Binding to 127.0.0.1 means only local connections are accepted. To allow remote access, bind to '0.0.0.0'.
Choose the correct code snippet to start a Flask web server that listens on all network interfaces.
To allow remote access, the host must be set to all interfaces.
Setting host='0.0.0.0' makes the server listen on all interfaces, allowing remote devices to connect.
Which of the following best explains the main advantage of running a web server on a Raspberry Pi for IoT control?
Think about how web servers enable communication over the internet.
Web servers provide a standard way to send and receive data remotely, enabling easy control of IoT devices from anywhere.