What Will I Learn?
让esp8266连接本地http服务器
获取实时时间制作时钟
Requirements
ESP8266 -12F
Arduino IDE (版本要大于1.6.7)
phpstudy
php获取时间脚本
Difficulty
Basic
Tutorial Contents
针对预先功能编写程序
- 引入esp8266的wifi库文件
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
- ESP8266WiFi.h:用于esp8266连接wifi并实现wifi操作功能的有关库文件
- WiFiClientSecure.h:用于esp8266连接服务器进行安全访问的库文件
- ESP8266HTTPClient.h:使esp8266支持连接http服务器
- 设置需要连接的wifi的账号密码
const char* ssid = "steemit";
const char* password = "cha0s0000";
- ssid:设置要连接的wif的账号
- password:设置要连接的wif的密码
- const char* ssid = "steemit";:在这里,两个变量都要用c字符串指针形式
设置http服务器的地址与端口
const char* host = "192.168.0.101";
const int port = 80;
- host:服务器的IP或者域名地址
- port:服务器开放的用于连接的端口,这里可以使443或者80,WiFiClientSecure.h库文件支持https链接方式,但是这里我们搭建了本地的服务器采用80端口就OK
- 初始化串口并打印相关配置至串口,方便用于调试
Serial.begin(115200);
Serial.print("connecting to ");
Serial.println(ssid);
- 让esp8266连接wifi
WiFi.begin(ssid, password);
- 判断是否连接wifi成功,若不成功,一直等待
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
- 初始化esp8266的客户端配置
HTTPClient http;
- client:调用ESP8266HTTPClient.h库文件进行连接服务器
- 配置访问的url
String url = "/time.php";
url:获取实时时间的php脚本所在位置
- 编写获取实时时间的php脚本
<?php
date_default_timezone_set("Asia/Shanghai");
echo date("h:i:sa");
?>
- date_default_timezone_set:设置时区为”亚洲/上海“
- date:按照"h:i:sa"格式获取时间
- 文件名保存为time.php,并放置于服务器根目录
- 让esp8266连接上http服务器
http.begin(host, port, url);
- 获取http请求的返回码,用于确定请求状态
int httpCode = http.GET();
- GET():GET请求,截取字符串并获取请求码
- 判断是否请求成功并获取实时时间
if(httpCode == 200) {
String payload = http.getString();
Serial.println(payload);
}
- httpCode:返回码为200,则证明get请求成功
- getString():获取请求成功之后,服务器返回的数据
- 整合程序
#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
2.选择对应的开发板,此步骤可以参照:**你的wifi我作主----DIY一个wifi杀手 **
3.上传程序
注意:上传程序的时候,需要按一下esp8266-12f模块的RST键
4.测试效果:
先访问本地浏览器测试本地服务器是否搭建成功
测试esp8266模式
Posted on Utopian.io - Rewarding Open Source Contributors
@cha0s0000, Like your contribution, upvote.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
amazing speed!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@cha0s0000, 其实我就是来点赞的,别管我哈!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @cha0s0000 I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit