ESP8266 tutorials--连接http服务器并获取实时时间

in utopian-io •  7 years ago  (edited)

What Will I Learn?

  • 让esp8266连接本地http服务器

  • 获取实时时间制作时钟

GIF.gif

Requirements

  • ESP8266 -12F

    图片.png

  • Arduino IDE (版本要大于1.6.7)

    图片.png

  • ESP8266 library

  • phpstudy

    图片.png

  • php获取时间脚本

Difficulty

  • Basic

Tutorial Contents

针对预先功能编写程序

  1. 引入esp8266的wifi库文件
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
  • ESP8266WiFi.h:用于esp8266连接wifi并实现wifi操作功能的有关库文件
  • WiFiClientSecure.h:用于esp8266连接服务器进行安全访问的库文件
  • ESP8266HTTPClient.h:使esp8266支持连接http服务器

  1. 设置需要连接的wifi的账号密码
const char* ssid = "steemit";
const char* password = "cha0s0000";
  • ssid:设置要连接的wif的账号
  • password:设置要连接的wif的密码
  • const char* ssid = "steemit";:在这里,两个变量都要用c字符串指针形式

  1. 设置http服务器的地址与端口

    图片.png

const char* host = "192.168.0.101";
const int port = 80;
  • host:服务器的IP或者域名地址
  • port:服务器开放的用于连接的端口,这里可以使443或者80,WiFiClientSecure.h库文件支持https链接方式,但是这里我们搭建了本地的服务器采用80端口就OK

  1. 初始化串口并打印相关配置至串口,方便用于调试
 Serial.begin(115200);
 Serial.print("connecting to ");
 Serial.println(ssid);

  1. 让esp8266连接wifi
WiFi.begin(ssid, password);

  1. 判断是否连接wifi成功,若不成功,一直等待
 while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  1. 初始化esp8266的客户端配置
HTTPClient http;
  • client:调用ESP8266HTTPClient.h库文件进行连接服务器

  1. 配置访问的url
 String url = "/time.php";
  • url:获取实时时间的php脚本所在位置

    图片.png


  1. 编写获取实时时间的php脚本
<?php
  date_default_timezone_set("Asia/Shanghai");
  echo date("h:i:sa");
?>
  • date_default_timezone_set:设置时区为”亚洲/上海“
  • date:按照"h:i:sa"格式获取时间
  • 文件名保存为time.php,并放置于服务器根目录

  1. 让esp8266连接上http服务器
http.begin(host, port, url);

  1. 获取http请求的返回码,用于确定请求状态
int httpCode = http.GET();
  • GET():GET请求,截取字符串并获取请求码

  1. 判断是否请求成功并获取实时时间
if(httpCode == 200) {
      String payload = http.getString();
      Serial.println(payload);
  }
  • httpCode:返回码为200,则证明get请求成功
  • getString():获取请求成功之后,服务器返回的数据

  1. 整合程序
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "god";
const char* password = "zhangliuchen";
const char* host = "192.168.0.101";
const int port = 80;

HTTPClient http;
void setup() {
  Serial.begin(115200);
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  
  
}

void loop() {
  String url = "/time.php";
  http.begin(host, port, url);
  int httpCode = http.GET();
  if(httpCode == 200) {
      String payload = http.getString();
      Serial.println(payload);
  }
  delay(1000);
}



烧写上传到ESP8266模块

1.打开Arduino IDE

图片.png


2.选择对应的开发板,此步骤可以参照:**你的wifi我作主----DIY一个wifi杀手 **

图片.png


3.上传程序

注意:上传程序的时候,需要按一下esp8266-12f模块的RST键

图片.png

GIF.gif

图片.png


4.测试效果:

  • 先访问本地浏览器测试本地服务器是否搭建成功

    图片.png

  • 测试esp8266模式

    GIF.gif



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@cha0s0000, Like your contribution, upvote.

amazing speed!

@cha0s0000, 其实我就是来点赞的,别管我哈!

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @cha0s0000 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x