forked from manitou48/teensy3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
espntp.ino
164 lines (138 loc) · 3.86 KB
/
espntp.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* Based on:
* ====== ESP8266 Demo ======
* (Updated Dec 14, 2014)
* Ray Wang @ Rayshobby LLC
* http://rayshobby.net/?p=9734
* ==========================
*
* Modified by R. Wozniak
* Compiled with Arduino 1.60 and Teensyduino 1.21b6
* ESP8266 Firmware: AT21SDK95-2015-01-24.bin
*
* Change SSID and PASS to match your WiFi settings.
* The IP address is displayed serial upon successful connection.
*/
#include <Time.h>
const int timeZone = -4; // Eastern DST (USA)
#define BUFFER_SIZE 1024
#define SSID "FIXME" // change this to match your WiFi SSID
#define PASS "FIXME" // change this to match your WiFi password
#define PORT "123"
#define NTPSERVER "192.168.1.4"
char buffer[BUFFER_SIZE],query[48];
byte binfind(char *str) {
unsigned long t=millis();
bool found=false;
int i=0,j=0, len = strlen(str);
char c;
while(millis()<t+5000) {
if(Serial1.available()) {
c=Serial1.read();
if (c == str[i]) {
i++;
if (i == len) return true;
} else i =0;
}
}
return found;
}
// By default we are looking for OK\r\n
char OKrn[] = "OK\r\n";
byte wait_for_esp_response(int timeout, char* term=OKrn) {
unsigned long t=millis();
bool found=false;
int i=0;
int len=strlen(term);
// wait for at most timeout milliseconds
// or if OK\r\n is found
while(millis()<t+timeout) {
if(Serial1.available()) {
buffer[i++]=Serial1.read();
if(i>=len) {
if(strncmp(buffer+i-len, term, len)==0) {
found=true;
break;
}
}
}
}
buffer[i]=0;
Serial.print(buffer);
return found;
}
void setup() {
// assume esp8266 operates at 115200 baud rate
// change if necessary to match your modules' baud rate
Serial1.begin(115200); // Teensy Hardware Serial port 1 (pins 0 and 1)
Serial.begin(115200); // Teensy USB Serial Port
delay(5000);
Serial.println("begin.");
setupWiFi();
// print device IP address
Serial.print("device ip addr: ");
Serial1.println("AT+CIFSR");
wait_for_esp_response(1000);
}
void loop() {
int ch_id, packet_len;
unsigned long t1, t;
char *pb;
// send UDP packet
Serial.println("UDP send");
while(Serial1.available()) Serial1.read(); // flush responses
t1=millis();
Serial1.print("AT+CIPSEND=4,");
Serial1.println(sizeof(query));
wait_for_esp_response(1000);
Serial1.write((uint8_t *)query,sizeof(query));
if (!binfind("+IPD,4,48:")){
Serial.println("IPD timeout");
delay(10000);
return;
}
t1= millis()-t1;
// parse out ntp reply +IPD,4,48:xxxxxxxxxxxxx
// ntp packet is network-byte order, big endian
Serial1.readBytes(buffer,sizeof(query));
unsigned long secsSince1900;
secsSince1900 = (unsigned long)buffer[40] << 24;
secsSince1900 |= (unsigned long)buffer[41] << 16;
secsSince1900 |= (unsigned long)buffer[42] << 8;
secsSince1900 |= (unsigned long)buffer[43];
t = secsSince1900 - 2208988800UL + timeZone * 3600;
Serial.print(t1); Serial.println(" ms");
Serial.println(t);
Serial.printf("%d/%d/%d %d:%d:%d\n",month(t),day(t),year(t),hour(t),minute(t),second(t));
wait_for_esp_response(1000);
delay(10000);
}
void setupWiFi() {
// turn on echo
Serial1.println("ATE1");
wait_for_esp_response(1000);
// set mode 1 (client)
Serial1.println("AT+CWMODE=3");
wait_for_esp_response(1000);
// reset WiFi module
Serial1.print("AT+RST\r\n");
wait_for_esp_response(1500);
//join AP
Serial1.print("AT+CWJAP=\"");
Serial1.print(SSID);
Serial1.print("\",\"");
Serial1.print(PASS);
Serial1.println("\"");
wait_for_esp_response(5000);
//Create UDP ports
Serial1.println("AT+CIPMUX=1");
wait_for_esp_response(1000);
String cmd = "AT+CIPSTART=4,\"UDP\",\"";
cmd += NTPSERVER;
cmd += "\",";
cmd += PORT;
cmd += ",4321,0";
Serial1.println(cmd);
wait_for_esp_response(1000);
query[0] = 0x1B; // NTP request
Serial.println("UDP ready");
}