Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ********************************************************************************************
- Garage Door Opener with Warning Light and Buzzer for Marantec
- using Cayenn IoT Framework - No MQTT
- Author: Rich Plakas / Austin, TX - USA
- Reviewed: November 1, 2016
- * ********************************************************************************************
- */
- #define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
- #include <ESP8266WiFi.h>
- #include "CayenneDefines.h"
- #include "BlynkSimpleEsp8266.h"
- #include "CayenneWiFiClient.h"
- #include <Wire.h>
- #include <DHT.h>
- #define DHTTYPE DHT22
- #define DHTPIN 4
- #define VIRTUAL_PIN V1
- #define VIRTUAL_PIN V2
- #define VIRTUAL_PIN V3
- #define VIRTUAL_PIN V4
- #define VIRTUAL_PIN V5
- #define VIRTUAL_PIN V6
- #define REST 1000 //Rest Between Inhalations.
- int lightPin = 0; //define a pin for Photo resistor
- int buzzer = 5 ;// connect the I/O pin on the buzzer to this
- DHT dht(DHTPIN, DHTTYPE, 20);
- float humidity, temp_f; // Values read from sensor - See more at: http://www.esp8266.com/viewtopic.php?f=29&t=8746#sthash.X8sbMBjM.dpuf
- // Generally, you should use "unsigned long" for variables that hold time
- unsigned long previousMillis = 0;
- unsigned long lastTempUpdate = 0;
- const long interval = 5000;
- // Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
- char token[] = "Enter Your Auth Token Here";
- // Your network name and password.
- char ssid[] = "Enter Your Access Point Name";
- char password[] = "Enter Your Access Point Password";
- int value = 0;
- int gState = 0;
- long rssi;
- IPAddress ip;
- void setup()
- {
- digitalWrite(13, HIGH); //GPIO13
- pinMode(13, OUTPUT); // Pin connected to the Relay
- digitalWrite(5, HIGH); //GPIO5
- pinMode(buzzer, OUTPUT); // Pin connected to the Buzzer
- Serial.begin(230400);
- Cayenne.begin(token, ssid, password);
- dht.begin();
- }
- // ==============( Void Loop ) ====================================
- void loop() {
- Cayenne.run();
- unsigned long now = millis();
- if ( now - lastTempUpdate > 60000 ) {
- String MyIp;
- String Both;
- MyIp = "IP: " + String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]);
- String MyWiFi;
- MyWiFi = "WiFi: " + String(WiFi.RSSI());
- Serial.println(MyIp);
- Serial.println(MyWiFi);
- gettemperature();
- Serial.print("Sending temperature:");
- Serial.println(temp_f);
- Serial.print("Sending humidity:");
- Serial.println(humidity);
- lastTempUpdate = now;
- switch (gState) {
- case 1:
- Cayenne.virtualWrite(V4, 100);
- break;
- case 2 :
- Cayenne.virtualWrite(V4, 0);
- break;
- default:
- Cayenne.virtualWrite(V4, 50);
- break;
- }
- }
- }
- void gettemperature() {
- // Wait at least 2 seconds seconds between measurements.
- // if the difference between the current time and last time you read
- // the sensor is bigger than the interval you set, read the sensor
- // Works better than delay for things happening elsewhere also
- /*unsigned long currentMillis = millis();
- if(currentMillis - previousMillis >= interval) {
- // save the last time you read the sensor
- previousMillis = currentMillis; */
- // Reading temperature for humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
- humidity = dht.readHumidity(); // Read humidity (percent)
- temp_f = dht.readTemperature(true); // Read temperature as Farenheight
- // Check if any reads failed and exit early (to try again).
- if (isnan(humidity) || isnan(temp_f)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- String TempF;
- String Humid;
- TempF = String(temp_f);
- Humid = String(humidity);
- Cayenne.virtualWrite(V1, temp_f);
- Cayenne.virtualWrite(V2, humidity);
- int lightsensor = analogRead(lightPin);
- float voltage = lightsensor;
- Cayenne.virtualWrite(V6, voltage);
- Serial.print("Ambient Light: ");
- Serial.println(analogRead(lightPin));
- Serial.print("Luminosity: ");
- Serial.println(voltage);
- }
- CAYENNE_IN(V3)
- {
- // get value sent from dashboard
- int currentValue = getValue.asInt(); // 0 to 1
- // assuming you wire your relay as normally open
- if (currentValue == 0) {
- digitalWrite(13, HIGH);
- } else {
- CloseGarage();
- }
- }
- void CloseGarage() {
- //Beep Buzzer & Flash Lights to warn about Garage Door Closing
- unsigned char i, j ;// define variables
- for (i = 0; i <12; i++)
- {
- digitalWrite (buzzer, LOW) ; // Turn buzzer ON
- delay (300) ;// Delay 300ms
- digitalWrite (buzzer, HIGH) ;// turn buzzer OFF
- delay (75) ;// delay 75ms
- }
- for (i = 0; i <15; i++)
- {
- digitalWrite(13, LOW); // GPIO13 Closes Relay
- digitalWrite (buzzer, LOW) ; // Turn buzzer ON
- delay (75) ;// Delay 75ms to turn Garage Door Light Onn/Off
- digitalWrite(13, HIGH);
- digitalWrite (buzzer, HIGH) ;// turn buzzer OFF
- delay (150) ;// delay 150ms
- }
- delay(1500);
- Serial.println("Closing Garage!");
- digitalWrite(13, LOW); //Close Relay = Close Garage Door
- delay(750);
- digitalWrite(13, HIGH);
- }
- void OpenGarage() {
- Serial.println("Opening Garage!"); //
- digitalWrite(13, LOW); //Close Relay = Close Garage Door
- delay(750);
- digitalWrite(13, HIGH);
- }
Add Comment
Please, Sign In to add comment