TA的每日心情 擦汗 2017-9-19 13:29
签到天数: 88 天
[LV.6]常住居民II
本帖最后由 敢问路在何方 于 2016-7-1 21:34 编辑
要使用软串的话,要将ESP8266.H里的“//#define ESP8266_USE_SOFTWARE_SERIAL”,改为“#define ESP8266_USE_SOFTWARE_SERIAL”,就是去掉那两根斜杠。
//互联网时间获取
#include "ESP8266.h"
#include <SoftwareSerial.h>
#define SSID "这里输入你家的wifi名称"
#define PASSWORD "你家wifi密码"
#define HOST_NAME "ntp.sjtu.edu.cn"//互联网授时服务器
#define HOST_PORT (123) //端口为123
//SoftwareSerial mySerial(10,11); // RX, TX,使用MEGA2560的软串
SoftwareSerial mySerial(2,3); // RX, TX,使用NANO的软串
ESP8266 wifi(mySerial,115200);//使用软串
//ESP8266 wifi(Serial1,115200);//使用MEGA2560的硬串1,19、18脚
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
int i;
void setup(void)
{
Serial.begin(115200);
Serial.print("setup begin\r\n");
Serial.print("FW Version: ");
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStationSoftAP()) {
Serial.print("to station + softap ok\r\n");
} else {
Serial.print("to station + softap err\r\n");
}
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print("Join AP success\r\n");
Serial.print("IP: ");
Serial.println(wifi.getLocalIP().c_str());
} else {
Serial.print("Join AP failure\r\n");
}
if (wifi.disableMUX()) {
Serial.print("single ok\r\n");
} else {
Serial.print("single err\r\n");
}
Serial.print("setup end\r\n");
}
void loop(void)
{
// send an NTP request to the time server at the given address
//unsigned long sendNTPpacket()
// set all bytes in the buffer to 0
uint8_t packetBuffer[NTP_PACKET_SIZE] = {0};
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
//Serial.println("2");
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
if (wifi.registerUDP(HOST_NAME,HOST_PORT)) {
Serial.print("register udp ok\r\n");
} else {
Serial.print("register udp err\r\n");
}
wifi.send((const uint8_t*)packetBuffer, NTP_PACKET_SIZE);
uint32_t len = wifi.recv(packetBuffer, NTP_PACKET_SIZE, 10000);
if (len > 0) {
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears + 8 * 3600;
// print Unix time:
Serial.println(epoch);
// print the hour, minute and second:
Serial.print("The Beijing time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600 ); // print the hour (86400 equals secs per day)
Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) {
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ( (epoch % 60) < 10 ) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch % 60); // print the second
}
else {
Serial.println("received failure");
}
if (wifi.unregisterUDP()) {
Serial.print("unregister udp ok\r\n");
} else {
Serial.print("unregister udp err\r\n");
}
delay(5000);
}
复制代码