Last active
April 5, 2018 23:35
-
-
Save Kevinwlee/7d666f631e8e8baebdd965962871d436 to your computer and use it in GitHub Desktop.
Thing Dev Board Garage Door
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DHT.h" | |
#include <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include <FirebaseArduino.h> | |
// Firebase info | |
const char FIREBASE_HOST[] = "YOUR_FIREBASE_HOST_HERE.firebaseio.com"; | |
const char FIREBASE_AUTH[] = "YOUR_FIRBASE_ADMIN_KEY"; | |
//wifi info | |
const char WiFiSSID[] = "YOUR_WIFI_NETWORK"; | |
const char WiFiPSK[] = "YOUR_WIFI_PASSWORD"; | |
//Set up DHT info | |
const int DHTPIN = 2; // digital pin DHT22 is connected to | |
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
//Other Pins | |
const int LED_PIN = 5; // Thing's onboard LED | |
const int DIGITAL_PIN = 12; // Digital pin that triggers garage | |
const int CIRCUIT_PIN = 13; // magnet connected to digital pin 7 | |
// Initialize DHT sensor. | |
DHT dht(DHTPIN, DHTTYPE); | |
// Initialize server | |
WiFiServer server(80); | |
// Rate Limiting Info for posting to Firebase | |
const unsigned long postRate = 60000; | |
unsigned long lastPost = 0; | |
// Default door state | |
int door = 0; | |
void setup() { | |
initHardware(); | |
connectWiFi(); | |
setupMDNS(); | |
server.begin(); | |
dht.begin(); | |
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); | |
} | |
void loop() { | |
// Check if a client has connected | |
WiFiClient client = server.available(); | |
if (client) { | |
processRequest(client); | |
} else { | |
//check sensors | |
checkSensors(); | |
} | |
} | |
void processRequest(WiFiClient client) { | |
// Read the first line of the request | |
String req = client.readStringUntil('\r'); | |
Serial.println(req); | |
client.flush(); | |
// Match the request | |
int val = -1; | |
if (req.indexOf("/door/1") != -1) | |
val = 1; //on | |
// Turn the pin on for 500ms (triggers the garage) | |
if (val >= 0) { | |
digitalWrite(DIGITAL_PIN, 1); | |
delay(500); | |
digitalWrite(DIGITAL_PIN, 0); | |
} | |
client.flush(); | |
// Prepare the response. Start with the common header: | |
String s = "HTTP/1.1 200 OK\r\n"; | |
s += "Content-Type: text/html\r\n\r\n"; | |
s += "<!DOCTYPE HTML>\r\n<html>\r\n"; | |
// If we're setting the LED, print out a message saying we did | |
if (val >= 0) { | |
s += "Garage is now moving"; | |
} else { | |
s += "Invalid Request.<br> Try /garage/1."; | |
} | |
s += "</html>\n"; | |
// Send the response to the client | |
client.print(s); | |
delay(1); | |
Serial.println("Client disonnected"); | |
} | |
void checkDoor() { | |
int doorRead = digitalRead(CIRCUIT_PIN); // read the input pin | |
if (door == doorRead) { | |
//Door is unchanged. | |
return; | |
} | |
door = doorRead; | |
String state; | |
if (door == 0) { | |
state = "closed"; | |
} else { | |
state = "open"; | |
} | |
digitalWrite(LED_PIN, LOW); | |
Serial.println("Posting to Firebase"); | |
Serial.println(state); | |
Firebase.setString("bots/HB006/door", state); | |
if (Firebase.failed()) { | |
Serial.print("setting humidity failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
digitalWrite(LED_PIN, HIGH); | |
} | |
void checkHumidity() { | |
if ((lastPost + postRate <= millis()) || lastPost == 0) { | |
// 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 c = dht.readTemperature(); | |
// Read temperature as Fahrenheit (isFahrenheit = true) | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(c) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
Serial.println(h); | |
Serial.println(c); | |
Serial.println(f); | |
Serial.println("Posting to Firebase"); | |
digitalWrite(LED_PIN, LOW); | |
Firebase.setFloat("bots/HB006/humidity", h); | |
if (Firebase.failed()) { | |
Serial.print("setting humidity failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(200); | |
Firebase.setFloat("bots/HB006/tempC", c); | |
if (Firebase.failed()) { | |
Serial.print("setting tempC failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(200); | |
Firebase.setFloat("bots/HB006/tempF", f); | |
if (Firebase.failed()) { | |
Serial.print("setting tempF failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
lastPost = millis(); | |
digitalWrite(LED_PIN, HIGH); | |
} | |
} | |
void checkSensors() { | |
checkDoor(); | |
checkHumidity(); | |
} | |
void connectWiFi() { | |
byte ledStatus = LOW; | |
// Set WiFi mode to station (as opposed to AP or AP_STA) | |
WiFi.mode(WIFI_STA); | |
// connect to wifi. | |
WiFi.begin(WiFiSSID, WiFiPSK); | |
Serial.print("connecting"); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
digitalWrite(LED_PIN, ledStatus); // Write LED high/low | |
ledStatus = (ledStatus == HIGH) ? LOW : HIGH; | |
delay(100); | |
} | |
Serial.println(); | |
Serial.print("connected: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void setupMDNS() { | |
// Call MDNS.begin(<domain>) to set up mDNS to point to | |
// "<domain>.local" | |
if (!MDNS.begin("garage1")) { | |
Serial.println("Error setting up MDNS responder!"); | |
while(1) { | |
delay(1000); | |
} | |
} | |
Serial.println("mDNS responder started"); | |
} | |
void initHardware() { | |
Serial.begin(9600); | |
pinMode(DIGITAL_PIN, OUTPUT); | |
pinMode(DIGITAL_PIN, HIGH); | |
pinMode(LED_PIN, OUTPUT); | |
pinMode(CIRCUIT_PIN, INPUT_PULLUP); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment