HomeServerIOExt_Arduino.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #define INTERVAL 60000
  2. // define how many input pins should be used
  3. #define IN_PINS 3
  4. // which pin number to start at (default = 2)
  5. #define START_AT_PIN 2
  6. //
  7. // if START_AT_PIN = 2 and IN_PINS 3 is defined:
  8. // pins 2, 3, 4 are used as inputs
  9. // input debounce time in ms
  10. #define DEBOUNCETIME 250
  11. #define USE_SENSOR_DHT
  12. #ifdef USE_SENSOR_DHT
  13. #include "DHT.h"
  14. #define DHTPIN A3 // Digital pin connected to the DHT sensor
  15. // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
  16. // Pin 15 can work but DHT must be disconnected during program upload.
  17. // Uncomment whatever type you're using!
  18. //#define DHTTYPE DHT11 // DHT 11
  19. #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  20. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  21. // Initialize DHT sensor.
  22. // Note that older versions of this library took an optional third parameter to
  23. // tweak the timings for faster processors. This parameter is no longer needed
  24. // as the current DHT reading algorithm adjusts itself to work on faster procs.
  25. DHT dht(DHTPIN, DHTTYPE);
  26. #endif
  27. unsigned long intervalMillis;
  28. bool state_pin[IN_PINS];
  29. bool state_pin_pending[IN_PINS];
  30. unsigned long lastPinChange[IN_PINS];
  31. void setup() {
  32. Serial.begin(57600);
  33. Serial.print(F("HomeServerIOExt v"));
  34. Serial.println("0.1");
  35. for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) {
  36. pinMode(i, INPUT_PULLUP);
  37. }
  38. for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) {
  39. state_pin[i - START_AT_PIN] = digitalRead(i);
  40. }
  41. dht.begin();
  42. }
  43. void loop() {
  44. getInputs();
  45. if ( (millis() - intervalMillis) > INTERVAL) {
  46. intervalMillis = millis();
  47. sendPinStates_all();
  48. #ifdef USE_SENSOR_DHT
  49. // Reading temperature or humidity takes about 250 milliseconds!
  50. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  51. float h = dht.readHumidity();
  52. // Read temperature as Celsius (the default)
  53. float t = dht.readTemperature();
  54. if (isnan(h) || isnan(t)) {
  55. Serial.println(F("ERR: DHT failed to read"));
  56. return;
  57. }
  58. else {
  59. Serial.print(F("{\"T\":"));
  60. Serial.print(t);
  61. Serial.print(F(",\"H\":"));
  62. Serial.print((int)h);
  63. Serial.println(F("}"));
  64. delay(5);
  65. }
  66. #endif
  67. }
  68. }
  69. void getInputs() {
  70. bool _pinState;
  71. for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) {
  72. _pinState = digitalRead(i);
  73. // check if pin state has changed since last loop
  74. if ( state_pin_pending[i - START_AT_PIN] != _pinState ) {
  75. // if changed -> set last pin change time to now
  76. lastPinChange[i - START_AT_PIN] = millis();
  77. state_pin_pending[i - START_AT_PIN] = _pinState;
  78. }
  79. // check if pin is still in changed state after debounce timeout
  80. if ( (state_pin[i - START_AT_PIN] != _pinState) && (millis() - lastPinChange[i - START_AT_PIN]) > DEBOUNCETIME ) {
  81. state_pin[i - START_AT_PIN] = _pinState;
  82. lastPinChange[i - START_AT_PIN] = millis();
  83. sendPinState(i);
  84. }
  85. }
  86. }
  87. void sendPinState(uint8_t _pin) {
  88. Serial.print(F("P"));
  89. Serial.print(_pin);
  90. Serial.print("=");
  91. if (state_pin[_pin - START_AT_PIN]) Serial.println("H");
  92. else Serial.println("L");
  93. delay(5);
  94. }
  95. void sendPinStates_all() {
  96. for (uint8_t i = START_AT_PIN; i < (IN_PINS + START_AT_PIN); i++) {
  97. sendPinState(i);
  98. }
  99. }