The Invincible A personal archive of software, founder life, travel, and the occasional detour.
Randoms

Smart Home – Programming ESP8266 Module

Assuming your module is already armed with the NodeMCU firmware, its time to program our end point so we could control it remotely through wifi. In our case at the moment, we simply need to control one of its output PIN to give out power ( Switch on / off ) depending on the signals we send through our home bridge device.

An easy way to do this is to run a small webserver on our ESP module and set few url’s to control our output. In our case we just need two unique url’s to switch on and off. Or we can also create a more generic url which we can pass on PIN numbers and STATUS to switch on and off. but that seems unnecessarily open and i’d prefer to have pre defined urls according to the need.

Its time to start programming our module. Assuming you already have ESPlorer application and its all setup to communicate with your device, lets insert the following lua code in to the ESP module.

 

wifi.setmode(wifi.STATION)
wifi.sta.config("WIFI_SSID","WIFI_PASSWORD")
led1 = 0
gpio.mode(led1, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        buf = buf.."

<h1> ESP8266 Web Server</h1>

”; buf = buf..”

GPIO0 <a href=&quot;?pin=ON1&quot;><button>ON</button></a>&nbsp;<a href=&quot;?pin=OFF1&quot;><button>OFF</button></a>

”; local _on,_off = "","" if(_GET.pin == “ON1”)then gpio.write(led1, gpio.LOW); elseif(_GET.pin == “OFF1”)then gpio.write(led1, gpio.HIGH); end —client:send(buf); client:close(); collectgarbage(); end) end)

This is a temporary script i’ve written to test things out at the time of writing this posts. Incase i forgot to come back here and update, please get the latest codes from here https://github.com/nilansanjaya/smart-home/

When you are done sending that code to your ESP module, you should now be able to switch on and off its built in LED light using the below url’s ( Assuming you are connected to the same wifi network )

http://youripaddress/?pin=ON1
http://youripaddress/?pin=OFF1

If that’s working, you can simply send requests to this through homebridge. Details on that should be on another post.

Related

More posts like this

Not Using AI Is a Choice, No more excuses

Frontier AI companies like OpenAI and Anthropic are competing hard, while open source models are catching up fast. This competition is driving costs down. The price per 1M tokens keeps dropping, which means you can do a lot more for a lot less. So the old excuse of 'I don't have the budget to use AI in my business' is quickly disappearing.

Read more
Day 5 of 31: Connections and Caffeine

The first Monday of the new year always feels like a reset button. For our service company, yesterday marked the official restart, and as expected, it was a busy one. The day was filled with catch-ups, follow-ups, and lots of reconnecting with clients who had patiently waited for us to get back into full swing.

Read more
Day 4 of 31: When the Day Slips Off Track

Some Sundays move at their own slow pace, and yesterday was one of them. I went into the weekend feeling excited to refactor a piece of code that’s been bothering me for weeks. But by Sunday evening, the work was still unfinished and instead of accepting that, I spent way too much time complaining to

Read more