Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Visvanathan Srinivasan
Published © GPL3+

ESP-Controlled Plant Watering System

Water plants in your balcony with solar power and control the pump from anywhere in the world!

BeginnerFull instructions provided4 hours10,064
ESP-Controlled Plant Watering System

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
RobotGeek Relay
RobotGeek Relay
×1
Submersible pump
×1
Pneumatic hose 4 mm
×1
Pneumatic TEE
×2
USB Powerbank
×1
Solar panel 3W
×1

Software apps and online services

Cloud MQTT

Story

Read more

Code

init.lua

Lua
Add Wifi credentials, MQTT subscription fields and desired Static IP number
-------Module created by Visvanathan Srinivasan (www.thingsbuzz.com)------
wifi.setmode(wifi.STATION)
wifi.sta.config("xxxYourWIFIxxx","xxxYourPassxxx")
wifi.sta.setip({ip="xxxStatic IPxxx",netmask="255.255.255.0",gateway="xxx"})
print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())
print(wifi.sta.getip())

-- MQTT connect code Engineered by rutierut, inspired by MikeV
broker = "m11.cloudmqtt.com"     -- IP or hostname of MQTT broker
mqttport = YourPortxx          -- MQTT port (default 1883)
userID = "useridxxx"              -- username for authentication if required
userPWD  = "passxxx"            -- user password if needed for security
clientID = "Devicenamexxx"        -- Device ID
Pumptopic = "Topicnamexx"
oppin = 3                -- IO Index of GPIO which is connected to a device
chpin = 7
gpio.mode(oppin, gpio.OUTPUT)

function setPort(data) 
  if (string.find(data,"on")~=nil ) then 
     gpio.write(3, gpio.HIGH); 
  end
  if (string.find(data,"off")~=nil ) then 
     gpio.write(3, gpio.LOW); 
  end 
end 

--- CloudMQTT Server info ------ 
function mqtt_do()
m1 = mqtt.Client(clientID, 1200, userID, userPWD); 
--- D0 as indicator light ------ 
m1:on("offline", function(con) 
     print ("offline") 
    end) 
m1:on("message", function(conn, topic, data) 
 print(topic .. ":" ) 
 if data ~= nil then 
   print(data); 
   setPort(data); 
 end 
end) 
m1:connect("m11.cloudmqtt.com", mqttport, 0,  
          function(client) 
          m1:subscribe("Solarpumpcontrol",0,function(client)  
          print("subscribed") 
          end)    
       end,  
          function(client, reason) print("failed reason: "..reason) end);
 collectgarbage();
end
------Timer for MQTT Function---------
tmr.alarm(0, 200000, 1, function() 
   mqtt_do()
   tmr.delay(1000)
end) 
-----HTTP Server-----
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.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
            gpio.write(oppin, gpio.HIGH);
               elseif(_GET.pin == "OFF1")then
            gpio.write(oppin, gpio.LOW);
                end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

Credits

Visvanathan Srinivasan
1 project • 2 followers
Mechanical engineer dabbling with Special purpose machines, SMART Shop floor, SMART Home.... Using ESP8266 modules since 2016

Comments