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

Contact

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

BLYNKING webpage data using ThingSpeak and NodeMcu
Easy
  • 320
  • 4

Protip

A simple project to display a particular webpage data in blynk app via bluetooth module and using thingspeak platform.

Automation Using Amazon Echo Alexa and NodeMCU Without IFTTT
Easy
  • 401
  • 3

Protip

Control relays attached to NodeMCUs using Amazon Echo Alexa without IFTTT.

Add WiFi to Arduino UNO
Easy
  • 1,194
  • 3

Protip

Connect your mincrocontroller to the internet by adding ESP8266 WiFi Module!

Arduino-Controlled Wall-E Head
Easy
  • 629
  • 8

Full instructions

In this project I'll show you how to control a servo remotely from a regular web page without needing a web server in your Arduino board.

NodeMCU Webserver with Class-Based 60-Second Temp Average
Easy
  • 343
  • 2

Protip

Averaging temperatures over 60 seconds smooths some of the sensor limitations, allowing for a smoother graphic.

Send e-mail - NodeMCU with Mikroelektronika Clicks
Easy
  • 192
  • 2

Full instructions

We read the data with the ambient click and in case the values become small, an e-mail message is sent.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login