-->

Monday, January 8, 2024

led two

A little test test just to see where it goes


<p>
import machine
from machine import Pin
import neopixel
import network
import uasyncio as asyncio
import usocket as socket
import ujson

n = 90
p = 16

np = neopixel.NeoPixel(machine.Pin(p), n)

def set_color(r, g, b):
    for i in range(n):
        np[i] = (r, g, b)
    np.write()

# Your LED control functions go here
def turn_on_red_led():
    set_color(100, 0, 0)
    print("Red LED turned on")

def turn_on_blue_led():
    set_color(0, 0, 100)
    print("Blue LED turned on")

def turn_on_green_led():
    set_color(0, 100, 0)
    print("Green LED turned on")
    
def turn_on_sky_led():
    set_color(0, 100, 100)
    print("sky LED turned on")
    
def turn_on_clear_led():
  for i in range(n):
    np[i] = (0, 0, 0)
    np.write()
    

# Request handler for handling incoming HTTP requests
# Request handler for handling incoming HTTP requests
async def handle_request(reader, writer):
    try:
        request = await reader.read(4096)
        request_str = request.decode("utf-8")
        print("Received request:", request_str)

        # Extract the color from the request data
        color = request_str.split('_button=on')[0]

        if request_str.startswith('GET'):
            # Serve the HTML file
            with open('www/led.html', 'r') as f:
                html_content = f.read()
            response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html_content}"
        else:
            if "red" in color:
                turn_on_red_led()
            elif "blue" in color:
                turn_on_blue_led()
            elif "green" in color:
                turn_on_green_led()
            elif "sky" in color:
                turn_on_sky_led()
            elif "clear" in color:
                turn_on_clear_led()    

            # Send a simple response back to the client
            response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nLED control successful for {color}!"
        
        await writer.awrite(response)
    except Exception as e:
        print("Error handling request:", e)
    finally:
        await writer.aclose()

# Create an asyncio event loop
loop = asyncio.get_event_loop()

# Start the HTTP server
loop.create_task(asyncio.start_server(handle_request, "0.0.0.0", 80))

# Run the event loop
loop.run_forever()
 </p>

Monday, February 15, 2021

ESP32 control via Node-Red

 This is a new variant of my home automation efforts where I will attempt to control a string of  LED's connected to an ESP32 and control several LED sequence's via  node-red. I should add that I don't really consider myself a programmer but more of a hacker. I modify existing exaple code to do whatever I'm trying to achieve. I do have an electronics background and got most of my electronics training in the Air Force where I was an Electronic Warfare Systems tech. I have worked in a variety of industrial maintenance and some broadcast TV electronics tech support. I've retired now and do some honey do list type handyman work part time. I got in on the drone thing pretty early on... well before you could buy one off the shelf. They used an arduino based microprocessor and were pretty primitive  compared to what's available today. They quickly evolved to STM32 based controllers with tons of capability. I still fly them occasionally and done some video's but they have become a pretty pricey hobby for an old retired handyman.

I have borrowed heavily from Rui Santos and his Random Nerd Tutorials and occasionally get help from the Node-Red support forum.

The code below is from his neopixel demo where he had some switches to select the different routines for a string of neopixels. This version runs trough the different routines and ends with the rainbow cycle routine which it runs 10 times before ending. 

I want to control these routines via node-red and hopefully illustrate for you how I went about it.

 

Monday, December 24, 2018

Node Red and ESP32

I'm still pretty new to python and node red... I'm experimenting with upython on ESP32 and node red on raspberry pi. I'm more of a hacker than a programmer... I find examples that I can modify to do what I want. Several good examples here... 



There is a new feature on github called gists... a way to share bits of code that are not so easy to share via a web page such this blog which is hosted on google blogger. You can copy the code in the window below and then import it into node red.

The code is for the bottom bit below as the top portion is avalable from the link above. The top one was the example I found and the bottom one was after modifications for my use. The original only changed the look and label. I've added the capability to send custom topic and payload to turn on a relay controlled by an ESP32 running upython. It acts as a client and the raspberry pi acts a the broker using the mosquito protocol. Node red is a package running on the raspberry pi which provides a graphical interface to create flows like the ones below to direct information to and from client devices on your local network.

Friday, March 30, 2018

Alexa Home Automation

I spent the last couple of weeks getting up to speed on some of the home automation tools that are available off the shelf and home brew versions as well. Many of the components can be used with the Alexa and similar devices. I picked up a couple of the Echo Dot 2nd generation devices and have some of the remote control devices that work with it on the way.  I can see where this thing might be a bit addictive as well as being capable of some pretty sophisticated uses.

I picked up one of these ESP32 boards and it's what got me on the home automation bandwagon. I didn't really know much about it when I bought it but in researching some of its capabilities I learned that it and its predecessor the ESP8266 can be used as a remote device to be controlled by an Alexa. The 32 board is still pretty bleeding edge but has a ton of capability. It has both WiFi and Bluetooth connectivity and a great deal of  information about them is available on the web.

It was a fairly easy undertaking to set up the ESP32 device as a webserver on my home wifi network that can control via a web page... 8 relays that can control lights or other low current devices.


Control by Alexa is not so well supported on the ESP32 but there are many examples using the ESP8266. I have a couple of them on the way.

I also have some Sonoff devices on the way as well that work with the Alexa or with a phone app right off the shelf.

I will be posting more on this undertaking as these parts arrive and get put online.

Stay tuned ðŸ˜€

Sunday, March 18, 2018

ESP32 and You

I recently bought one of the ESP32 development boards without really knowing what it was but it looked like it had potential. The first disappointment is in the package itself... it is not breadboard friendly and it seems no one has really attempted to address this with a breakout board that would make it more user friendly.

Finding good examples too is a bit of a tester... lots of smoke and mirrors and not a lot of user friendly help online. There are lots of high end undertakings out there, Luanode, Python, Fauxmo, AsyncTCP, NodeMC, even a few attempts to create a stand alone Alexa type device on ESP32. It's all a bit mind boggling for an old arduino hacker like me. Part of the problem is that this board is an upgrade of the ESP8266 and most of the tutorials try to show how integrate them both into their project. They are different enough that this only seems to make it all the more difficult for the beginner.

To be fair they are both relatively new and are both targeted more to developers that to ordinary hackers like myself. To me one of the most interesting uses of this device is as a remote device that can be controlled by an Alexa... and this will be the focus of this site.

I've spent the past couple of days looking for examples of projects that use the board in this way. I found one with promise but seemed to have lost track of it. In it the board was connected to several relays and  programmed to be found by an Alexa device. The Alexa would learn what devies were connected to it and what voice commands would be used to turn these things on and off. You could also group thing together such and all the lights in a particular room could be turned on or off with one command or you could control them individually. Try as I might I've not been able to find that particular application again.

So I invite you to join me in this undertaking to try and solve this mystery. I'm still waiting on a few components to experiment with and probably the easiest thing is to just use off the shelf stuff that is already available to do what I have in mind. 

Ironically I had no Alexa and didnt even know that they could be used to control remote devices. I do have one on the way and a few of the Sonoff devices to get the ball rolling. I also have a couple of the ESP32 devices on hand and an 8 relay device on the way.

So stay tuned and we'll see how it goes.