/*
ESP32 WiFi STA Mode
http:://www.electronicwings.com
*/
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "esp_system.h"
// For example purpose
String formattedDate;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Timer conf
hw_timer_t * timer = NULL;
volatile SemaphoreHandle_t timerSemaphore;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
volatile uint32_t isrCounter = 0;
volatile uint32_t lastIsrAt = 0;
void ARDUINO_ISR_ATTR onTimer(){
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMux);
isrCounter = isrCounter + 1;
lastIsrAt = millis();
portEXIT_CRITICAL_ISR(&timerMux);
// Give a semaphore that we can check in the loop
xSemaphoreGiveFromISR(timerSemaphore, NULL);
// It is safe to use digitalRead/Write here if you want to toggle an output
}
// Wifi conf
const char* ssid = "Wokwi-GUEST"; /*Enter Your SSID*/
const char* password = ""; /*Enter Your Password*/
WiFiServer server(80); /* Instance of WiFiServer with port number 80 */
String request;
WiFiClient client;
void callback() {
Serial.print("Callback... ");
}
void setup() {
Serial.begin(9600);
/*
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.print("\n");
Serial.print("Connected to Wi-Fi ");
Serial.println(WiFi.SSID());
delay(1000);
server.begin();
Serial.print("Connect to IP Address: ");
Serial.print("http://");
Serial.println(WiFi.localIP());
/* Start the HTTP web Server */
// Create semaphore to inform us when the timer has fired
timerSemaphore = xSemaphoreCreateBinary();
// Set timer frequency to 1Mhz
timer = timerBegin(1000000, 80, true);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onTimer, true);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timer, 1000000, true, 0);
}
void loop() {
formattedDate = timeClient.getFormattedTime();
Serial.println(formattedDate);
// If Timer has fired
if (xSemaphoreTake(timerSemaphore, 0) == pdTRUE){
uint32_t isrCount = 0, isrTime = 0;
// Read the interrupt count and time
portENTER_CRITICAL(&timerMux);
isrCount = isrCounter;
isrTime = lastIsrAt;
portEXIT_CRITICAL(&timerMux);
// Print it
Serial.print("onTimer no. ");
Serial.print(isrCount);
Serial.print(" at ");
Serial.print(isrTime);
Serial.println(" ms");
}
// If button is pressed
if (digitalRead(BTN_STOP_ALARM) == LOW) {
// If timer is still running
if (timer) {
// Stop and free timer
timerEnd(timer);
timer = NULL;
}
}
/*
// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
Serial.print("DATE: ");
Serial.println(dayStamp);
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("HOUR: ");
Serial.println(timeStamp);
*/
delay(1000);
/*
client = server.available();
if (!client) {
return;
}
while (client.connected()) {
if (client.available()) {
char c = client.read();
request += c;
if (c == '\n') {
String postres = client.readString();
int indp = postres.indexOf("progo=")+6;
Serial.println(postres);
Serial.println(request);
if (request.indexOf("POST /PROG") != -1) {
Serial.println("go");
programme =postres.substring(indp);
}
if (request.indexOf("GET /L1") != -1) {
L1 = !L1;
}
if (request.indexOf("GET /L2") != -1) {
L2 = !L2;
}
if (request.indexOf("GET /L3") != -1) {
L3 = !L3;
}
if (request.indexOf("GET /L4") != -1) {
L4 = !L4;
}
if (request.indexOf("GET /L5") != -1) {
L5 = !L5;
}
if (request.indexOf("GET /L6") != -1) {
L6 = !L6;
}
if (request.indexOf("GET /L7") != -1) {
L7 = !L7;
}
if (request.indexOf("GET /L8") != -1) {
L8 = !L8;
}
html();
break;
}
}
}
delay(1);
request = "";
client.stop();
*/
}