How to Automate Anything and integrate with Jarvis

Project Summary

Python script that runs your ESP8266 web server for fan and light control into Jarvis AI system. This integration will enable you to perform actions by voice command or through other automated processes by these fundamentals you can automate anything.

Arduino Code Explanation

" ESP8266 Web Server to Control Fan and Light with Adjustable Levels " 

This code configures an ESP8266 to act as a web server where one can control a fan and light using a web page. Users can choose different fan and light levels, turn the settings to default, and send pulsed control signals. All the control options are available through specific web server paths.

# 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 D2Simulate level adjustments by pulsing these pins. Here’s a brief breakdown:

    # Key components:

    1. WiFi Setup: The ESP8266 connects to WiFi with a predefined ssid and password.
    2. Pin Configuration: GPIO D1 controls the fan, and D2 controls the light.
    3. 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 pulsing D1.
      • /led_l1, /led_l2: Set light levels by pulsing D2.
      • /fan_reset, /light_reset: Resets fan/light by pulsing the pin longer.
    4. Pulse Logic: Functions like pulsePin and pulsePinMultiple handle pin pulsing to simulate different levels by changing duty cycles.
    5. 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

    The script utilizes the requests library to make calls to the ESP8266 web server which has routes for fan and light control. It gives a base URL for all control actions and has functions for all the actions.

    # Key components:

    1. Base URL: The script starts by defining the base_url, which should point to the ESP8266's IP address.
    2. Control Functions:
      • fan_l1(), fan_l2(), and fan_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.
    3. 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)
































    Post a Comment

    If you have any doubts, please let me know

    Previous Post Next Post