RichP

ESP8266 IoT WiFi Garage Door Opener with Cayenne

Nov 1st, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. /*   ********************************************************************************************
  2.           Garage Door Opener with Warning Light and Buzzer for Marantec
  3.           using Cayenn IoT Framework - No MQTT
  4.           Author: Rich Plakas / Austin, TX - USA
  5.           Reviewed: November 1, 2016
  6.  *   ********************************************************************************************
  7. */
  8. #define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
  9.  
  10. #include <ESP8266WiFi.h>
  11. #include "CayenneDefines.h"
  12. #include "BlynkSimpleEsp8266.h"
  13. #include "CayenneWiFiClient.h"
  14.  
  15. #include <Wire.h>
  16. #include <DHT.h>
  17. #define DHTTYPE DHT22
  18. #define DHTPIN 4
  19. #define VIRTUAL_PIN V1
  20. #define VIRTUAL_PIN V2
  21. #define VIRTUAL_PIN V3
  22. #define VIRTUAL_PIN V4
  23. #define VIRTUAL_PIN V5
  24. #define VIRTUAL_PIN V6
  25. #define REST      1000    //Rest Between Inhalations.
  26.  
  27. int lightPin = 0;  //define a pin for Photo resistor
  28. int buzzer = 5 ;// connect the I/O pin on the buzzer to this
  29.  
  30. DHT dht(DHTPIN, DHTTYPE, 20);
  31. float humidity, temp_f;  // Values read from sensor - See more at: http://www.esp8266.com/viewtopic.php?f=29&t=8746#sthash.X8sbMBjM.dpuf
  32.  
  33. // Generally, you should use "unsigned long" for variables that hold time
  34. unsigned long previousMillis = 0;
  35. unsigned long lastTempUpdate = 0;  
  36. const long interval = 5000;
  37.  
  38. // Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
  39. char token[] = "Enter Your Auth Token Here";
  40.  
  41. // Your network name and password.
  42. char ssid[] = "Enter Your Access Point Name";
  43. char password[] = "Enter Your Access Point Password";
  44.  
  45. int value = 0;
  46. int gState = 0;
  47. long rssi;
  48. IPAddress ip;
  49.  
  50. void setup()
  51. {
  52.   digitalWrite(13, HIGH); //GPIO13
  53.   pinMode(13, OUTPUT);       // Pin connected to the Relay
  54.   digitalWrite(5, HIGH); //GPIO5
  55.   pinMode(buzzer, OUTPUT);       // Pin connected to the Buzzer
  56.   Serial.begin(230400);
  57.   Cayenne.begin(token, ssid, password);
  58.   dht.begin();
  59. }
  60.  
  61.  
  62.  
  63. // ==============( Void Loop  ) ====================================
  64.  
  65. void loop() {
  66.   Cayenne.run();
  67.   unsigned long now = millis();
  68.   if ( now - lastTempUpdate > 60000 ) {  
  69.     String MyIp;  
  70.     String Both;
  71.     MyIp =  "IP: " + String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]);
  72.     String MyWiFi;
  73.     MyWiFi = "WiFi: " + String(WiFi.RSSI());
  74.     Serial.println(MyIp);
  75.     Serial.println(MyWiFi);
  76.     gettemperature();
  77.     Serial.print("Sending temperature:");
  78.     Serial.println(temp_f);
  79.     Serial.print("Sending humidity:");
  80.     Serial.println(humidity);
  81.     lastTempUpdate = now;
  82.     switch (gState) {
  83.        case 1:
  84.        Cayenne.virtualWrite(V4, 100);
  85.        break;
  86.        case 2 :
  87.        Cayenne.virtualWrite(V4, 0);
  88.        break;
  89.        default:
  90.        Cayenne.virtualWrite(V4, 50);
  91.        break;
  92.     }  
  93.   }
  94.    
  95. }
  96.  
  97. void gettemperature() {
  98.   // Wait at least 2 seconds seconds between measurements.
  99.   // if the difference between the current time and last time you read
  100.   // the sensor is bigger than the interval you set, read the sensor
  101.   // Works better than delay for things happening elsewhere also
  102.   /*unsigned long currentMillis = millis();
  103.  
  104.   if(currentMillis - previousMillis >= interval) {
  105.     // save the last time you read the sensor
  106.     previousMillis = currentMillis;   */
  107.  
  108.     // Reading temperature for humidity takes about 250 milliseconds!
  109.     // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
  110.     humidity = dht.readHumidity();          // Read humidity (percent)
  111.     temp_f = dht.readTemperature(true);     // Read temperature as Farenheight
  112.    
  113.    
  114.     // Check if any reads failed and exit early (to try again).
  115.     if (isnan(humidity) || isnan(temp_f)) {
  116.       Serial.println("Failed to read from DHT sensor!");
  117.       return;
  118.     }
  119.     String TempF;
  120.     String Humid;
  121.     TempF = String(temp_f);
  122.     Humid = String(humidity);
  123.     Cayenne.virtualWrite(V1, temp_f);
  124.     Cayenne.virtualWrite(V2, humidity);
  125.  
  126.     int lightsensor = analogRead(lightPin);
  127.     float voltage = lightsensor;
  128.    
  129.     Cayenne.virtualWrite(V6, voltage);
  130.     Serial.print("Ambient Light: ");
  131.     Serial.println(analogRead(lightPin));
  132.     Serial.print("Luminosity: ");
  133.     Serial.println(voltage);
  134. }
  135.  
  136. CAYENNE_IN(V3)
  137. {
  138.   // get value sent from dashboard
  139.   int currentValue = getValue.asInt(); // 0 to 1
  140.  
  141.   // assuming you wire your relay as normally open
  142.   if (currentValue == 0) {
  143.     digitalWrite(13, HIGH);
  144.   } else {
  145.     CloseGarage();
  146.     }
  147. }
  148.  
  149. void CloseGarage() {
  150.   //Beep Buzzer & Flash Lights to warn about Garage Door Closing
  151.      unsigned char i, j ;// define variables
  152.       for (i = 0; i <12; i++)
  153.       {
  154.       digitalWrite (buzzer, LOW) ; // Turn buzzer ON
  155.       delay (300) ;// Delay 300ms
  156.       digitalWrite (buzzer, HIGH) ;// turn buzzer OFF
  157.       delay (75) ;// delay 75ms
  158.     }
  159.       for (i = 0; i <15; i++)
  160.       {
  161.       digitalWrite(13, LOW); // GPIO13 Closes Relay
  162.       digitalWrite (buzzer, LOW) ; // Turn buzzer ON
  163.       delay (75) ;// Delay 75ms to turn Garage Door Light Onn/Off
  164.       digitalWrite(13, HIGH);
  165.       digitalWrite (buzzer, HIGH) ;// turn buzzer OFF
  166.       delay (150) ;// delay 150ms
  167.       }
  168.       delay(1500);
  169.       Serial.println("Closing Garage!");
  170.       digitalWrite(13, LOW); //Close Relay = Close Garage Door
  171.       delay(750);
  172.       digitalWrite(13, HIGH);    
  173. }
  174.  
  175. void OpenGarage() {  
  176.       Serial.println("Opening Garage!"); //
  177.       digitalWrite(13, LOW); //Close Relay = Close Garage Door
  178.       delay(750);
  179.       digitalWrite(13, HIGH);    
  180. }
Add Comment
Please, Sign In to add comment