/* Using Adafruit Huzzah ESP8266 to Dweet humidity, temp and float switch stats that end up on a Freeboard dashboard. 7/7 Added float switch 7/15 Float switch connected to pin # 4 and gnd 7/15 humidity sensor goes to pin # 2 */ #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <ESP8266HTTPClient.h> #include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); const char* ssid = "xxxxxxxxx"; const char* password = "xxxxxxxxxxxx"; const char* host = "dweet.io"; const int httpsPort = 443; String theDate = ""; int h = 0; // humidity int t = 0; // temp in celsius int f = 0; // temp in farenheit int p = 0; // sump pump int floatSwitchPin = 4; // pump pin int pumpValue = 0; unsigned long previousMillis = 0; //const long interval = 3000000; // 30000 = 5 minutes const long interval = 180000; void setup() { Serial.begin(115200); //pinMode(pumpPin, INPUT); pinMode(floatSwitchPin, INPUT_PULLUP); // pinMode(floatSwitchPin, INPUT); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(F(".")); } Serial.println(""); Serial.print(F("WiFi connected to ")); Serial.print(F("IP address: ")); Serial.println(WiFi.localIP()); delay(200); WiFiClientSecure client; if (!client.connect(host, httpsPort)) { Serial.println(F("connection failed")); return; } checkPump(); getWeather(); postData(); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; checkPump(); getWeather(); postData(); } } void postData() { HTTPClient http; char temp[400]; snprintf ( temp, 400, "{\"Temperature\":%02d,\"Humidity\":%02d,\"Pump\":%01d}", f, h, p); Serial.print(F("Posting data.......")); Serial.println(temp); http.begin("http://dweet.io/dweet/for/insertyouruniquenamehere?"); http.addHeader("Content-Type", "application/json"); int httpCode = http.POST(temp); if (httpCode == 204 || httpCode == 200) Serial.println(F("Successful posting of data")); else Serial.println (httpCode); Serial.println(""); http.end(); //Close connection // reset temp and humidity values after posting data h = 0; f = 0; } void getWeather() { Serial.println(F("Getting weather")); // get avg in case reading is off for (int i = 0; i < 3; i++) { h = h + dht.readHumidity(); t = t + dht.readTemperature(); f = f + dht.readTemperature(true); } h = h / 3; f = f / 3; Serial.print(F("Temperature = ")); Serial.print(f); Serial.print(F(" Humidity = ")); Serial.print(h); Serial.print(F(" Pump = ")); if (p == 0) Serial.println(F("Off")); else { Serial.println(F("On")); } } void checkPump() { pumpValue = digitalRead(floatSwitchPin); Serial.print(F("Float Switch = ")); Serial.println(pumpValue); p = pumpValue; delay(150); }