element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Portable Electronics Kit
  • Challenges & Projects
  • Project14
  • Portable Electronics Kit
  • More
  • Cancel
Portable Electronics Kit
Blog Control an relay from web server using NodeMCU
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Portable Electronics Kit to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: kk99
  • Date Created: 11 Feb 2018 9:04 PM Date Created
  • Views 2162 views
  • Likes 8 likes
  • Comments 4 comments
  • portableekitch
Related
Recommended

Control an relay from web server using NodeMCU

kk99
kk99
11 Feb 2018

image

I would like to present simple circuit which allow to control device via relay with usage of web server running on ESP8266 board. The main component is ESP8266 board which allow to run script in LUA language. This ESP8266 board is connected with relay module via pin headers. Here is relay module:
image

Coil of relay is driven by NPN transistor from GPIO16 pin (PIN0) of ESP8266 board. Here is schematic of relay module:

image

To ESP8266 board was uploaded code (init.lua) which do following things:
- creates WiFi software access point with static IP address,
- sets PIN D0 as output and proper state,
- creates web server with simple HTML page which allow to control relay remotely.

 

Here code of LUA script:

cfg={} 
cfg.ssid="WiFiSwitch" 
cfg.pwd="Password" 
wifi.ap.config(cfg) 
cfg={} 
cfg.ip="192.168.1.1"; 
cfg.netmask="255.255.255.0"; 
cfg.gateway="192.168.1.1"; 
wifi.ap.setip(cfg); 
wifi.setmode(wifi.SOFTAP) 
gpio.mode(0, gpio.OUTPUT) 
gpio.write(0, gpio.LOW) 
srv=net.createServer(net.TCP) 
srv:listen(80,function(conn) 
conn:on("receive",function(conn,request) 
print(request) 
_, j = string.find(request, 'switchState=') 
if j ~= nil then 
command = string.sub(request, j + 1) 
if command == 'on' then 
gpio.write(0, gpio.HIGH) 
else 
gpio.write(0, gpio.LOW) 
end 
end 
conn:send('HTTP/1.1 200 OK\n\n') 
conn:send('<!DOCTYPE html>') 
conn:send('<html lang="en">') 
conn:send('<head><meta charset="utf-8" />') 
conn:send('<title>Remote switch</title></head>') 
conn:send('<body><h1 align="center">Remote switch</h1>') 
if gpio.read(0) == gpio.HIGH then 
switchValue = "ON" 
else 
switchValue = "OFF" 
end 
conn:send('<p>The switch is ' .. switchValue .. '</p>') 
conn:send('<form method="post">') 
conn:send('<input type="radio" name="switchState" value="on">ON</input><br />') 
conn:send('<input type="radio" name="switchState" value="off">OFF</input><br />') 
conn:send('<input type="submit" value="Switch" />') 
conn:send('</form></body></html>') 
conn:on("sent",function(conn) conn:close() end) 
end) 
end)


Here is view of this HTML page:
image

Attachments:
code.zip
  • Sign in to reply

Top Comments

  • genebren
    genebren over 7 years ago +3
    Pretty cool project. One step closer to controlling the world via WiFi . What are you planning on controlling with your relay? Gene
  • kk99
    kk99 over 7 years ago in reply to genebren +2
    I would like to use this module to control 12 V output from regulator from solar system. In future I would like to add to this module possibility to set timer to control how long this relay is in ON or…
  • rcaste53
    rcaste53 over 7 years ago +1
    The connection of 1N4148 diode es wrong. Its cathode must be connected to VCC
  • kk99
    kk99 over 7 years ago in reply to rcaste53

    Yes, thank you for information.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rcaste53
    rcaste53 over 7 years ago

    The connection of 1N4148 diode es wrong. Its cathode must be connected to VCC

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • kk99
    kk99 over 7 years ago in reply to genebren

    I would like to use this module to control 12 V output from regulator from solar system. In future I would like to add to this module possibility to set timer to control how long this relay is in ON or OFF state.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • genebren
    genebren over 7 years ago

    Pretty cool project.  One step closer to controlling the world via WiFiimage.  What are you planning on controlling with your relay?

    Gene

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube