#include #include #include "DHT.h" #include #include #define DHTPIN 2 // Digital pin connected to the DHT sensor #define DHTTYPE DHT11 // DHT 11 DHT dht(DHTPIN, DHTTYPE); OneWire ds(0); // on pin GPIO0 (a 4.7K Pull-up resistor is necessary) String readString; const char *ssid = "Roxys House"; const char *password = "jerry&nando161"; const char *host = "script.google.com"; const int httpsPort = 443; WiFiClientSecure client; String GAS_ID = "AKfycbxD8-AupYEeqRG9tHUYZqnglBFVTY2BC0JRyf35CErib4ssx-18y44LmzBA1gvITqYy"; // Replace by your GAS service id // e.g. // https://script.google.com/macros/s/AKfycbxD8-AupYEeqRG9tHUYZqnglBFVTY2BC0JRyf35CErib4ssx-18y44LmzBA1gvITqYy/exec?B=123&C=456&D=789&E=101112 // MAC address String macAddy; byte mac[6]; String macString; // 1 is serial output, 0 is off #define _debug 1 /* Timing vaitables */ unsigned long previousMillis = 0; // will store last time LED was updated // constants won't change: const long interval = 3 * 60 * 1000; // interval at which to blink minutes (milliseconds) //const int ledPin = LED_BUILTIN;// the number of the LED pin // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&ds); // DS18S20 address on the one wire port DeviceAddress DS18S20 = {0x10, 0xF0, 0xBE, 0x7A, 0x02, 0x08, 0x00, 0xEE}; void setup() { if (_debug) { Serial.begin(115200); Serial.setTimeout(2000); } // set the digital pin as output: //pinMode(LED_BUILTIN, OUTPUT); // Disabled as the DHT11 sensor is using this pin // Turn off wifi radio untill we need it wifiModeSet(WIFI_OFF); WiFi.disconnect( true ); delay( 1 ); // WAKE_RF_DISABLED to keep the WiFi radio disabled when we wake up //ESP.deepSleep( SLEEPTIME, WAKE_RF_DISABLED ); // Get mac address as string macAddy = WiFi.macAddress(); // Get 6 hex bits of mac address WiFi.macAddress(mac); for (int i = 0; i < 6; i++) { macString += String(mac[i], HEX); } if (_debug) { Serial.println("MAC Address = " + macAddy); } // set the resolution to 10 bit (good enough?) sensors.setResolution(DS18S20, 10); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; // Check DS18S20 temp sensor float t2 = 0.0; t2 = DS_Temp(DS18S20); // Check sensor and upload data // 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 t = dht.readTemperature(); t = dht.readTemperature(); //2nd read to stabilise device? // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { if (_debug) Serial.println(F("Failed to read from DHT sensor!")); return; } else { sendData(macString, t, h, t2); } wifiModeSet(WIFI_OFF); } /* // Going to deep sleep for uS. Only uses about 20-30u to run the RTC // Requires wiring for external wakeup by RTC. GPIO16 to Reset pin Serial.println("Going into deep sleep mode for 30 seconds"); ESP.deepSleep(30e6); // 30,000,000us or 30 seconds */ } // Turn on or off the WiFi Radio void wifiModeSet(byte wifiMode) { if (wifiMode == WIFI_STA) { //Turn on LED //digitalWrite(LED_BUILTIN, LOW); // Wake up WiFi.forceSleepWake(); delay( 1 ); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); if (_debug) Serial.println("WiFI Wake and connecting/n"); while (WiFi.status() != WL_CONNECTED) { delay(500); if (_debug) Serial.print("."); } } else if (wifiMode == WIFI_OFF) { //Turn off LED //digitalWrite(LED_BUILTIN, HIGH); if (_debug) Serial.println("WiFI radio off/n"); WiFi.mode(WIFI_OFF); WiFi.forceSleepBegin(); delay( 1 ); } } // Send data to the google sheets script void sendData(String id, float temp, float humidity, float temp2) { // Emable the WiFi radio and connect wifiModeSet(WIFI_STA); client.setInsecure(); if (_debug) { Serial.print("connecting to "); Serial.println(host); } if (!client.connect(host, httpsPort)) { if (_debug) Serial.println("connection failed"); return; } String string_temperature = String(temp, 2); String string_humidity = String(humidity, 2); String string_temperature2 = String(temp2, 2); String url = "/macros/s/" + GAS_ID + "/exec?" + "B=" + id + "&C=" + string_temperature + "&D=" + string_humidity + "&E=" + string_temperature2; if (_debug) { Serial.print("requesting URL: "); Serial.println(url); } client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: BuildFailureDetectorESP8266\r\n" + "Connection: close\r\n\r\n"); if (_debug) Serial.println("request sent"); } // Get the temp from the DS sensor float DS_Temp(DeviceAddress deviceAddress) { byte i; byte present = 0; byte dataRead[12]; float tempC; ds.reset(); ds.select(deviceAddress); ds.write(0x44, 1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. present = ds.reset(); ds.select(deviceAddress); ds.write(0xBE); // Read Scratchpad if (_debug) { Serial.print(" Data = "); Serial.print(present, HEX); Serial.print(" "); } for ( i = 0; i < 9; i++) { // we need 9 bytes dataRead[i] = ds.read(); if (_debug) { Serial.print(dataRead[i], HEX); Serial.print(" "); } } if (_debug) { Serial.print(" CRC="); Serial.print(OneWire::crc8(dataRead, 8), HEX); Serial.println(); } int16_t raw = (dataRead[1] << 8) | dataRead[0]; raw = raw << 3; // 9 bit resolution default if (dataRead[7] == 0x10) { // "count remain" gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - dataRead[6]; } tempC = (float)raw / 16.0; return tempC; }