Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 20db2ee

Browse filesBrowse files
committed
Add Time and TimerWDT examples
1 parent 79e5d4c commit 20db2ee
Copy full SHA for 20db2ee

File tree

Expand file treeCollapse file tree

2 files changed

+86
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+86
-0
lines changed
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <WiFi.h>
2+
#include "time.h"
3+
4+
const char* ssid = "YOUR_SSID";
5+
const char* password = "YOUR_PASS";
6+
7+
const char* ntpServer = "pool.ntp.org";
8+
const long gmtOffset_sec = 3600;
9+
const int daylightOffset_sec = 3600;
10+
11+
void printLocalTime()
12+
{
13+
struct tm timeinfo;
14+
if(!getLocalTime(&timeinfo)){
15+
Serial.println("Failed to obtain time");
16+
return;
17+
}
18+
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
19+
}
20+
21+
void setup()
22+
{
23+
Serial.begin(115200);
24+
25+
//connect to WiFi
26+
Serial.printf("Connecting to %s ", ssid);
27+
WiFi.begin(ssid, password);
28+
while (WiFi.status() != WL_CONNECTED) {
29+
delay(500);
30+
Serial.print(".");
31+
}
32+
Serial.println(" CONNECTED");
33+
34+
//init and get the time
35+
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
36+
printLocalTime();
37+
38+
//disconnect WiFi as it's no longer needed
39+
WiFi.disconnect(true);
40+
WiFi.mode(WIFI_OFF);
41+
}
42+
43+
void loop()
44+
{
45+
delay(1000);
46+
printLocalTime();
47+
}
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "esp_system.h"
2+
3+
const int button = 0; //gpio to use to trigger delay
4+
const int wdtTimeout = 3000; //time in ms to trigger the watchdog
5+
hw_timer_t *timer = NULL;
6+
7+
void IRAM_ATTR resetModule() {
8+
ets_printf("reboot\n");
9+
esp_restart_noos();
10+
}
11+
12+
void setup() {
13+
Serial.begin(115200);
14+
Serial.println();
15+
Serial.println("running setup");
16+
17+
pinMode(button, INPUT_PULLUP); //init control pin
18+
timer = timerBegin(0, 80, true); //timer 0, div 80
19+
timerAttachInterrupt(timer, &resetModule, true); //attach callback
20+
timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
21+
timerAlarmEnable(timer); //enable interrupt
22+
}
23+
24+
void loop() {
25+
Serial.println("running main loop");
26+
27+
timerWrite(timer, 0); //reset timer (feed watchdog)
28+
long loopTime = millis();
29+
//while button is pressed, delay up to 3 seconds to trigger the timer
30+
while (!digitalRead(button)) {
31+
Serial.println("button pressed");
32+
delay(500);
33+
}
34+
delay(1000); //simulate work
35+
loopTime = millis() - loopTime;
36+
37+
Serial.print("loop time is = ");
38+
Serial.println(loopTime); //should be under 3000
39+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.