Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This sketch shows how to use WiFi event handlers.
- In this example, ESP8266 works in AP mode.
- Three event handlers are demonstrated:
- - station connects to the ESP8266 AP
- - station disconnects from the ESP8266 AP
- - ESP8266 AP receives a probe request from a station
- Written by Markus Sattler, 2015-12-29.
- Updated for new event handlers by Ivan Grokhotkov, 2017-02-02.
- This example is released into public domain,
- or, at your option, CC0 licensed.
- */
- #include <ESP8266WiFi.h>
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- #include <PolledTimeout.h>
- // Definizione del timezone e degli intervalli dell'ora legale
- #define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
- // struct dove memorizzare le informazioni realtive al timedate (giorno, ora, minuti, etc etc )
- struct tm timeinfo; // è definita in "time.h"
- // timer con frequenza di 1Hz
- static esp8266::polledTimeout::periodicMs showTimeNow(1000);
- #ifndef APSSID
- #define APSSID "xxxxxxx"
- #define APPSK "xxxxxxx"
- #endif
- const char* ssid = APSSID;
- const char* password = APPSK;
- WiFiEventHandler connectedHandler;
- WiFiEventHandler disconnectedHandler;
- WiFiEventHandler gotIpEventHandler;
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- // Register event handlers.
- // Callback functions will be called as long as these handler objects exist.
- connectedHandler = WiFi.onStationModeConnected(&onConnected);
- disconnectedHandler = WiFi.onStationModeDisconnected(&onDisconnected);
- gotIpEventHandler = WiFi.onStationModeGotIP(&onStationGotIp);
- }
- void onStationGotIp(const WiFiEventStationModeGotIP& evt) {
- Serial.print("Station ip address: ");
- Serial.println(WiFi.localIP());
- configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
- }
- void onConnected(const WiFiEventStationModeConnected& evt) {
- Serial.print("ESP connected: ");
- }
- void onDisconnected(const WiFiEventStationModeDisconnected& evt) {
- Serial.print("ESP disconnected: ");
- }
- void showTime() {
- time_t now = time(nullptr);
- timeinfo = *localtime(&now);
- char buffer [40];
- strftime (buffer,40,"%A, %D %T", &timeinfo);
- Serial.println(buffer);
- }
- void loop() {
- if (showTimeNow)
- showTime();
- }
Add Comment
Please, Sign In to add comment