#define INTERVAL 60000 // define how many input pins should be used #define IN_PINS 4 // which pin number to start at (default = 2) #define START_AT_PIN 2 // // if START_AT_PIN = 2 and IN_PINS 3 is defined: // pins 2, 3, 4 are used as inputs // input debounce time in ms #define DEBOUNCETIME 250 #define USE_SENSOR_DHT #ifdef USE_SENSOR_DHT #include "DHT.h" #define DHTPIN A3 // Digital pin connected to the DHT sensor // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- // Pin 15 can work but DHT must be disconnected during program upload. // Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Initialize DHT sensor. // Note that older versions of this library took an optional third parameter to // tweak the timings for faster processors. This parameter is no longer needed // as the current DHT reading algorithm adjusts itself to work on faster procs. DHT dht(DHTPIN, DHTTYPE); #endif unsigned long intervalMillis; bool state_pin[IN_PINS]; bool state_pin_pending[IN_PINS]; unsigned long lastPinChange[IN_PINS]; void setup() { Serial.begin(115200); Serial.print(F("HomeServerIOExt v")); Serial.println("0.1"); for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) { pinMode(i, INPUT_PULLUP); } for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) { state_pin[i - START_AT_PIN] = digitalRead(i); } dht.begin(); } void loop() { getInputs(); if ( (millis() - intervalMillis) > INTERVAL) { intervalMillis = millis(); sendPinStates_all(); #ifdef USE_SENSOR_DHT // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println(F("ERR: DHT failed to read")); return; } else { Serial.print(F("{\"T\":")); Serial.print(t); Serial.print(F(",\"H\":")); Serial.print((int)h); Serial.println(F("}")); delay(5); } #endif } } void getInputs() { bool _pinState; for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) { _pinState = digitalRead(i); // check if pin state has changed since last loop if ( state_pin_pending[i - START_AT_PIN] != _pinState ) { // if changed -> set last pin change time to now lastPinChange[i - START_AT_PIN] = millis(); state_pin_pending[i - START_AT_PIN] = _pinState; } // check if pin is still in changed state after debounce timeout if ( (state_pin[i - START_AT_PIN] != _pinState) && (millis() - lastPinChange[i - START_AT_PIN]) > DEBOUNCETIME ) { state_pin[i - START_AT_PIN] = _pinState; lastPinChange[i - START_AT_PIN] = millis(); sendPinState(i); } } } void sendPinState(uint8_t _pin) { Serial.print(F("P")); Serial.print(_pin); Serial.print("="); if (state_pin[_pin - START_AT_PIN]) Serial.println("H"); else Serial.println("L"); delay(5); } void sendPinStates_all() { for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) { sendPinState(i); } }