Author: Not specified Language: c
Description: Not specified Timestamp: 2015-11-28 10:50:01 +0000
View raw paste Reply
  1. #include <ESP8266WiFi.h>
  2.  
  3. #define SSID "SSID"
  4. #define WIFI_PASS "pass"
  5.  
  6. #define MOTION_PIN 5
  7. #define LED_PIN 2  // Or 16
  8. #define RF_PIN 4
  9. #define ALARM_PIN 14
  10.  
  11. #define UPDATE_INTERVAL_MS 200
  12. #define SHOW_INTERVAL_MS 1000
  13. #define MAX_MESSAGE_LENGTH 150
  14. #define DELAY_MULTIPLIER 100
  15. #define UNAUTO_DELAY 13 * 60 * 60L
  16.  
  17. WiFiClient wclient;
  18.  
  19. enum {CONNECTING_WIFI, CONNECTING_MQTT, CONNECTED} state = CONNECTING_WIFI;
  20.  
  21. struct Sensors {
  22.     unsigned long motion;
  23.     unsigned long motionTime;
  24.     unsigned int motionCounter;
  25. } sensors;
  26.  
  27. struct Misc {
  28.     unsigned int lastUpdate = 0;
  29.     unsigned int lastShow = 0;
  30.     unsigned char messageCounter = 0;
  31.     unsigned char message[MAX_MESSAGE_LENGTH] = { 0 };
  32.     unsigned long autoDisabledTime = 0;
  33.     char automation = 1;
  34. } misc;
  35.  
  36.  
  37. void mqttPublish(String topic, String payload) {
  38.     Serial.println(payload);
  39. }
  40.  
  41.  
  42. void updateSensors() {
  43.     char motionInput = 0;
  44.  
  45.     // Check last update.
  46.     if (millis() > misc.lastUpdate + UPDATE_INTERVAL_MS) {
  47.         misc.lastUpdate = millis();
  48.     } else {
  49.         return;
  50.     }
  51.  
  52.     // Check motion.
  53.     motionInput = digitalRead(MOTION_PIN);
  54.  
  55.     if (motionInput) {
  56.         sensors.motionTime = millis() / 1000;
  57.     }
  58.  
  59.     if ((motionInput == 1) && (sensors.motion == 0)) {
  60.         sensors.motionCounter++;
  61.     }
  62.     sensors.motion = motionInput;
  63. }
  64.  
  65.  
  66. void showInputs() {
  67.     Serial.print("LAST_MOTION_SEC: ");
  68.     Serial.println((unsigned long)((millis() / 1000) - sensors.motionTime));
  69.     Serial.print("MOTION_COUNTER: ");
  70.     Serial.println(sensors.motionCounter);
  71.     Serial.print("AUTOMATION: ");
  72.     Serial.println((int)misc.automation);
  73.  
  74.     Serial.print("AUTO_DISABLED_TIME: ");
  75.     Serial.println(misc.autoDisabledTime);
  76.     Serial.print("SECS: ");
  77.     Serial.println(millis() / 1000);
  78.     Serial.print("UNAUTO_DELAY: ");
  79.     Serial.println(UNAUTO_DELAY);
  80. }
  81.  
  82.  
  83. void publishState() {
  84.     // Check last update.
  85.     if (millis() > misc.lastShow + SHOW_INTERVAL_MS) {
  86.         misc.lastShow = millis();
  87.     } else {
  88.         return;
  89.     }
  90.  
  91.     mqttPublish("feedback", String("LAST_MOTION_SEC: " + String((unsigned long)((millis() / 1000) - sensors.motionTime), DEC)));
  92.     mqttPublish("feedback", String("MOTION_COUNTER: " + String(sensors.motionCounter, DEC)));
  93. }
  94.  
  95.  
  96. void connectWifi() {
  97.     // Check the WiFi connection and connect if it's down.
  98.  
  99.     if (WiFi.status() == WL_CONNECTED) {
  100.         state = CONNECTING_MQTT;
  101.     } else {
  102.         state = CONNECTING_WIFI;
  103.  
  104.         delay(1);
  105.         WiFi.begin(SSID, WIFI_PASS);
  106.         while (WiFi.status() != WL_CONNECTED) {
  107.             Serial.print(".");
  108.             delay(500);
  109.         }
  110.     }
  111. }
  112.  
  113.  
  114. void setup() {
  115.     Serial.begin(57600);
  116.  
  117.     pinMode(MOTION_PIN, INPUT);
  118.     pinMode(LED_PIN, OUTPUT);
  119.     pinMode(RF_PIN, OUTPUT);
  120.  
  121.     connectWifi();
  122.  
  123.     if (WiFi.status() != WL_CONNECTED) {
  124.         Serial.println("\nfatal: WiFi router connection failed");
  125.         ESP.restart();
  126.     }
  127.  
  128.     Serial.println("\nConnected to Wifi.");
  129.  
  130.     state = CONNECTED;
  131. }
  132.  
  133.  
  134. void loop() {
  135.     digitalWrite(LED_PIN, (digitalRead(MOTION_PIN) == 1) ? LOW : HIGH);
  136.  
  137.     connectWifi();
  138.  
  139.     state = CONNECTED;
  140.  
  141.     updateSensors();
  142.     publishState();
  143. }
  144.  
View raw paste Reply