Created
March 1, 2017 04:02
-
-
Save Kevinwlee/408151fcc6794aabb7d21d4fdc45601d to your computer and use it in GitHub Desktop.
ThermoBot
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 <FirebaseArduino.h> | |
// Set these to run example. | |
#define FIREBASE_HOST "" | |
#define FIREBASE_AUTH "" | |
#define WIFI_SSID "" | |
#define WIFI_PASSWORD "" | |
#define DHTPIN 2 // what digital pin we're connected to | |
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
// Initialize DHT sensor. | |
DHT dht(DHTPIN, DHTTYPE); | |
//Pins | |
const int LED_PIN = 5; // Thing's onboard, blue LED | |
// Config | |
const unsigned long postRate = 60000; | |
unsigned long lastPost = 0; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("DHTxx test!"); | |
initHardware(); // Setup input/output I/O pins | |
connectWiFi(); // Connect to WiFi | |
digitalWrite(LED_PIN, LOW); // LED on to indicate connect success | |
dht.begin(); | |
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); | |
} | |
void loop() { | |
// Wait a few seconds between measurements. | |
delay(2000); | |
// 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); | |
if ((lastPost + postRate <= millis()) || lastPost == 0) { | |
Serial.println("Posting to Firebase"); | |
digitalWrite(LED_PIN, LOW); | |
Firebase.setFloat("bots/HB003/humidity", h); | |
if (Firebase.failed()) { | |
Serial.print("setting humidity failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(200); | |
Firebase.setFloat("bots/HB003/tempC", c); | |
if (Firebase.failed()) { | |
Serial.print("setting tempC failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(200); | |
Firebase.setFloat("bots/HB003/tempF", f); | |
if (Firebase.failed()) { | |
Serial.print("setting tempF failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
lastPost = millis(); | |
digitalWrite(LED_PIN, HIGH); | |
} | |
} | |
void connectWiFi() { | |
byte ledStatus = LOW; | |
// connect to wifi. | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
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 initHardware() { | |
Serial.begin(9600); | |
pinMode(LED_PIN, OUTPUT); // Set LED as output | |
digitalWrite(LED_PIN, HIGH); // LED off | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment