First ESP8266 home automation node

It was bound to happen…

You had probably seen it happening somewhere else, maybe you were wondering why not here? You might even have cried a bit, but do not fear, I caught up with time, and also made something with a ESP8266 module.

The model I decided to try with is the ESP-01, which seems to be the most common and easiest to get started with. But at the same time, it is also the most limited one. This module only got two GPIO, so it is very limited to what it is capable of controlling, without adding a shift register.

This following code can run on the ESP8266, after it is flashed with NodeMCU, and can be uploaded with LuaUploader. (I am going to cover this in another guide.)

wifi.setmode(wifi.STATION);
wifi.sta.config("SSID" ,"WIFIKEY");

GPIO00 = 3;
GPIO02 = 4;

gpio.mode(GPIO00, gpio.OUTPUT);
gpio.mode(GPIO02, gpio.OUTPUT);

nodepass = "NODEPASS";

srv=net.createServer(net.TCP);
srv:listen(80,function(conn)
	conn:on("receive",function(conn,payload)
		if (string.find(payload, "favicon.ico") ~= nil) then
			return;		
		end
 
		local dotaz = "";
 
		if (string.find(payload, "GET / HTTP/1.1") ~= nil) then
			--print("No command received")
		else
			kdesi={string.find(payload,"cmd=")}
			
			local password = string.sub(payload, 6, string.len(nodepass)+5);
			
			if (password == nodepass) then
				--If POST value exist, set pins
				if (kdesi[2]~=nil) then
					dotaz = string.upper(string.sub(payload,kdesi[2]+1,#payload));
					local splitpos = string.find(dotaz, " ");
					dotaz = string.sub(dotaz, 0, splitpos-1);
					print("Command: " .. dotaz);
		
					if (dotaz == "ON1")  then gpio.write(GPIO00,gpio.HIGH) end
					if (dotaz == "OFF1")  then gpio.write(GPIO00,gpio.LOW) end
					if (dotaz == "ON2")  then gpio.write(GPIO02,gpio.HIGH) end
					if (dotaz == "OFF2")  then gpio.write(GPIO02,gpio.LOW) end
				end
			end
		end
  
		local gpio0 = gpio.read(GPIO00);
		local gpio2 = gpio.read(GPIO02);
		local buffer = "{";
		buffer = buffer .. "\"mem\":";
		buffer = buffer .. node.heap();
		buffer = buffer .. ",\"ip\":\"";
		buffer = buffer .. wifi.sta.getip();
		buffer = buffer .. "\"";
		buffer = buffer .. ",\"GPIO00\":";
		buffer = buffer .. gpio0;
		buffer = buffer .. ",\"GPIO02\":";
		buffer = buffer .. gpio2;
		buffer = buffer .. "}";
		conn:send(buffer);
	end)
	
	conn:on("sent",function(conn)
		conn:close();
		collectgarbage();
	end)
end)

To use this, and turn the GPIO pins high or low, you call http://ip/NODEPASS/?cmd=ON1 to turn GPIO00 high, cmd=OFF1 to turn GPIO00 low, cmd=ON2 to turn GPIO02 high, and cmd=OFF2 to turn GPIO02 low.

For added security, I also added “nodepass”, which can be set in the LUA code, and must then match the call to the node afterwards to turn the pins high or low. When just calling the IP, it will respond with a bit info in JSON format.

{"mem":10192,"ip":"192.168.1.109","GPIO00":1,"GPIO02":0}

which will show how much memory is free, the ip of the node, and the status for both GPIO pins.

Leave a Reply