#include <WiFi.h> //Wifi library
//#include "esp_wpa2.h" //wpa2 library for connections to Enterprise networks
//#include "time.h"
#include <esp_wifi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include "secrets.h"
const char* ntpServer = "pool.ntp.org";
const long gmtOffsetSec = 25200;
const int daylightOffsetSec = 0;
const String url = "http://www.httpbin.org/deflate";
struct tm timeInfo;
HTTPClient http;
int delayTime;
void connectWiFi();
void setClock();
void printLocalTime();
void calDelayTime ();
void setup() {
Serial.begin(115200);
delay(10);
connectWiFi();
setClock();
calDelayTime();
}
// ------------------------------------------------------------------------------
void loop() {
// if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network
// counter = 0; //reset counter
// Serial.println("Wifi is still connected with IP: ");
// Serial.println(WiFi.localIP()); //inform user about his IP address
// } else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
// WiFi.begin(ssid);
// }
// while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
// delay(500);
// Serial.print(".");
// counter++;
// if (counter >= 60) { //30 seconds timeout - reset board
// ESP.restart();
// }
// }
// Serial.print("Connecting to website: ");
// Serial.println(host);
// WiFiClient client;
// if (client.connect(host, 80)) {
// String url = "/rele/rele1.txt";
// client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP32\r\n" + "Connection: close\r\n\r\n");
// while (client.connected()) {
// String line = client.readStringUntil('\n');
// if (line == "\r") {
// break;
// }
// }
// String line = client.readStringUntil('\n');
// Serial.println(line);
// } else {
// Serial.println("Connection unsucessful");
// }
//delay(15000);
delay(5000);
printLocalTime();
calDelayTime();
// WiFiClientSecure *client = new WiFiClientSecure;
// //WiFiClientSecure client;
// if (client) {
// // client -> setCACert(rootCACertificate);
// client->setInsecure();
// //client.setInsecure();
// // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
// HTTPClient https;
// Serial.print("[HTTPS] begin...\n");
// if (https.begin(*client, "https://httpbin.org")) { // HTTPS
// Serial.print("[HTTPS] GET...\n");
// // start connection and send HTTP header
// int httpCode = https.GET();
// // httpCode will be negative on error
// if (httpCode > 0) {
// // HTTP header has been send and Server response header has been handled
// Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// // file found at server
// if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
// String payload = https.getString();
// Serial.println(payload);
// }
// } else {
// Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
// }
// https.end();
// } else {
// Serial.printf("[HTTPS] Unable to connect\n");
// }
// }
// http.begin(url);
// int httpResponseCode = http.GET();
// if (httpResponseCode > 0) {
// Serial.print("HTTP ");
// Serial.println(httpResponseCode);
// String payload = http.getString();
// Serial.println();
// Serial.println(payload);
// }
// else {
// Serial.print("Error code: ");
// Serial.println(httpResponseCode);
// Serial.println(":-(");
// }
// http.end();
}
// ------------------------------------------------------------------------------
void connectWiFi() {
int counter = 0;
uint8_t newMACAddress[] = {MAC_ADDR_1, MAC_ADDR_2, MAC_ADDR_3, MAC_ADDR_4, MAC_ADDR_5, MAC_ADDR_6};
Serial.println();
Serial.print("Connecting to network: ");
Serial.println(SSID);
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
WiFi.mode(WIFI_STA); //init wifi mode
// Serial.print("old hostname: ");
// Serial.println(WiFi.getHostname());
// Serial.print("olc mac address: ");
// Serial.println(WiFi.macAddress());
WiFi.setHostname(HOSTNAME);
esp_wifi_set_mac(WIFI_IF_STA, newMACAddress);
Serial.print("Hostname: ");
Serial.print(WiFi.getHostname());
Serial.print(" MAC Address: ");
Serial.println(WiFi.macAddress());
WiFi.begin(SSID, WIFI_PASSWORD);
//WiFi.begin(SSID, WPA2_AUTH_PEAP, WIFI_IDENTITY, WIFI_USERNAME, WIFI_PASSWORD); //WPA2 Enterprise no cert
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
counter++;
if (counter >= 120) { //after 60 seconds timeout - reset board
ESP.restart();
}
}
Serial.println("");
Serial.print("WiFi connected IP address : ");
Serial.println(WiFi.localIP()); //print LAN IP
}
// ------------------------------------------------------------------------------
void setClock() {
int counter = 0;
configTime(gmtOffsetSec, daylightOffsetSec, ntpServer);
Serial.print("syc NTP time ");
while (!getLocalTime(&timeInfo)) {
delay(500);
Serial.print(".");
counter++;
if (counter >= 60) { //after 30 seconds timeout - reset board
ESP.restart();
}
}
Serial.println("");
printLocalTime();
}
// ------------------------------------------------------------------------------
void printLocalTime() {
if (!getLocalTime(&timeInfo)) {
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeInfo, "%A, %d %B %Y %H:%M:%S");
}
// ------------------------------------------------------------------------------
void calDelayTime () {
int intHour;
int calToMin;
int intWkDay;
int timeDelay;
while (!getLocalTime(&timeInfo)) {
delay(500);
}
// intHour = timeInfo.tm_hour;
// intMin = timeInfo.tm_min;
// intWkDay = timeInfo.tm_wday;
//hour 0-23 min 0-59 wday 0-6
//morning 7.15 (435) noon 12.15 (735) afternoon 16.45 (1005) 23.50 (1439)
if (timeInfo.tm_wday != 0 && timeInfo.tm_wday != 7) {
calToMin = (timeInfo.tm_hour * 60) + timeInfo.tm_min;
Serial.println(calToMin);
if (calToMin < 435) {
timeDelay = (435 - calToMin);
}
else if (calToMin < 735) {
Serial.println(calToMin);
timeDelay = (735 - calToMin);
}
else if (calToMin < 1005) {
timeDelay = (1005 - calToMin);
Serial.println(timeDelay);
}
else {
timeDelay = ((1440 - calToMin) + 345);
Serial.println(timeDelay);
}
}
Serial.println((timeDelay + random(5)) * 60 * 1000);
}
Loading
wemos-s2-mini
wemos-s2-mini