Things used in this project

Hardware components:
Nodemcu
NodeMCU ESP8266 Breakout Board
×1
Rg relay a
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

Code

init.luaLua
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

9e26fadc7852d457b5fc2591810ca9e1
Visvanathan Srinivasan

Mechanical engineer dabbling with Special purpose machines, SMART Shop floor, SMART Home.... Using ESP8266 modules since 2016

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

WiFi Voice Controlled Robot With NodeMCU
Easy
  • 1,478
  • 12

Full instructions

WiFi Voice Controlled Robot using as a controller, a NodeMCU! The voice control is done by an Android device.

Voice Activated Control With Android and NodeMCU
Easy
  • 582
  • 5

Full instructions

We will explore how to control Home Devices, thru voice and buttons using an Android App.

SONOFF - ESP8266 Update Firmware With Arduino IDE
Easy
  • 503
  • 3

Full instructions

This is a guide for how to upload new firmware using Arduino IDE. Sonoff is an ESP8266-based WiFi wireless switch.

Cloud-Based Temperature Logger for Homebrew Fermenter
Easy
  • 615
  • 8

Work in progress

Using battery powered NodeMCU v2 and DS18B20 thermometer to log temperature and system voltage on Google Sheets.

ESP32 Touch Controlled LED Using Arduino IDE
Easy
  • 595
  • 3

Protip

This tutorial uses built-in touch sensors on the ESP32 Dev Module to control an LED.

PlatformIO for Visual Studio Code
Easy
  • 2,586
  • 13

Protip

PlatformIO is an open source ecosystem for IoT development. And now, PlatformIO for Visual Studio Code is coming!

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback