成功了!用WeeEsp8266库实现获取NTP时间
本帖最后由 敢问路在何方 于 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 = {0};
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
//Serial.println("2");
packetBuffer = 0b11100011; // LI, Version, Mode
packetBuffer = 0; // Stratum, or type of clock
packetBuffer = 6; // Polling Interval
packetBuffer = 0xEC;// Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer= 49;
packetBuffer= 0x4E;
packetBuffer= 49;
packetBuffer= 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, packetBuffer);
unsigned long lowWord = word(packetBuffer, packetBuffer);
// 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);
}
楼主,用的什么库呢 本帖最后由 yuqingshan 于 2016-7-1 15:18 编辑
楼主啊,能共享您的ESP8266.h库文件吗?谢谢网上下载:ITEADLIB_Arduino_ESP8266-master 的库文件,改名为:ESP8266,但无法通过编译的!
本帖最后由 敢问路在何方 于 2016-7-1 21:24 编辑
yuqingshan 发表于 2016-7-1 15:02
楼主啊,能共享您的ESP8266.h库文件吗?谢谢网上下载:ITEADLIB_Arduino_ESP8266-master 的库文件,改名为 ...
没问题。不过,92行代码里有一个错误,应该将“+8”删掉,在87行加“+8*3600”。如果不清楚,再下一次我的程序,已经改正了。
yuqingshan 发表于 2016-7-1 15:02
楼主啊,能共享您的ESP8266.h库文件吗?谢谢网上下载:ITEADLIB_Arduino_ESP8266-master 的库文件,改名为 ...
11和17行的波特率要改为115200 以前我也想知道怎么弄的, zhangke0504 发表于 2016-7-1 22:27
以前我也想知道怎么弄的,
请问ESP8266是怎么连接网络的,里面没写程序和设置,是不是直接把程序写到ARDUINO里,接上ESO8266程序会自动写到ESP8266里呢?不很懂,请指点下,谢谢。
setup begin
FW Version:
to station + softap err
Join AP failure
single err
setup end
请问ESP8266是怎么连接网络的,里面没写程序和设置,是不是直接把程序写到ARDUINO里,接上ESO8266程序会自动写到ESP8266里呢?不很懂,请指点下,谢谢。 setup begin FW Version:to station + softap err Join AP failure single err setup end 本帖最后由 敢问路在何方 于 2016-7-4 20:32 编辑
Peter58 发表于 2016-7-3 21:58
请问ESP8266是怎么连接网络的,里面没写程序和设置,是不是直接把程序写到ARDUINO里,接上ESO8266程序会自 ...
第23行就是设置Wi-Fi的工作模式,第29行就是加入到你家的Wi-Fi网络。是这样的,程序是写到arduino面,而arduino是通过调用库文件直接发指令给ESP 8266模块执行网络的连接、设置等。出现你所列的信息,有可能是因为模块没有激活,所以设置成功后返回的不是OK,程序没有检测到OK,出现err,但实际上它已经设置正确了。你让程序重新运行就会一路OK。另外一种情况是你Rx和Tx没接对,D2接ESP8266的TXD,D3接ESP8266的RXD。
谢谢回复,我用的是ESP28266-12的模块