今天是第九专题,主要介绍智能配网的方法途径和具体实现。在项目开发和调试阶段,设置 WIFI 连接信息,通常是在项目中修改源程序代码完成的。项目调试完成后,客户应用环境中如何实现WIFI连接信息(ssid 和 password)的配置呢?
一、AP模式智能配网
所谓AP模式智能配网,就是 IOT 设备启动后,会优先从闪存(FALSH)中查找以前保存的 wifi 连接信息尝试联网,如果联网成功则正常启动项目程序。如果没有查找到以前保存的 wifi 连接信息,或者虽然查到 wifi 连接信息但是无法联网成功,那么 IOT设备将自动配置生成一个 AP 热点,通过 PC或手机等其他智能连接到这个AP热点,借此将设备所在环境的路由器 SSID和Password发送到 IOT 设备。IOT 设备接收到 wifi 账号和密码之后,将从 AP 模式切换回 Station 模式,然后用过手机发送下来的 SSID 和 Password 连接所在环境的路由器,尝试实现联网功能。
二、添加智能配网 WiFiManager 功能库
添加库方法。打开 PlatformIO 界面,选择 Libraries 图标,在搜索栏内输入 WiFiManager,在查询结果中选择 WiFiManager by tzapu 库,添加到项目中。
三、官方示例使用代码说明
以下代码主要来自于该库示例程序 AutoConnect.ino ,增加了几行调试信息串口显示代码。请将其全部内容复制到任一新项目的 main.cpp 中,编译上传到开发板,尝试使用 PC 或手机等其他智能设备连接到 AP 热点(“AutoConnectAP”),对 wifi 连接信息进行配置。
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
// needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
// WiFiManager
// Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
// reset saved settings
// wifiManager.resetSettings();
// set custom ip for portal
// wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
// fetches ssid and pass from eeprom and tries to connect
// if it does not connect it starts an access point with the specified name
// here "AutoConnectAP"
// and goes into a blocking loop awaiting configuration
wifiManager.autoConnect("AutoConnectAP");
// or use this for auto generated name ESP + ChipID
// wifiManager.autoConnect();
// if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
// WiFi连接成功后将通过串口监视器输出连接成功信息
Serial.println("");
Serial.print("ESP8266 Connected to ");
Serial.println(WiFi.SSID()); // WiFi名称
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // IP
}
void loop()
{
// put your main code here, to run repeatedly:
}
四、为天气时钟项目增加智能配网功能
为项目增加智能配网功能非常简单方便,仅需要几行代码就可以实现。
(1)添加 WiFiManager 功能库,详见上面第二部分内容。
(2)在 main.cpp 包含必要的头文件,即以下 3 个头文件 。
// needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
(3)在 setup{ } 函数中声明一个 WiFiManager 实例,运行 autoConnect 函数。
// 自动配网功能的实现代码
WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
(4)(可选)增加调试信息,在串口监视器输出连接成功后的信息。
// WiFi连接成功后将通过串口监视器输出连接成功信息
Serial.println("");
Serial.print("ESP8266 Connected to ");
Serial.println(WiFi.SSID()); // WiFi名称
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // IP
五、增加智能配网功能后的 main.cpp
#include <Arduino.h>
// 连接wifi用的库
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
// needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
// TFT 屏幕驱动库
#include <TFT_eSPI.h>
// 解析JSON数据用的库
#include <ArduinoJson.h>
// 解压GZIP的JSON数据用的库
#include <ArduinoUZlib.h>
// 构造函数,实例化 TFT 屏幕对象
TFT_eSPI tft = TFT_eSPI();
// 程序用到的字库文件,后面会详细说明
#include "hefeng-min-40px.h"
#include "weather_font20.h"
#include "weather_font16.h"
// 网络时钟的刷新频率
unsigned long last_ntp = 0;
const long interval_ntp = 1000; // 网络时钟的刷新频率(毫秒)
// 今日天气的刷新频率
unsigned long last_weather = 0;
const long interval_weather = 1000 * 60 * 5; // 今日天气的刷新频率(毫秒),每300秒更新一次
// 明日天气的刷新频率
unsigned long last_tmr_weather = 0;
const long interval_tmr_weather = 1000 * 60 * 60; // 明天天气的刷新频率(毫秒),每3600秒更新一次
// 温湿度传感器的刷新频率
unsigned long last_sht = 0;
const long interval_sht = 3000; // 温湿度传感器的刷新频率(毫秒),每3000毫秒更新一次
// 来自和风天气的密钥
const String key = "aebf03d0af********8b0ed0ca5";
// 您的城市ID
const String cityid = "101120110";
#include "sht30.h"
#include "ntptime.h"
#include "weather_hefeng.h"
void setup()
{
Serial.begin(115200);
tft.init();
tft.setSwapBytes(true);
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.setTextSize(2);
// 自动配网功能的实现代码
WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
// WiFi连接成功后将通过串口监视器输出连接成功信息
Serial.println("");
Serial.print("ESP8266 Connected to ");
Serial.println(WiFi.SSID()); // WiFi名称
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // IP
// 设定屏幕的基本
tft.println("");
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.println("");
// 今日天气
tft.println("Get Today weather");
get_now_Weather();
get_tmr_Weather();
tft.setTextSize(1);
tft.println("");
tft.setTextSize(2);
// 对时
tft.println("Get NTP Time");
initNtp();
// 温湿度传感器
tft.println("");
tft.println("load Sensor Data..");
sht30_setup();
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.println("");
tft.println("start...");
delay(500);
tft.fillScreen(TFT_BLACK);
}
void loop()
{
// 获取单片机启动至今的毫秒数
unsigned long currentMillis = millis();
// 显示当前日期,星期几,农历
// update ntp 时间
if (last_ntp == 0 || currentMillis - last_ntp >= interval_ntp)
{
last_ntp = currentMillis;
loopNtp();
tft.loadFont(weather_font16);
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.drawString(dt.localDate + " " + weekOfDate1(dt.year, dt.month, dt.day) + " " + outputLunarDate(dt.year, dt.month, dt.day), 0, 0);
tft.unloadFont();
tft.setTextSize(5);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
tft.drawString(dt.localTime, 0, 30);
}
// 今日天气
if (last_weather == 0 || currentMillis - last_weather >= interval_weather)
{
if (last_weather > 0)
{
get_now_Weather();
}
last_weather = currentMillis;
// 擦除指定区域
tft.fillRect(55, 90, 240, 40, TFT_BLACK);
tft.setTextColor(TFT_YELLOW, TFT_BLACK, true);
tft.loadFont(hefeng40);
tft.drawString(icon(wd.now_icon), 10, 90);
tft.unloadFont();
tft.loadFont(weather_font20);
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.drawString(String(wd.now_temp) + "° " + wd.now_text, 55, 90);
tft.drawString(wd.now_windDir + "" + String(wd.now_windScale) + "级 " + wd.now_windSpeed + "KM/H", 55, 110);
tft.drawLine(0, 140, 240, 140, TFT_WHITE);
}
// 明日天气
if (last_tmr_weather == 0 || currentMillis - last_tmr_weather > interval_tmr_weather)
{
if (last_tmr_weather > 0)
{
get_tmr_Weather();
}
last_tmr_weather = currentMillis;
// 擦除指定区域
tft.fillRect(55, 150, 240, 40, TFT_BLACK);
tft.loadFont(hefeng40);
tft.setTextColor(TFT_YELLOW, TFT_BLACK, true);
tft.drawString(icon(wtd[1].iconDay), 10, 150);
tft.unloadFont();
tft.loadFont(weather_font20);
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.drawString("明天 " + String(wtd[1].tempMin) + "° - " + String(wtd[1].tempMax) + "°", 55, 150);
tft.drawString(wtd[1].textDay + ", " + "风力" + wtd[1].windScaleDay + "级", 55, 170);
// 这条线其实没必要重新绘制
tft.drawLine(0, 200, 240, 200, TFT_WHITE);
}
// 温湿度传感器的数据
if (last_sht == 0 || currentMillis - last_sht > interval_sht)
{
last_sht = currentMillis;
sht30();
tft.loadFont(weather_font20);
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.drawString("室温:", 0, 210);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
tft.drawString(String(sht_data.temperature) + "C ", 40, 210);
tft.setTextColor(TFT_WHITE, TFT_BLACK, true);
tft.drawString("湿度", 120, 210);
tft.setTextColor(TFT_GREEN, TFT_BLACK, true);
tft.drawString(String(sht_data.humidity) + "% ", 170, 210);
}
}
六、项目源代码下载
百度网盘下载链接:WeatherClock_example_9, 提取码:rvks
友情提示:请务必将 const String key 修改成您自己申请的和风天气API密钥。
如您需要了解其它专题的内容,请点击下面的链接。
第一专题内容,请参考:连接点亮SPI-TFT屏幕和UI布局设计
第二专题内容,请参考:WIFI模式设置及连接
第三专题内容,请参考:连接SHT30传感器,获取并显示当前环境温湿度数据(I2C)
第四专题内容,请参考:通过NTPClient库获取实时网络时间并显示在TFT屏幕上
第五专题内容,请参考:获取关于城市天气实况和天气预报的JSON信息(心知天气版)
第六专题内容,请参考:解析天气信息JSON数据并显示在 TFT 屏幕上(心知天气版)
第七专题内容,请参考:和风天气API返回JSON数据信息的解压缩实现
第八专题内容,请参考:解析和风天气信息压缩JSON数据并显示在 TFT 屏幕上