[C++] 纯文本查看 复制代码
/*
ESP8266 Sleep Mode test
Use ammeter to monitor current values:
1.when use the ESP8266 module connect the network,and get data from the html web;
2.when use the ESP8266 module into the deep-sleep mode;
test module : NodeMCU v1
test web adress : http://www.yfrobot.com/test/testNetwork.html
http://www.yfrobot.com
5-20-2017
*/
// Include the ESP8266 WiFi library. (Works a lot like the Arduino WiFi library.)
#include <ESP8266WiFi.h>
//////////////////////// WiFi Definitions ////////////////////////
const char WiFiSSID[] = "YFROBOT";
const char WiFiPSK[] = "yfrobot2016";
/////////////////////// Pin Definitions ///////////////////////
const int LED_PIN = 16; // Thing's onboard, green LED
////////////////// require address //////////////////
const char YFHost[] = "www.yfrobot.com";
//
// Time to sleep (in seconds):
const int sleepTimeS = 15;
void setup()
{
initHardware();
connectWiFi();
digitalWrite(LED_PIN, HIGH);
while (postToHTML() != 1)
{
delay(100);
}
digitalWrite(LED_PIN, LOW);
//
// WAKE_RF_DEFAULT : do or not do the radio calibration depending on the init byte 108. 做或不做无线电校准依赖于init字节108。
// WAKE_RFCAL : do the radio calibration every time.每次都要进行无线电校准。
// WAKE_NO_RFCAL : do NOT the radio calibration on wake up.不要在醒来时使用无线电校准。
// WAKE_RF_DISABLED : on wake up DISABLE the modem. So for example I can't connect the esp to wifi.在醒来时禁用调制解调器。例如,我无法将esp连接到wifi。
// deepSleep time is defined in microseconds. Multiply seconds by 1e6
ESP.deepSleep(sleepTimeS * 1000000); // 默认模式 WAKE_RFCAL
}
void loop()
{
}
void connectWiFi()
{
byte ledStatus = LOW;
// Set WiFi mode to station (as opposed to AP or AP_STA)
WiFi.mode(WIFI_STA);
// WiFI.begin([ssid], [passkey]) initiates a WiFI connection
// to the stated [ssid], using the [passkey] as a WPA, WPA2,
// or WEP passphrase.
WiFi.begin(WiFiSSID, WiFiPSK);
// Use the WiFi.status() function to check if the ESP8266 is connected to a WiFi network.
while (WiFi.status() != WL_CONNECTED)
{
// Blink the LED
digitalWrite(LED_PIN, ledStatus); // Write LED high/low
ledStatus = (ledStatus == HIGH) ? LOW : HIGH;
// Delays allow the ESP8266 to perform critical tasks
// defined outside of the sketch. These tasks include
// setting up, and maintaining, a WiFi connection.
delay(100);
// Potentially infinite loops are generally dangerous.
// Add delays -- allowing the processor to perform other
// tasks -- wherever possible.
}
Serial.println("WiFi connected");
Serial.println("");
}
void initHardware()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Don't need to set ANALOG_PIN as input, that's all it can be.
}
int postToHTML()
{
// LED turns on when we enter, it'll go off when we
// successfully post.
digitalWrite(LED_PIN, HIGH);
// Now connect to data.sparkfun.com, and post our data:
WiFiClient client;
const int httpPort = 80;
if (!client.connect(YFHost, httpPort))
{
// If we fail to connect, return 0.
return 0;
}
String reurl = "/test/testNetwork.html"; // We now create a URI for the request
// If we successfully connected, print our Phant post:
// This will send the request to the server
client.print(String("GET ") + reurl + " HTTP/1.1\r\n" +
"Host: " + YFHost + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 2000) {
Serial.println(">>> Client Timeout !");
client.stop();
return 0;
}
}
String getWeb = "";
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
char e = client.read();
getWeb += e;
}
Serial.print(getWeb);
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
return 1; // Return success
}