Arduino for ESP32Web配网以及硬件信息获取并打印输出
Posted perseverance52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino for ESP32Web配网以及硬件信息获取并打印输出相关的知识,希望对你有一定的参考价值。
Arduino for ESP32Web配网以及硬件信息获取并打印输出
-
本次采用的开发板:esp32
* -
开发板型号:
-
依赖库文件路径:C:\\Users\\Administrator\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.6\\cores\\esp32
-
库ESP.h
-
esp32硬件信息打印输出实现函数
uint32_t ideSize = ESP.getFlashChipSize();
Serial.printf("\\n Flash ide size: %u bytes\\n\\n", ideSize);//Flash ide size: 4194304 bytes
FlashMode_t ideMode = ESP.getFlashChipMode();
delay(5);
Serial.printf("getCPUCores: %d Core\\n\\n", ESP.getChipCores());//getCPUCores: 2 Core
delay(5);
Serial.printf("getCpuFreqMHz: %d MHz\\n\\n", ESP.getCpuFreqMHz()); //getCpuFreqMHz: 240 MHz
delay(5);
Serial.printf("SketchSize: %d bytes\\n\\n" , ESP.getSketchSize()); //SketchSize: 772528 bytes
delay(5);
Serial.printf("getFreeSketchSpace: %d bytes\\n\\n" , ESP.getFreeSketchSpace()); //getFreeSketchSpace: 1310720 bytes
delay(5);
Serial.printf("getSketchMD5: %s \\n\\n" , ESP.getSketchMD5().c_str()); //getSketchMD5: eb3794f8571687536bb6e416edd2b28b
Serial.printf("Flash ide speed: %u Hz\\n", ESP.getFlashChipSpeed());//Flash ide speed: 80000000 Hz
delay(5);
Serial.printf("Flash ide mode: %s\\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN"));
//Flash ide mode: DIO
esp32配网以及硬件信息打印和获取时间戳
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>//用于设备域名 MDNS.begin("esp32")
#include <HTTPClient.h>
#include <esp_wifi.h> //用于esp_wifi_restore() 删除保存的wifi信息
#ifndef STASSID
#define STASSID "ESP32"
#define STAPSK "12345678"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
String Time_api = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp";
WebServer server(80);
bool LED_Flag = false;
String str =
"<!DOCTYPE html><html><head><meta charset=\\"UTF-8\\"><meta name=\\"viewport\\"content=\\"width=device-width, initial-scale=1.0\\"><meta http-equiv=\\"X-UA-Compatible\\"content=\\"ie=edge\\"><title>ESP32网页配网</title></head><body><form name=\\"my\\">WiFi名称:<input type=\\"text\\"name=\\"s\\"placeholder=\\"请输入您WiFi的名称\\"id=\\"aa\\"><br>WiFi密码:<input type=\\"text\\"name=\\"p\\"placeholder=\\"请输入您WiFi的密码\\"id=\\"bb\\"><br><input type=\\"button\\"value=\\"连接\\"οnclick=\\"wifi()\\"></form><script language=\\"javascript\\">function wifi(){var ssid=my.s.value;var password=bb.value;var xmlhttp=new XMLHttpRequest();xmlhttp.open(\\"GET\\",\\"/HandleVal?ssid=\\"+ssid+\\"&password=\\"+password,true);xmlhttp.send()}</script></body></html>";
/*****************************************************
* 函数名称:handleRoot()
* 函数说明:客户端请求回调函数
* 参数说明:无
******************************************************/
void handleRoot() {
server.send(200, "text/html", str);
}
/*****************************************************
函数名称:HandleVal()
函数说明:对客户端请求返回值处理
参数说明:无
******************************************************/
void HandleVal()
{
String wifis = server.arg("ssid"); //从JavaScript发送的数据中找ssid的值
String wifip = server.arg("password"); //从JavaScript发送的数据中找password的值
Serial.println(wifis); Serial.println(wifip);
WiFi.begin(wifis.c_str(), wifip.c_str());
}
/*****************************************************
函数名称:handleNotFound()
函数说明:响应失败函数
参数说明:无
******************************************************/
void handleNotFound() {
digitalWrite(2, 0);
String message = "File Not Found\\n\\n";
message += "URI: ";
message += server.uri();
message += "\\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\\nArguments: ";
message += server.args();
message += "\\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\\n";
}
server.send(404, "text/plain", message);
digitalWrite(2, 1);
}
/*****************************************************
函数名称:autoConfig()
函数说明:自动连接WiFi函数
参数说明:无
返回值说明:true:连接成功 false:连接失败
******************************************************/
bool autoConfig()
{
WiFi.mode(WIFI_STA);
WiFi.begin();
Serial.print("AutoConfig Waiting......");
for (int i = 0; i < 20; i++)
{
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("AutoConfig Success");
Serial.printf("SSID:%s\\r\\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\\r\\n", WiFi.psk().c_str());
WiFi.printDiag(Serial);
//IP地址
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
//子网掩码
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
//网关地址
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
//DNS
Serial.print("DNS 1: ");
Serial.println(WiFi.dnsIP(0));
Serial.print("DNS 2: ");
Serial.println(WiFi.dnsIP(1));
return true;
//break;
}
else
{
Serial.print(".");
LED_Flag = !LED_Flag;
if (LED_Flag)
digitalWrite(2, HIGH);
else
digitalWrite(2, LOW);
delay(500);
}
}
Serial.println("AutoConfig Faild!" );
return false;
//WiFi.printDiag(Serial);
}
/*****************************************************
函数名称:htmlConfig()
函数说明:web配置WiFi函数
参数说明:无
******************************************************/
void htmlConfig()
{
WiFi.mode(WIFI_AP_STA);//设置模式为AP+STA
digitalWrite(2, LOW);
WiFi.softAP(ssid, password);
Serial.println("AP设置完成");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/HandleVal", HTTP_GET, HandleVal);
server.onNotFound(handleNotFound);//请求失败回调函数
server.begin();//开启服务器
Serial.println("HTTP server started");
while (1)
{
server.handleClient();
//MDNS.update();
// MDNS.enableWorkstation();
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("HtmlConfig Success");
Serial.printf("SSID:%s\\r\\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\\r\\n", WiFi.psk().c_str());
Serial.println("HTML连接成功");
break;
}
}
}
void setup(void) {
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
Serial.begin(115200);
Serial.println(); delay(5);
uint32_t ideSize = ESP.getFlashChipSize();
Serial.printf("\\n Flash ide size: %u bytes\\n\\n", ideSize);//Flash ide size: 4194304 bytes
FlashMode_t ideMode = ESP.getFlashChipMode();
delay(5);
Serial.printf("getCPUCores: %d Core\\n\\n", ESP.getChipCores());//getCPUCores: 2 Core
delay(5);
Serial.printf("getCpuFreqMHz: %d MHz\\n\\n", ESP.getCpuFreqMHz()); //getCpuFreqMHz: 240 MHz
delay(5);
Serial.printf("SketchSize: %d bytes\\n\\n" , ESP.getSketchSize()); //SketchSize: 772528 bytes
delay(5);
Serial.printf("getFreeSketchSpace: %d bytes\\n\\n" , ESP.getFreeSketchSpace()); //getFreeSketchSpace: 1310720 bytes
delay(5);
Serial.printf("getSketchMD5: %s \\n\\n" , ESP.getSketchMD5().c_str()); //getSketchMD5: eb3794f8571687536bb6e416edd2b28b
Serial.printf("Flash ide speed: %u Hz\\n", ESP.getFlashChipSpeed());//Flash ide speed: 80000000 Hz
delay(5);
Serial.printf("Flash ide mode: %s\\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN"));
//Flash ide mode: DIO
bool wifiConfig = autoConfig();
if (wifiConfig == false)
htmlConfig();//HTML配网
// esp_wifi_restore();//删除WiFi信息
}
void loop(void) {
digitalWrite(2, HIGH);
delay(200);
digitalWrite(2, LOW);
delay(200);
long int Timestamp =getTimestamp(Time_api);
Serial.printf("%u \\n",Timestamp);
}
long int getTimestamp(String url)
{
String Time1 = "";
HTTPClient http;
http.begin(url);
// start connection and send HTTP headerFFF
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0)
{
// file found at server
if (httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
int pos = (payload.indexOf("data"));
Time1 = payload.substring(pos + 12, payload.length() - 6);
Serial.println(payload.substring(pos + 12, payload.length() - 6));
}
}
else
{
Serial.printf("[HTTP] GET... failed, error: %s\\n", http.errorToString(httpCode).c_str());
}
http.end();
return atoll(Time1.c_str());
}
- 串口打印信息:(程序有更改,新增时间戳,)
以上是关于Arduino for ESP32Web配网以及硬件信息获取并打印输出的主要内容,如果未能解决你的问题,请参考以下文章
Arduino ESP32SmartConfig配网+NTP时间获取