0
0
Raspberry Piprogramming~5 mins

Why web servers enable remote IoT control in Raspberry Pi

Choose your learning style9 modes available
Introduction

Web servers let you control devices like lights or sensors from anywhere using the internet. This makes managing your gadgets easy and flexible.

You want to turn on your home lights while away from home.
You need to check sensor data from your garden remotely.
You want to control a robot or device in another room or city.
You want to automate tasks based on commands sent over the internet.
You want to share device control with family or friends safely.
Syntax
Raspberry Pi
from http.server import BaseHTTPRequestHandler, HTTPServer

class SimpleHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/turn_on':
            # Code to turn on device
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'Device turned on')
        else:
            self.send_response(404)
            self.end_headers()

server = HTTPServer(('0.0.0.0', 8080), SimpleHandler)
server.serve_forever()

This example shows a simple web server on Raspberry Pi that listens for commands.

When you visit /turn_on in a browser, it runs code to control a device.

Examples
This server responds with device status when you visit /status.
Raspberry Pi
from http.server import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/status':
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'Device is off')

server = HTTPServer(('0.0.0.0', 8000), Handler)
server.serve_forever()
This example toggles a device when /toggle is accessed.
Raspberry Pi
from http.server import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/toggle':
            # Toggle device state here
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'Device toggled')

server = HTTPServer(('0.0.0.0', 8080), Handler)
server.serve_forever()
Sample Program

This program runs a web server on Raspberry Pi that lets you turn a device on or off and check its status by visiting URLs.

Try visiting http://your-pi-ip:8080/turn_on to turn it on, /turn_off to turn it off, and /status to see the current state.

Raspberry Pi
from http.server import BaseHTTPRequestHandler, HTTPServer

class IoTHandler(BaseHTTPRequestHandler):
    device_on = False

    def do_GET(self):
        if self.path == '/turn_on':
            IoTHandler.device_on = True
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'Device turned on')
        elif self.path == '/turn_off':
            IoTHandler.device_on = False
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'Device turned off')
        elif self.path == '/status':
            self.send_response(200)
            self.end_headers()
            status = 'on' if IoTHandler.device_on else 'off'
            self.wfile.write(f'Device is {status}'.encode())
        else:
            self.send_response(404)
            self.end_headers()

server = HTTPServer(('0.0.0.0', 8080), IoTHandler)
print('Starting server on port 8080...')
server.serve_forever()
OutputSuccess
Important Notes

Make sure your Raspberry Pi is connected to the internet and the port (8080) is open.

Use simple URLs to send commands from any device with a browser.

For real devices, replace comments with actual control code.

Summary

Web servers let you control IoT devices remotely using simple web addresses.

This makes managing devices easy from anywhere with internet access.

Raspberry Pi can run these servers to connect your devices to the web.