<commit_before>/*
 * Copyright (C) 2015 <NAME>  <http://gregmeiste.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Adafruit_DHT/Adafruit_DHT.h"

#define DHT_PIN         D2
#define DHT_TYPE        DHT22

#define BUTTON_PIN      D1
#define BUTTON_PRESS_MS 50

#define LED_PIN         D7

unsigned long reportIntervalMillis = 2000;
bool isReporting = true;

volatile unsigned long buttonPressMillis = 0;
volatile unsigned long buttonReleaseMillis = 0;

DHT dht(DHT_PIN, DHT_TYPE);

void buttonPress() {
    buttonPressMillis = millis();
    buttonReleaseMillis = 0;
}

void checkStateChange() {
    if (buttonPressMillis > 0) {
        if (digitalRead(BUTTON_PIN) == HIGH) {
            // False button press, ignore.
            buttonPressMillis = 0;
        } else if ((millis() - buttonPressMillis) >= BUTTON_PRESS_MS) {
            // Button press debounced
            if (isReporting) {
                digitalWrite(LED_PIN, HIGH);
                Serial.println("Reporting stopped!");
            } else {
                digitalWrite(LED_PIN, LOW);
                Serial.println("Reporting started!");
            }
            isReporting = !isReporting;
            buttonPressMillis = 0;
        }
    } else if (!isReporting && (digitalRead(BUTTON_PIN) == HIGH)) {
        if (buttonReleaseMillis > 0) {
            if ((millis() - buttonReleaseMillis) >= BUTTON_PRESS_MS) {
                Serial.println("Entering STOP mode");
                Spark.sleep(BUTTON_PIN, FALLING);
            }
        } else {
            buttonReleaseMillis = millis();
        }
    }
}

void doReportIfTime() {
    static unsigned long lastReadingMillis = 0;
    unsigned long now = millis();

    if ((now - lastReadingMillis) >= reportIntervalMillis) {
        float h = dht.getHumidity();
        float f = dht.getTempFarenheit();

        // Check if any reads failed and exit early (to try again).
        if (isnan(h) || isnan(f)) {
            Serial.println("Failed to read from DHT sensor!");
            return;
        }

        Serial.print("Humid: "); 
        Serial.print(h);
        Serial.print("% - ");
        Serial.print("Temp: "); 
        Serial.print(f);
        Serial.print("*F ");
        Serial.println(Time.timeStr());

        lastReadingMillis = now;
    }
}

void setup() {
    Serial.begin(9600);

    pinMode(LED_PIN, OUTPUT);

    pinMode(BUTTON_PIN, INPUT_PULLUP);
    attachInterrupt(BUTTON_PIN, buttonPress, FALLING);

    dht.begin();
}

void loop() {
    checkStateChange();

    if (isReporting) {
        doReportIfTime();
    }
}
<commit_msg>Spark Core: Add function to report sensor data to server (someday)<commit_after>/*
 * Copyright (C) 2015 <NAME>  <http://gregmeiste.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Adafruit_DHT/Adafruit_DHT.h"

#define DHT_PIN            D2
#define DHT_TYPE           DHT22

#define BUTTON_PIN         D1
#define BUTTON_PRESS_MS    50

#define LED_PIN            D7

#define SENSOR_CHECK_MS    2000
#define SENSOR_REPORT_MS   30000

bool isReporting = true;
volatile unsigned long buttonPressMillis = 0;
volatile unsigned long buttonReleaseMillis = 0;

DHT dht(DHT_PIN, DHT_TYPE);

void buttonPress() {
    buttonPressMillis = millis();
    buttonReleaseMillis = 0;
}

void checkStateChange() {
    if (buttonPressMillis > 0) {
        if (digitalRead(BUTTON_PIN) == HIGH) {
            // False button press, ignore.
            buttonPressMillis = 0;
        } else if ((millis() - buttonPressMillis) >= BUTTON_PRESS_MS) {
            // Button press debounced
            if (isReporting) {
                digitalWrite(LED_PIN, HIGH);
                Serial.println("Reporting stopped!");
            } else {
                digitalWrite(LED_PIN, LOW);
                Serial.println("Reporting started!");
            }
            isReporting = !isReporting;
            buttonPressMillis = 0;
        }
    } else if (!isReporting && (digitalRead(BUTTON_PIN) == HIGH)) {
        if (buttonReleaseMillis > 0) {
            if ((millis() - buttonReleaseMillis) >= BUTTON_PRESS_MS) {
                Serial.println("Entering STOP mode");
                Spark.sleep(BUTTON_PIN, FALLING);
            }
        } else {
            buttonReleaseMillis = millis();
        }
    }
}

void doMonitorIfTime() {
    static unsigned long lastReadingMillis = 0;
    unsigned long now = millis();

    if ((now - lastReadingMillis) >= SENSOR_CHECK_MS) {
        float h = dht.getHumidity();
        float f = dht.getTempFarenheit();

        // Check if any reads failed and exit early (to try again).
        if (isnan(h) || isnan(f)) {
            Serial.println("Failed to read from DHT sensor!");
            return;
        }

        Serial.print("Humid: "); 
        Serial.print(h);
        Serial.print("% - ");
        Serial.print("Temp: "); 
        Serial.print(f);
        Serial.print("*F ");
        Serial.println(Time.timeStr());

        lastReadingMillis = now;
    }
}

void doReportIfTime() {
    static unsigned long lastReportMillis = 0;
    unsigned long now = millis();

    if ((now - lastReportMillis) >= SENSOR_REPORT_MS) {
        Serial.println("Reporting sensor data to server");
        
        // TODO: implement
        
        lastReportMillis = now;
    }
}

void setup() {
    Serial.begin(9600);

    pinMode(LED_PIN, OUTPUT);

    pinMode(BUTTON_PIN, INPUT_PULLUP);
    attachInterrupt(BUTTON_PIN, buttonPress, FALLING);

    dht.begin();
}

void loop() {
    checkStateChange();

    if (isReporting) {
        doMonitorIfTime();
        doReportIfTime();
    }
}

{'fraction_non_alphanumeric': 0.10709987966305656, 'fraction_numerical': 0.01022864019253911, 'mean_word_length': 2.316209476309227, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2847310', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1955, 'n_words': 541}