使用stm32和esp8266同步时间的代码
时间: 2023-12-28 09:05:12 浏览: 114
stm32+esp8266+阿里云+温湿度+光照-烟雾-远程显示+控制-之STM32代码
5星 · 资源好评率100%
您可以参考下面的代码:
```
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
IPAddress timeServerIP;
const char* ntpServerName = "cn.pool.ntp.org";
const int timeZone = 8;
WiFiUDP Udp;
unsigned int localPort = 8888;
const int NTP_PACKET_SIZE = 48;
byte packetBuffer[NTP_PACKET_SIZE];
SoftwareSerial esp8266(2, 3);
void setup() {
Serial.begin(9600);
esp8266.begin(9600);
WiFi.begin("your_SSID", "your_PASSWORD");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Udp.begin(localPort);
Serial.println("UDP started");
}
void loop() {
sendNTPpacket(timeServerIP);
delay(1000);
if (esp8266.available())
Serial.write(esp8266.read());
if (Serial.available())
esp8266.write(Serial.read());
if (Udp.parsePacket()) {
Serial.println("Packet received");
Udp.read(packetBuffer, NTP_PACKET_SIZE);
unsigned long secsSince1900;
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
unsigned long epoch = secsSince1900 - 2208988800UL + timeZone * 3600;
setTime(epoch);
Serial.println(epoch);
String date = String(day()) + "/" + month() + "/" + year();
String time = String(hour()) + ":" + minute() + ":" + second();
String datetime = date + " " + time;
Serial.println(datetime);
esp8266.println("AT+CIPSTART=\"TCP\",\"your_server_IP\",your_server_port");
Serial.println("Trying to connect to server");
while(!esp8266.find("OK")) {
delay(1000);
Serial.write(".");
}
Serial.println("");
Serial.println("Connected to server");
String postRequest = "POST /your_action HTTP/1.1\r\n";
postRequest += "Host: your_server_IP:your_server_port\r\n";
postRequest += "Content-Type: application/x-www-form-urlencoded\r\n";
postRequest += "Content-Length: whatever your_content_length_is\r\n";
postRequest += "\r\n";
postRequest += "datetime=" + datetime;
esp8266.println("AT+CIPSEND=" + String(postRequest.length()));
if (esp8266.find(">")) {
esp8266.print(postRequest);
}
delay(5000);
while (esp8266.available()) {
String message = esp8266.readStringUntil('\n');
Serial.println(message);
}
esp8266.println("AT+CIPCLOSE");
Serial.println("Connection closed");
delay(10000);
}
}
void sendNTPpacket(IPAddress& address) {
memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011;
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
Udp.beginPacket(address, 123);
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
```
其中,你需要填写以下信息:
- your_SSID:Wi-Fi 名称
- your_PASSWORD:Wi-Fi 密码
- your_server_IP:服务器 IP 地址
- your_server_port:服务器端口号
- your_action:服务器端口的 Action
- your_content_length_is:需发送的数据的长度
此代码使用 ESP8266 发送 NTP 请求来同步时间,然后将时间发送给服务器。你需要将代码中的 your_server_IP,your_server_port,your_action 和 your_content_length_is 替换为您的服务器 IP 地址,服务器端口号,服务器端口的 Action 以及您要发送的数据的长度。
阅读全文