之前冲动消费了几块ESP-01主板,原本考虑用于arduino芯片的网络化。最近梁总有需求远程开机,考虑给他用起。
EPS-01 在Arduino IDE中选择EPS-12,速率默认115200. 板载8个接线位分别是(板载天线正面,右方): TX (GPIO1) GND CH_PD GPIO2 GPIO16 GPIO0 VCC RX(GPIO3)
GPIO0 默认悬空,表示工作模式。下载程序时接地
GPIO16 低电平表示复位(默认已拉高) 类似RST
CH_PD 高电平工作,低电平则关闭供电
GPIO2 开机必须高电平(默认已拉高)
借用几张网上的图片
这是网友画的关于开机的连接
使用中发现,即使连接和设置没有问题,也经常会出现不能上传的情况,多试几次就可以了。通过手动的拉低GPIO16让芯片重启,往往能让其有效上传。
测试中的代码如下,暂时还没有解决手机端,考虑使用微信或者网页。等待继电器测试。
使用中发现WebOTA不成功,总是提示出错,不知道为什么。暂时放过。
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#define POWER_PIN 0
#define LED_PIN 2
const char* host = "ease";
const char* Ver = "0.3c";
const char* WebServer = "scwy.net";
const uint16_t port = 9090;
const uint16_t update_time = 10000;
bool led_status = false;
bool up_status = false;
uint32_t runTime = 0;
ESP8266WebServer server(80);
const char* serverIndex = "Ver:0.3c<br/><form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
// 闪烁LED
void ShowLed(uint16_t otime,uint16_t ftime){
digitalWrite(LED_PIN, LOW); delay(otime);
digitalWrite(LED_PIN, HIGH); delay(ftime);
}
// 网络服务,包括Web更新固件
void setup_WebServer() {
MDNS.begin(host);
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
delay(2000);
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
up_status = true;
Serial.setDebugOutput(true);
WiFiUDP::stopAll();
Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
});
server.begin();
MDNS.addService("http", "tcp", 80);
}
// 通过Web页,设置wifi
void steup_Wifi(){
WiFiManager wifiManager;
//wifiManager.setDebugOutput(false);
if (!wifiManager.autoConnect()) {
Serial.println("连接超时");
}
Serial.println("连接wifi成功");
}
// 通过WEB获取计算机机状态
void get_Command() {
WiFiClient client;
HTTPClient http;
if (http.begin(client, "http://scwy.net:9090/pc/get/1")) { // HTTP
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
if(payload=="1"){
digitalWrite(POWER_PIN, HIGH);
delay(200);
digitalWrite(POWER_PIN, LOW);
}
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
void setup() {
Serial.begin(115200);
uint8_t num = 0;
pinMode(POWER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.print("\n当前版本:");Serial.println(Ver);
steup_Wifi();
setup_WebServer(); // 连接了网络才会运行Web服务
Serial.printf("Ready! Open http://%s.local in your browser\n", host);
runTime = millis() + update_time;
}
void loop() {
server.handleClient();
if(up_status)return; //在更新固件时,不运行其它功能
MDNS.update();
// 状态灯
if(millis() % 500 == 0) {
if(led_status){
digitalWrite(LED_PIN, LOW);
} else {
digitalWrite(LED_PIN, HIGH);
}
led_status = not led_status;
}
//每x秒钟查询一次状态
if(runTime<millis()){
get_Command();
runTime = millis() + update_time;
}
delay(100);
}