|
楼主 |
发表于 2017-9-15 21:43
|
显示全部楼层
本帖最后由 敢问路在何方 于 2017-9-15 22:11 编辑
这是我重新检测过的程序:(在11行就定义了wifi这个对象了呀,应该不会出现你提的错误)
- //互联网NTP时间获取
- #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); //使用软串,要将\libraries\WeeESP8266\ESP8266.h里的//#define ESP8266_USE_SOFTWARE_SERIAL前面的“//”删掉
- //ESP8266 wifi(Serial1,115200); //使用MEGA2560的硬串1,19、18脚,要在\libraries\WeeESP8266\ESP8266.h里的#define ESP8266_USE_SOFTWARE_SERIAL前面加“//”
- const int NTP_PACKET_SIZE = 48; // NTP 时间戳在返回信息的前48字节里,40——43
- 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)
- {
- //将缓冲区里所有字节清零
- uint8_t packetBuffer[NTP_PACKET_SIZE] = {0};
- //准备NTP请求数据
- 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);//发送数据
- wifi.send(packetBuffer, NTP_PACKET_SIZE);//发送数据
- uint32_t len = wifi.recv(packetBuffer, NTP_PACKET_SIZE, 10000);
- if (len > 0) {//接收到数据
- //从接收包第40个字节开始的4个字为节时间戳,先分别赋值到2 word
- unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
- unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
- //合并4字节成长整型,这就是NTP时间(从1900.1.1开始的秒数)
- 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;
- // print Unix time:
- //Serial.println(epoch);
- //打印小时、分钟和秒:
- Serial.print("The Beijing time is "); // 北京时间,东八区
- Serial.print((epoch % 86400L) / 3600 + 8); // 打印小时 (每天86400秒)
- Serial.print(':');
- if ( ((epoch % 3600) / 60) < 10 ) {
- // 前10分钟加“0”
- Serial.print('0');
- }
- Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
- Serial.print(':');
- if ( (epoch % 60) < 10 ) {
- // 前10秒加“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);
- }
复制代码
|
|