Skip to content

Instantly share code, notes, and snippets.

@ashish1405
Last active March 21, 2023 09:21

Revisions

  1. ashish1405 revised this gist Mar 21, 2023. 1 changed file with 0 additions and 15 deletions.
    15 changes: 0 additions & 15 deletions nodemcu_esp8266_on_off_timer_switch.ino
    Original file line number Diff line number Diff line change
    @@ -48,16 +48,6 @@ void setup() {
    delay(100);
    Serial.println("Sleep!");
    ESP.deepSleep(offInterval*1000000UL, WAKE_RF_DEFAULT);



    // Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
    // Serial.print("Goodday:");
    // Serial.println(millis()-cm);
    // cm = millis();
    // delay(100);
    // ESP.deepSleep(offInterval*1000000UL);
    // delay(100);

    }

    @@ -97,10 +87,5 @@ void writeToRTCMemory() {


    void loop() {
    // put your main code here, to run repeatedly:
    // digitalWrite(D4, LOW);
    // delay(onInterval);
    // digitalWrite(D4, HIGH);
    // delay(offInterval);

    }
  2. ashish1405 created this gist Mar 21, 2023.
    106 changes: 106 additions & 0 deletions nodemcu_esp8266_on_off_timer_switch.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    #define RTCMEMORYSTART 65
    #define MAXHOUR 11 // number of hours to deep sleep for

    extern "C" {
    #include "user_interface.h"
    }

    typedef struct {
    int count;
    } rtcStore;

    rtcStore rtcMem;


    const long onInterval = 2*60*1000;
    const long offInterval = 60*60;
    unsigned long cm = 0;
    void setup() {
    // put your setup code here, to run once:
    cm = millis();

    Serial.begin(115200);
    Serial.setTimeout(2000);
    while(!Serial) { }
    delay(1000);

    Serial.println("");

    Serial.print("Waking up...");
    Serial.println(millis()-cm);

    Serial.print("Reading ");
    readFromRTCMemory();
    Serial.print("Writing ");
    writeToRTCMemory();
    Serial.print("Current Count ");
    Serial.println(rtcMem.count);

    if (rtcMem.count == 0) {
    pinMode(D4,OUTPUT);
    Serial.println("Start!");
    delay(100);
    Serial.println(millis()-cm);
    digitalWrite(D4, LOW);
    delay(onInterval);
    Serial.println(millis()-cm);
    }
    delay(100);
    Serial.println("Sleep!");
    ESP.deepSleep(offInterval*1000000UL, WAKE_RF_DEFAULT);



    // Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
    // Serial.print("Goodday:");
    // Serial.println(millis()-cm);
    // cm = millis();
    // delay(100);
    // ESP.deepSleep(offInterval*1000000UL);
    // delay(100);

    }

    void readFromRTCMemory() {
    system_rtc_mem_read(RTCMEMORYSTART, &rtcMem, sizeof(rtcMem));

    if (rtcMem.count < 0){
    rtcMem.count = 0;
    system_rtc_mem_write(RTCMEMORYSTART, &rtcMem, 4);

    pinMode(D4,OUTPUT);
    Serial.println("Start!");
    delay(100);

    digitalWrite(D4, LOW);
    delay(onInterval);
    }

    Serial.print("read count = ");
    Serial.println(rtcMem.count);
    yield();
    }

    void writeToRTCMemory() {
    if (rtcMem.count <= MAXHOUR) {
    rtcMem.count++;
    } else {
    rtcMem.count = 0;
    }

    system_rtc_mem_write(RTCMEMORYSTART, &rtcMem, 4);

    Serial.print("write count = ");
    Serial.println(rtcMem.count);
    yield();
    }


    void loop() {
    // put your main code here, to run repeatedly:
    // digitalWrite(D4, LOW);
    // delay(onInterval);
    // digitalWrite(D4, HIGH);
    // delay(offInterval);

    }