Arduino 基于 ESP8266 配置WIFI模块

使用ESP8266作为服务器,使用浏览器访问该服务器,从而控制LED灯

  • 选择 【文件】->【示例】->【ESP8266WIFI】->【WiFiWebServer】
  • 用Arduino新建一个文件,将刚打开的的WIFIWebServer的内容复制过去
  • 修改 ssid 和 password 为自家路由器的名称以及密码
  • 将程序上传到 ESP8266 开发板中
  • 最后就可以通过网站 [ip]/gpio/1 或 [ip]/gpio/0 控制灯开关
  1. # http://192.168.0.57/gpio/1
  2. # ip 地址在序列库中可以看到
  • 附上代码
  1. /*
  2. * This sketch demonstrates how to set up a simple HTTP-like server.
  3. * The server will set a GPIO pin depending on the request
  4. * http://server_ip/gpio/0 will set the GPIO2 low,
  5. * http://server_ip/gpio/1 will set the GPIO2 high
  6. * server_ip is the IP address of the ESP8266 module, will be
  7. * printed to Serial when the module is connected.
  8. */
  9. #include <ESP8266WiFi.h>
  10. const char* ssid = "YUCHANJIUYE";
  11. const char* password = "19283746";
  12. // Create an instance of the server
  13. // specify the port to listen on as an argument
  14. WiFiServer server(80);
  15. void setup() {
  16. Serial.begin(115200);
  17. delay(10);
  18. // prepare GPIO2
  19. pinMode(LED_BUILTIN, OUTPUT);
  20. digitalWrite(LED_BUILTIN, 0);
  21. // Connect to WiFi network
  22. Serial.println();
  23. Serial.println();
  24. Serial.print("Connecting to ");
  25. Serial.println(ssid);
  26. WiFi.begin(ssid, password);
  27. while (WiFi.status() != WL_CONNECTED) {
  28. delay(500);
  29. Serial.print(".");
  30. }
  31. Serial.println("");
  32. Serial.println("WiFi connected");
  33. // Start the server
  34. server.begin();
  35. Serial.println("Server started");
  36. // Print the IP address
  37. Serial.println(WiFi.localIP());
  38. }
  39. void loop() {
  40. // Check if a client has connected
  41. WiFiClient client = server.available();
  42. if (!client) {
  43. return;
  44. }
  45. // Wait until the client sends some data
  46. Serial.println("new client");
  47. while(!client.available()){
  48. delay(1);
  49. }
  50. // Read the first line of the request
  51. String req = client.readStringUntil('\r');
  52. Serial.println(req);
  53. client.flush();
  54. // Match the request
  55. int val;
  56. if (req.indexOf("/gpio/0") != -1)
  57. val = 0;
  58. else if (req.indexOf("/gpio/1") != -1)
  59. val = 1;
  60. else {
  61. Serial.println("invalid request");
  62. client.stop();
  63. return;
  64. }
  65. // Set GPIO2 according to the request
  66. digitalWrite(LED_BUILTIN, val);
  67. client.flush();
  68. // Prepare the response
  69. String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  70. s += (val)?"high":"low";
  71. s += "</html>\n";
  72. // Send the response to the client
  73. client.print(s);
  74. delay(1);
  75. Serial.println("Client disonnected");
  76. // The client will actually be disconnected
  77. // when the function returns and 'client' object is detroyed
  78. }

Arduino 基于 ESP8266 配置WIFI模块的更多相关文章

  1. Obloq模块:基于ESP8266的物联网模块

    OBLOQ 物联网模块 OBLOQ模块是DFRobot公司开发的一款基于ESP8266芯片的物联网通信模块.模块使用串口(TTL UART)和Arduino(或者其他单片机)通信,支持MQTT,HTT ...

  2. 玩转X-CTR100 l STM32F4 l ESP8266串口WIFI模块

    我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]- ESP8266是一款非常火的WIFI模块,性价 ...

  3. [原创]基于Zybo SDIO WiFi模块调试

    采用的是RTL8189 SDIO 模块,介绍如下 The Realtek RTL8189ES-VB-CG is a highly integrated single-chip 802.11n Wire ...

  4. ESP8266串口WiFi扩展板详解

    产品简介 ESP8266串口WiFi扩展板是深圳四博智联科技有限公司开发的一款基于乐鑫ESP8266的超低功耗的UART-WiFi模块,兼容Arduino UNO.Mega等标准主板,可以方便地进行二 ...

  5. STM32F10xx(高容量)WiFi模块的初始化和使用

    本次实验是使用每次传输不超过200B的ESP8266芯片的WiFi模块,WiFi模块内部自有驱动,我们初始化它,只需要发送指定的指令给他就可以了,指定的指令其实是使用USART3的复用的PB10和PB ...

  6. ARDUINO MEGA2560 经过ESP8266 WIFI模块上传温湿度数据到 OneNet 服务器

    简述 原来写了一个C++的wifi库但是发现用c++ arduino这小身板有点扛不住,代码比较大,使用String类型数据处理速度慢,而且很容易无缘无故跑飞.而且封装成库后使用还需要修改arduin ...

  7. ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于Wi-Fi模块(ESP8266)AT指令TCP透传方式,MQTT通信控制升级(加入数据校验)

    前言 这节演示下,上两节写的利用MQTT来控制STM32控制的程序 测试准备工作(默认访问我的服务器,改为自己的服务器,请看后面说明) 一,下载BootLoader程序(请自行下载) 首先BootLo ...

  8. WIFI模块ESP8266的使用指南【转】

    本文转载自:http://www.itdadao.com/articles/c15a814052p0.html 本文主要对讲述ESP8266模块硬件连接工作,以及作为服务器和客户端情况下的配置实现的详 ...

  9. 「雕爷学编程」Arduino动手做(33)——ESP-01S无线WIFI模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

随机推荐

  1. php 加入 unless 语法

    1. php 的版本 :PHP 7.3.0-dev (cli) (built: Mar 18 2018 00:28:55) ( NTS ) 2. unless 语法结构: unless($cond){ ...

  2. ndnsim安装遇到的一些问题

    我是安装的Ubuntu18.04+ndnsim2.7 由于最新版ndnsim的可视化与Python不兼容,出现了一些问题 1. No visualization support (cannot imp ...

  3. Python3+RobotFramework+pycharm环境搭建

    我的环境为 python3.6.5+pycharm 2019.1.3+robotframework3.1.2 1.安装python3.x 略 之后在cmd下执行:pip  install  robot ...

  4. LeetCode刷题总结-递归篇

    递归是算法学习中很基本也很常用的一种方法,但是对于初学者来说比较难以理解(PS:难点在于不断调用自身,产生多个返回值,理不清其返回值的具体顺序,以及最终的返回值到底是哪一个?).因此,本文将选择Lee ...

  5. 冷知识: 不会出现OutOfMemoryError的内存区域

    程序计数器(PC) 因为程序计数器只是记录当前线程正在执行的那条字节码指令的地址,即使出现死循环都不会内存溢出

  6. calendar类-时间处理类

    calendar类 calendar类是时间处理类 比如在scala中 //字符串转化日期格式 val df = new SimpleDateFormat("yyyy-MM-dd hh:mm ...

  7. Linux进程和线程

    一.进程产生的方式 1.描述进程的ID号通常叫做PID,即进程ID,PID的变量类型为pid_t. 2.getpid(void)返回当前进程的ID号,getppid(void)返回当前进程的父进程的I ...

  8. (五)Unity插件生成

    1)新建空的AndroidStudio工程,但是新建过程时最小SDK版本要与unity一致,如下图所示,本次操作均为api16 2)创建Library,如下图所示,新建module,然后选择Andro ...

  9. Java内存模型(JMM)详解

    在Java JVM系列文章中有朋友问为什么要JVM,Java虚拟机不是已经帮我们处理好了么?同样,学习Java内存模型也有同样的问题,为什么要学习Java内存模型.它们的答案是一致的:能够让我们更好的 ...

  10. Pandas 时间序列

    # 导入相关库 import numpy as np import pandas as pd 在做金融领域方面的分析时,经常会对时间进行一系列的处理.Pandas 内部自带了很多关于时间序列相关的工具 ...