一,教程目的

通过Nodemcu+ESP8266通过网页去控制继电器以及灯泡。

二,实验环境

操作系统: windows10
硬件: ESP8266开发板 x1(CP2102)需要下载CP2102驱动点我下载
继电器: 1路5V低电平触发 x1
面包板: 随便都行
杜邦线若干: 一般买面包板送线

三,物品清单

四,硬件连接

1.继电器上的VCCVCC可以理解为正极接入,连接板子上的3V33V3是3.3V供电,将继电器上的GNDGND是负极,连接板子上的GND,将继电器上的ININ可理解为信号控制,连接板子上的D1,接线法看图。

2.将继电器上的COM,连接板子上其他3V3口,将继电器上的NC连接LED灯上的正极(如果要控制家用电器,请将COM接火线NC接零线,如果要在之间加电器请在零线上加)再将板子上的GND接LED的负极,接线法看图。

公共触点:COM
常开触点:NC
请勿接错

继电器接线图:


不懂面包板的朋友请看面包板后面就懂面包板怎么用了
3.将第二个小灯泡的正极接板子的D2,负极接板子的GND,接线法看图。

五,Arduino的esp8266的配置

1.打开Arduino-文件-首选项,开发板管理填http://arduino.esp8266.com/stable/package_esp8266com_index.json


2.工具-开发板-开发板管理

搜索esp8266

安装上去

六,开始写代码


#include <ESP8266WiFi.h>// Add wifi access point credentiaals
const char* ssid     = "这里填你家WIFI名称";
const char* password = "这里填你家WIFI密码";WiFiServer server(80);// Set port to 80String header; // This storees the HTTP request// Declare the pins to which the LEDs are connected
int inD1 = D1;
int inD2 = D2; String D1state = "关";// state of green LED
String D2state = "关";// state of red LEDvoid setup() {Serial.begin(115200);// Set the pinmode of the pins to which the LEDs are connected and turn them low to prevent flunctuationspinMode(inD1, OUTPUT);pinMode(inD2, OUTPUT);digitalWrite(inD1, LOW);digitalWrite(inD2, LOW);//connect to access pointWiFi.begin(ssid, password);Serial.print("Connecting to ");Serial.println(ssid);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}// Print local IP address and start web serverSerial.println("");Serial.println("WiFi connected.");Serial.println("IP address: ");Serial.println(WiFi.localIP());// this will display the Ip address of the Pi which should be entered into your browserserver.begin();
}void loop(){WiFiClient client = server.available();   // Listen for incoming clientsif (client) {                             // If a new client connects,String currentLine = "";                // make a String to hold incoming data from the clientwhile (client.connected()) {            // loop while the client's connectedif (client.available()) {             // if there's bytes to read from the client,char c = client.read();             // read a byte, thenSerial.write(c);                    // print it out the serial monitorheader += c;if (c == '\n') {                    // if the byte is a newline character// if the current line is blank, you got two newline characters in a row.// that's the end of the client HTTP request, so send a response:if (currentLine.length() == 0) {// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)// and a content-type so the client knows what's coming, then a blank line:client.println("HTTP/1.1 200 OK");client.println("Content-type:text/html");client.println("Connection: close");client.println();// turns the GPIOs on and offif (header.indexOf("GET /D1/on") >= 0) {Serial.println("D1 开");D1state = "开";digitalWrite(inD1, HIGH);} else if (header.indexOf("GET /D1/off") >= 0) {Serial.println("D1 关");D1state = "关";digitalWrite(inD1, LOW);} else if (header.indexOf("GET /D2/on") >= 0) {Serial.println("D2 开");D2state = "开";digitalWrite(inD2, HIGH);} else if (header.indexOf("GET /D2/off") >= 0) {Serial.println("D2 关");D2state = "关";digitalWrite(inD2, LOW);}// Display the HTML web pageclient.println("<!DOCTYPE html><html>");client.println("<head><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\" charset=\"utf-8\">");client.println("<link rel=\"icon\" href=\"data:,\">");// CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferencesclient.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");client.println(".button2 {background-color: #77878A;}</style></head>");// Web Page Headingclient.println("<body><h1>nodemcu 网页测试 BY:southwind QQ:511114355</h1>");// Display current state, and ON/OFF buttons for GPIO 5  client.println("<p>D1 - 信息 " + D1state + "</p>");// If the green LED is off, it displays the ON button       if (D1state == "关") {client.println("<p><a href=\"/D1/on\"><button class=\"button\">开</button></a></p>");} else {client.println("<p><a href=\"/D1/off\"><button class=\"button button2\">关</button></a></p>");} // Display current state, and ON/OFF buttons for GPIO 4  client.println("<p>D2 - 信息 " + D2state + "</p>");// If the red LED is off, it displays the ON button       if (D2state == "关") {client.println("<p><a href=\"/D2/on\"><button class=\"button\">开</button></a></p>");} else {client.println("<p><a href=\"/D2/off\"><button class=\"button button2\">关</button></a></p>");}client.println("</body></html>");// The HTTP response ends with another blank lineclient.println();// Break out of the while loopbreak;} else { // if you got a newline, then clear currentLinecurrentLine = "";}} else if (c != '\r') {  // if you got anything else but a carriage return character,currentLine += c;      // add it to the end of the currentLine}}}// Clear the header variableheader = "";// Close the connectionclient.stop();Serial.println("Client disconnected.");Serial.println("");}
}

七,烧录进开发板

点击Arduino的上传

代码上传完后访问esp8266的IP

八,效果演示

在路由器管理中已经可以看见ESP8266的ip了

我们在浏览器中输入:192.168.4.103,并打开串口监视器查看信息

我们看看Arduino的串口监视器

我们去打开D1上的继电器看看效果


LED可以正常亮起,我们吧D2口上的LED也打开

正常亮起。

九,总结

不可把开发板上的接口直接去接上家庭电器,因为家庭电器220V,板子承受不了,若要接家庭电器,请在继电器上接,红线接COM,零线接NC。

Nodemcu+ESP8266实现WEB控制家用电器相关推荐

  1. ESP8266 NodeMCU:ESP-NOW Web 服务器传感器仪表板(ESP-NOW + Wi-Fi)

    在本项目中,您将学习如何使用 ESP8266 NodeMCU 板托管 Web 服务器并同时使用 ESP-NOW 通信协议.您可以让多个 ESP8266 板通过 ESP-NOW 将传感器读数发送到一个 ...

  2. ESP8266+Arduino实现控制【开关外设装置-记录一】

    具体实现功能:网页+APP 实现控制舵机等IO操作. 1.使用 AP 模式提供WiFi接入,终端设备通过 Web 网页实现舵机控制: 2.使用串口连接蓝牙模组,提供蓝牙接入,终端设备通过手机APP实现 ...

  3. esp8266原理图_ESP32/ESP8266使用MicroPython控制DHT11/DHT22

    背景知识视频教程 高级ESP32 - 国外课栈​viadean.com 使用NodeMCU(由ESP8266支持),MicroPython和PyCharm进行物联网 - 国外课栈​viadean.co ...

  4. html控制智能家居,一种通过web控制的智能家居系统的制作方法

    一种通过web控制的智能家居系统的制作方法 [专利摘要]本发明提供了一种通过web控制的智能家居系统,包括移动终端,以MIPS芯片为核心的WiFi转串口模块,STM32控制模块,DALI控制模块,SI ...

  5. ESP8266 01S 继电器控制智能灯实现

    ESP8266 01S 继电器控制 智能灯实现(使用小度音响) 最近研究esp8266,发现可以通过wifi连接物联网云平台实现远程控制,同时也可以加入小度语音DIY简单的智能家居. 前言 本人也是刚 ...

  6. OpenStack环境搭建(四:web控制端各节点的部署及配置)

    实验要求: 完成Virtual box平台安装,会应用相关操作: 在virtual box虚拟平台上部署Fuel Master节点: 在virtual box虚拟平台上部署计算节点Computer: ...

  7. 远程WEB控制MP3播放器设计(基于mini2440)

    网上有很多 基于mini2440的MP3播放器设计的资料,多是按键控制,这里博主做了些轻微改动,利用远程WEB来控制MP3播放,具体怎么实现,下面会给出,大家先看看效果: WEB界面: 后台运行: 因 ...

  8. 网页里如何嵌入服务器控制,在嵌入式设备中实现Web动态服务与Web控制的实现思路...

    随着网络技术的不断发展,嵌入式系统将不断地和网络相结合.嵌入式Web技术是计算机领域研究的热点,其优点是开发成本低.通用性强,能运行在8位或16 位MCU环境中,其丰富的Web用户图形界面使得嵌入式设 ...

  9. hadoop3访问hdfs web控制页面遇到的各种问题总结

    目录 问题1:浏览器访问不了hdfs web 问题2:浏览器点击Browse the file system打开文件系统报错:Failed to retrieve data from /webhdfs ...

最新文章

  1. php math函数
  2. qt designer启动后不显示界面问题的原因与解决办法
  3. C++ 二维数组示例 - 控制台和MFC版
  4. python 匹配字符串多个_在Python中匹配多个数据集的字符串
  5. WCF部署到IIS异常(详细: 不能加载类型System.ServiceModel.Activation.HttpModule )
  6. ngCloak 实现 Angular 初始化闪烁最佳实践
  7. AI+服务 阿里巴巴如何做智能服务转型?
  8. 吴恩达深度学习5.1笔记_Sequence Models_循环序列模型
  9. 分享java web 期末项目实验源码20套,BBS论坛,ERP管理系统,OA自动化等等
  10. php扩展拦截请求,PHP的拦截器实例分析
  11. Linux MySQL主主复制(Replication)(MySQL数据双向同步)配置
  12. git push命令入门
  13. (1)、win10 本地 安装 rabbitmq
  14. layui 点击头像 上传头像
  15. 使用Java复制文件及显示进度
  16. linux配置jdk环境变量
  17. ps界面为啥突然变大了_PS如何用快速蒙版抠图,超实用的技巧!!!
  18. 电感滤波电路的工作原理
  19. composer安装fxp/composer-asset-plugin包报错问题解决方案
  20. 进程虚拟地址空间区域划分

热门文章

  1. 菜鸟教程java_JAVA笔记(菜鸟教程)
  2. Java进阶基础知识
  3. 卡特兰数(Catalan)公式、证明、代码、典例.
  4. 如何将计算机的硬盘分割,怎么给电脑分盘
  5. org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir异常解决方法 【转】
  6. 计算两个一维数组的卷积
  7. 应用层网关防火墙简介
  8. 一个值得喜欢的女孩子
  9. 如何安装K8s面板工具:kuboard
  10. 瑞士成立工作组以协助区块链公司开立银行账户