Watering Flowers with ESP8266


I was thinking how to use the ESP8266 board for something useful and maybe cool 🙂 and I came up with the idea to control a relay with which to switch on/off a water pump which will water the flowers automatically.


The main part is of course the ESP8266-01. You can obtain one from banggood.com

Flashing The ESP8266 with Custom ROM

The first challenge was how to control the IO ports. I realised that the original firmware cannot handle (as far as I know) this task so I flashed the esp with the lua script rom – nodemcu (https://github.com/nodemcu/nodemcu-firmware). To do this first download the firmware flashing tool – esptool from here: https://github.com/themadinventor/esptool (assume connected esp)
After that connect the esp to your computer. I’ve used my home-built TTL-to-RS232 together with usb to serial port cable. Very important is to connect the esp to the power supply and GPIO0 pin to ground. When the later pin is grounded during reset the chip goes into flashing mode.

Once there from your console issue the following command:

sudo python esptool.py --port /dev/tty.usbserial write_flash 0x00000 /Users/hbb/Downloads/nodemcu_latest.bin

Here probably due to bad connection I had to retry several times until the firmware was fully loaded on the ESP.
When you are ready, unground the GPIO0 pin and restart. If you are still connected you should see similar message in your terminal program:

NodeMCU 0.9.5 build 20150214  powered by Lua 5.1.4

Uploading the Pin control program

Having the NodeMCU ROM we can control the gpio pins of ESP8266 using lua script. On the NodeMCU github page there many examples showing how to do it. We are going to create a http server recognising two commands: pump_off and pump_on. We need two lua script files init.lua that will be executed after restart and the actual program: server.lua

pin = 4
if string.find(payload,"pump_on") ~= nil then gpio.write(pin,gpio.HIGH) end
if string.find(payload,"pump_off") ~= nil then gpio.write(pin,gpio.LOW) end
conn:send("Hello, NodeMcu.") end) conn:on("sent",function(conn) conn:close() end) end)

To upload the two files on the ESP I am using the nodemcu-uploader program which can be found here: https://github.com/kmpm/nodemcu-uploader
You can list already uploaded files with this command:

nodemcu-uploader.py --port /dev/tty.usbserial upload file list

Uploading files is done with this command:

nodemcu-uploader.py --port /dev/tty.usbserial upload server.lua --restart

Connecting the ESP to the wireless network and Sending commands

To connect the ESP using a static ip address you can use these commands in the init.lua script:

 init.lua wich uses fixed IP adresses
 wifi.sta.config("YOUR SSID","YOUR KEY")
 wifi.sta.setip({ip="FIXED IP ADDRESS",netmask="YOUR NETMASK",gateway="YOUR GATEWAY"})
 print("ESP8266 mode is: " .. wifi.getmode())
 print("The module MAC address is: " .. wifi.ap.getmac())
 print("Config done, IP is "..wifi.sta.getip())
 dofile ("domoticz.lua")

After your esp boots just open your browser and point it to http://your-ip-address/pump_off this will turn off GPIO2 pin. If you use http://your-ip-address/pump_on you will turn on your pump.

To connect using dynamic IP use:

Init.lua wich uses DHCP
 wifi.sta.config("YOUR SSID","YOUR KEY")
 tmr.alarm(1, 1000, 1, function()
  if wifi.sta.getip()== nil then
  print("IP unavaiable, Waiting...")
 print("ESP8266 mode is: " .. wifi.getmode())
 print("The module MAC address is: " .. wifi.ap.getmac())
 print("Config done, IP is "..wifi.sta.getip())
 dofile ("domoticz.lua")


The schematic is very simple. It uses BC547 NPN transistor to drive a relay which in turn switches the water pump on or off. Important thing is that the GPIO pins of the ESP must not be grounded during boot or otherwise the controller will go into flashing mode. For this reason there is a capacitor C1 which will generate short pulse on power on. Initially I used 1 uF capacitor which turned out to be insufficient. Now with 220 uF the pulse is longer than needed but everything works fine anyway.



ESP on The Board



It seems that using ESP8266 for applications where internet connection is needed is much easier than some other options, for example wiz550io and also in much smaller form factor.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.