Last active
January 25, 2016 12:16
-
-
Save argelius/aa436023db966dbe2937 to your computer and use it in GitHub Desktop.
ESP8622 for my fish tank
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 <Arduino.h> | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#define ONE_WIRE_BUS 4 | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature sensors(&oneWire); | |
DeviceAddress thermometer; | |
ESP8266WiFiMulti WiFiMulti; | |
float fetchTemperature() { | |
sensors.requestTemperatures(); | |
return sensors.getTempCByIndex(0); | |
} | |
void setup() { | |
Serial.begin(115200); | |
sensors.begin(); | |
if (sensors.getAddress(thermometer, 0)) { | |
sensors.setResolution(thermometer, 12); | |
} | |
for(uint8_t t = 4; t > 0; t--) { | |
Serial.printf("[SETUP] WAIT %d...\n", t); | |
Serial.flush(); | |
delay(1000); | |
} | |
WiFiMulti.addAP("SSID", "PASSWORD"); | |
} | |
void loop() { | |
// Fetch temperature from sensor. | |
float temperature = fetchTemperature(); | |
// Send data to Firebase. | |
if((WiFiMulti.run() == WL_CONNECTED)) { | |
HTTPClient http; | |
http.begin("URL", 80, "/"); | |
String data = "{\"temperature\": " + String(temperature) + ", \"timestamp\": {\".sv\": \"timestamp\"}}"; | |
int httpCode = http.POST(data); | |
if (httpCode) { | |
if (httpCode == 200) { | |
Serial.println("Data saved! :)"); | |
delay(1000 * 60 * 5); | |
} else { | |
Serial.println("Unable to save data to server: " + String(httpCode)); | |
} | |
} else { | |
Serial.println("No connection to server."); | |
} | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment