Project Summary
Arduino Code Explanation
# Explanation
The ESP8266 is set up as a web server with WiFi capability. The main control page (handleRoot) has buttons that allow the user to change and reset the fan and light settings. The code regulates the fan through pulsePin on pin D1 and the light through resetPin on pin D2. This code creates a web server on an ESP8266 to control a fan and light through GPIO pins D1
and D2
Simulate level adjustments by pulsing these pins. Here’s a brief breakdown:
# Key components:
- WiFi Setup: The ESP8266 connects to WiFi with a predefined
ssid
andpassword
. - Pin Configuration: GPIO
D1
controls the fan, andD2
controls the light. - Server Routes:
/
: Displays an HTML control page with buttons for setting fan and light levels./fan_l1
,/fan_l2
,/fan_l3
: Set fan levels by pulsingD1
./led_l1
,/led_l2
: Set light levels by pulsingD2
./fan_reset
,/light_reset
: Resets fan/light by pulsing the pin longer.
- Pulse Logic: Functions like
pulsePin
andpulsePinMultiple
handle pin pulsing to simulate different levels by changing duty cycles. - Main Loop: Continuously listens for incoming web requests to execute actions accordingly.
Python Code Explanation
" Python Script to Control Fan and Light via ESP8266 Web Server "
This Python script enables a user to make HTTP requests to turn a fan and light connected to an ESP8266 microcontroller. It includes functions to select the fan to three different modes; turn off the fan and light and check the response from the ESP8266 server.
# Explanation
# Key components:
- Base URL: The script starts by defining the
base_url
, which should point to the ESP8266's IP address. - Control Functions:
fan_l1()
,fan_l2()
, andfan_l3()
: Functions to set the fan to levels 1, 2, and 3, respectively.fan_reset()
: Function to reset the fan.light_l1()
,light_l2()
: Functions to set the light to levels 1 and 2, respectively.light_reset()
: Function to reset the light.
- HTTP Requests: Each function sends a GET request to the respective URL, then prints the response from the ESP8266, providing feedback on the action taken.
</> Python Code :
import requests
# Base URL for your ESP8266 server
base_url = "http://192.168.1.5" # Replace with the actual IP address of your ESP8266
# Define the URLs for the fan and light controls
fan_l1_url = f"{base_url}/fan_l1"
fan_l2_url = f"{base_url}/fan_l2"
fan_l3_url = f"{base_url}/fan_l3"
fan_reset_url = f"{base_url}/fan_reset"
light_l1_url = f"{base_url}/led_l1"
light_l2_url = f"{base_url}/led_l2"
light_reset_url = f"{base_url}/light_reset"
def fan_l1():
response = requests.get(fan_l1_url)
print("Fan Level 1 Response:", response.text)
def fan_l2():
response = requests.get(fan_l2_url)
print("Fan Level 2 Response:", response.text)
def fan_l3():
response = requests.get(fan_l3_url)
print("Fan Level 3 Response:", response.text)
def fan_reset():
response = requests.get(fan_reset_url)
print("Fan Reset Response:", response.text)
def light_l1():
response = requests.get(light_l1_url)
print("Light Level 1 Response:", response.text)
def light_l2():
response = requests.get(light_l2_url)
print("Light Level 2 Response:", response.text)
def light_reset():
response = requests.get(light_reset_url)
print("Light Reset Response:", response.text)