zomertijd met ESP8266
3 berichten
• Pagina 1 van 1
zomertijd met ESP8266
Ik probeer met een ESP8266 automatisch de zomertijd goed te zetten.
cpp code
de volgende browser url https://maps.googleapis.com/maps/api/ti ... M2rY_e92X4
geeft als response:
{
"dstOffset" : 3600,
"rawOffset" : 3600,
"status" : "OK",
"timeZoneId" : "Europe/Amsterdam",
"timeZoneName" : "Central European Summer Time"
}
"dstOffset" : 3600 betekent dat de zomertijd geldt in Nederland. Dat wil ik vervolgens gebruiken om de UNIX tijd actueel te maken. Maar ik kom niet zover en komt geen verbinding tot stand:
WiFi connected
IP address:
192.168.1.67
connection failed, retrying...
Wat gaat er fout.
cpp code
#include <ESP8266WiFi.h>
#define USE_SSL 1
#if USE_SSL
# include <WiFiClientSecure.h>
WiFiClientSecure client;
# define PORT 443
#else
WiFiClient client;
# define PORT 80
#endif
#include <stdlib.h>
const char* ssid = "XXX";
const char* password = "XXXXXX";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
Serial.println(getDST());
delay(1000);
}
String getDST() {
WiFiClient client;
while (!!!client.connect("https://maps.googleapis.com/maps/api/timezone/json?location=51.643567,%205.946934×tamp=1490531639&key=AIzaSyCtC0BKgufYKsLMvouXrtr66M2rY_e92X4", 443)) {
Serial.println("connection failed, retrying...");
}
client.print("HEAD / HTTP/1.1\r\n\r\n");
while(!!!client.available()) {
yield();
}
while(client.available()){
if (client.read() == '\n') {
if (client.read() == 'd') {
if (client.read() == 's') {
if (client.read() == 't') {
if (client.read() == 'O') {
if (client.read() == 'f') {
if (client.read() == 'f') {
if (client.read() == 's') {
if (client.read() == 'e') {
if (client.read() == 't') {
client.read();
String DST = client.readStringUntil('\r');
client.stop();
return DST;
}
}
}
}
}
}
}
}
}
}
}
}
de volgende browser url https://maps.googleapis.com/maps/api/ti ... M2rY_e92X4
geeft als response:
{
"dstOffset" : 3600,
"rawOffset" : 3600,
"status" : "OK",
"timeZoneId" : "Europe/Amsterdam",
"timeZoneName" : "Central European Summer Time"
}
"dstOffset" : 3600 betekent dat de zomertijd geldt in Nederland. Dat wil ik vervolgens gebruiken om de UNIX tijd actueel te maken. Maar ik kom niet zover en komt geen verbinding tot stand:
WiFi connected
IP address:
192.168.1.67
connection failed, retrying...
Wat gaat er fout.
Advertisement
Re: zomertijd met ESP8266
Ik gebruik momenteel een andere methode om de tijd te syncen en Zomer/Winter tijd automatisch te laten verlopen.
(werkt hier redelijk probleemloos, met redelijk bedoel ik, het kan voorkomen dat er geen response komt, maar de volgende check na 12 uur weer wel)
Werkt op een Lolin NodeMCU 1.0 (ESP-12E Module) device
Met Wifi Manager en password protected
(werkt hier redelijk probleemloos, met redelijk bedoel ik, het kan voorkomen dat er geen response komt, maar de volgende check na 12 uur weer wel)
Werkt op een Lolin NodeMCU 1.0 (ESP-12E Module) device
Met Wifi Manager en password protected
- Code: Alles selecteren
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>
extern "C" {
#include "user_interface.h"
}
// not working with Wifimanager!!
char myhostname[] = "Machine12-A"; // Device name
ESP8266WebServer server(80);
String webPage = "";
int gpio2Led = 2; // D4 blue led on NodeMCU Lolin
int val = 0; // variable for reading the pin status
// NTP Servers:
IPAddress timeServer(216, 229, 0, 179); // time.nist.gov //time-a.timefreq.bldrdoc.gov
// Wintertime = 1 Summertime = 2
// start with Wintertime
int timeZone = 1; // Central European Time
WiFiUDP Udp;
unsigned int localPort = 2390; // local port to listen for UDP packets
//******************************************************************************************
//******************** VOID SETUP
//******************************************************************************************
void setup()
{
webPage ="<!DOCTYPE html>\n";
webPage +="<html>\n";
webPage +="<head>\n";
webPage +="<META name='viewport' content='width=device-width, initial-scale=1'>\n";
webPage +="<title>Machine 12</title>\n";
webPage +="</head>\n";
webPage +="<h1>Machine led On/Off</h1>\n";
webPage +="<p><a href=\"on\"><button type='button' style='height:50px; width:225px'>ON</button></a> <a href=\"off\"><button type='button' style='height:50px; width:225px'>OFF</button></a></p>\n";
webPage +="<br> 2017\n";
webPage +="</body>\n";
webPage +="</html>\n";
// preparing GPIOs
pinMode(gpio2Led, OUTPUT);
digitalWrite(gpio2Led, HIGH);
// END preparing GPIOs
Serial.begin(115200);
while (!Serial) ; // Needed for Leonardo only
delay(250);
Serial.println("Starting device...");
WiFiManager wifiManager;
/*
Password protect the configuration Access Point
You can and should password protect the configuration access point. Simply add the password as a second parameter to autoConnect.
A short password seems to have unpredictable results so use one that's around 8 characters or more in length.
The guidelines are that a wifi password must consist of 8 to 63 ASCII-encoded characters in the range of 32 to 126 (decimal)
*/
if (!wifiManager.autoConnect("AMachine12", "12345678")) {
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000);
ESP.restart();
delay(5000);
}
Serial.print("IP number assigned DHCP");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(Udp.localPort());
Serial.println("waiting for sync..");
delay(1000);
setSyncProvider(getNtpTime); // get the first time from NTP server
delay(5000); // wait 5 seconds
timeZone = adjustDstEurope(); // set time zone
Serial.print("UTC time + "); // just print the time
Serial.println(timeZone); // ""
setSyncProvider(getNtpTime); // print the new time with corrected timezone
setSyncInterval(43200); // set interval get time (in seconds) 43200 = 12 uur
server.on("/", [](){ // root of device
server.send(200, "text/html", webPage); // Go to Webpage
});
server.on("/on", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio2Led, LOW);
delay(1000);
});
server.on("/off", [](){
server.send(200, "text/html", webPage);
digitalWrite(gpio2Led, HIGH);
delay(1000);
});
server.on("/reset", [](){ // used for resetting the WifiManager, so you will get the Config portal
server.send(200, "text/html", webPage);
delay(1000);
WiFiManager wifiManager;
wifiManager.resetSettings();
delay(2000);
ESP.reset(); // better way to use restart
delay(3000);
});
server.begin();
Serial.println("HTTP server started");
}
//********************** END VOID SETUP ***************************************************
time_t prevDisplay = 0; // when the digital clock was displayed
//******************************************************************************************
//******************** VOID LOOP
//******************************************************************************************
void loop()
{
server.handleClient();
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if time has changed
prevDisplay = now();
digitalClockDisplay();
}
}
}
//********************** END VOID LOOP *****************************************************
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(".");
Serial.print(month());
Serial.print(".");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/*-------- NTP code Start here ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
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;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
// ------ set timezone -------
int adjustDstEurope()
{
// last sunday of march
int beginDSTDate= (31 - (5* year() /4 + 4) % 7);
int beginDSTMonth=3;
//last sunday of october
int endDSTDate= (31 - (5 * year() /4 + 1) % 7);
int endDSTMonth=10;
// DST is valid as:
if (((month() > beginDSTMonth) && (month() < endDSTMonth))
|| ((month() == beginDSTMonth) && (day() >= beginDSTDate))
|| ((month() == endDSTMonth) && (day() <= endDSTDate)))
return 2; // 7200 DST europe = utc +2 hour changed for own use Summertime
else return 1; // 3600 nonDST europe = utc +1 hour changed for own use Wintertime
}
Re: zomertijd met ESP8266
Dank je Peter,
Inmiddels had ik "mijn zomertijd" ook aan de praat en werkt nu ook stabiel. Zie andere topic. Eerlijk gezegd had ik gezocht naar "jouw oplossing" tevergeefs. Bij het googelen kwam ik toe de website oplossing tegen.
Groet
René
Inmiddels had ik "mijn zomertijd" ook aan de praat en werkt nu ook stabiel. Zie andere topic. Eerlijk gezegd had ik gezocht naar "jouw oplossing" tevergeefs. Bij het googelen kwam ik toe de website oplossing tegen.
Groet
René
3 berichten
• Pagina 1 van 1
Wie is er online?
Gebruikers in dit forum: Geen geregistreerde gebruikers en 17 gasten