/* 8/7/17 The Soldering Station 8/7/17 Adafruit Huzzah ESP8266 Weather Station With Sensiron SHT31-D Temperature & Humidity Sensor Posting To Dweet 8/9 - Added battery check 8/13 - Adding posting to Weather Underground https://www.wunderground.com/personal-weather-station/dashboard?ID=KNCRALEI304 8/20 Finally got Wunderground working. 8/23 Added dewpoint */ #include #include #include #include "Adafruit_SHT31.h" #include #include Adafruit_SHT31 sht31 = Adafruit_SHT31(); // WiFi Login const char* ssid = “xxxxxx”; const char* password = “xxxxxx”; // Wunderground Login char ID [] = “xxxxxxx”; char PASSWORD [] = “xxxxxx”; const char* host = "dweet.io"; const int httpsPort = 443; const char* host2 = "rtupdate.wunderground.com"; const int sleepTimeS = 360; // 10 = 10 seconds 180 = 3 minutes 360 = 6 minutes float t = 0.0; float h = 0.0; float f = 0.0; int value = 0; int tem = 0; int hum = 0; int level = 0; int dewptf = 0; int dew = 0; void setup() { Serial.begin(115200); delay(100); Serial.println(F("Feather ESP8266 Wifi Weather Station Vs 7")); Serial.println(); Serial.print(F("Connecting to ")); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println(F("WiFi connected")); Serial.println(F("IP address: ")); Serial.println(WiFi.localIP()); if (! sht31.begin(0x44)) { Serial.println(F("Couldn't find SHT31")); while (1) delay(1); } } void loop() { getWeather(); dewptf = (dewpointCalc(f, h)); dew = dewptf; battery_level(); postData(); delay(90); // uncomment line below to send data to Wunderground //senddataWU(); // data posted - reset variables t = 0.0; f = 0.0; h = 0.0; level = 0; sleepTime(); } void sleepTime() { Serial.println(F("ESP8266 in sleep mode")); ESP.deepSleep(sleepTimeS * 1000000); } void getWeather() { for (int i = 0; i < 3; i++) { // get avg of 3 readings t = t + sht31.readTemperature(); h = h + sht31.readHumidity(); delay(5); } t = t / 3; h = h / 3; Serial.println(F("")); if (! isnan(t)) { // check if 'is not a number' Serial.print(F("Temp *C = ")); Serial.println(t); } else { Serial.println(F("Failed to read temperature")); } f = (t * 1.8) + 32; Serial.print(F("Temp F = ")); Serial.println(f); if (! isnan(h)) { // check if 'is not a number' Serial.print(F("Hum. % = ")); Serial.println(h); } else { Serial.println(F("Failed to read humidity")); } Serial.println(F("")); tem = (int)f; hum = (int)h; } void postData() { Serial.print("Connecting to "); Serial.println(host); WiFiClientSecure client; //const int httpPort = 80; if (!client.connect(host, httpsPort)) { Serial.println(F("Connection failed")); return; } HTTPClient http; char temp[500]; snprintf (temp, 500, "{\"Temperature\":%02d,\"Humidity\":%02d,\"Dewpoint\":%02d,\"Battery\":%2d}", tem, hum, dew, level); Serial.print(F("Posting data...")); Serial.println(temp); http.begin("http://dweet.io/dweet/for/xxxxxx?”); http.addHeader("Content-Type", "application/json"); int httpCode = http.POST(temp); if (httpCode == 204 || httpCode == 200 || httpCode == -11) Serial.println(F("Successful posting of data")); else Serial.println (httpCode); http.end(); Serial.println(F("")); Serial.println(F("-----------------------------")); Serial.println(F("")); } void battery_level() { for (int i = 0; i < 3; i++) { // get avg of 3 readings level = level + analogRead(A0); delay(2); } level = level / 3; // convert battery level to percent level = map(level, 580, 758, 0, 100); // bit of a kludge fully charged battery shows up as 75-76% // Add 21 to to get ~ real battery level level = level + 21; if (level >= 100) level = 100; } void senddataWU() { Serial.print(F("Connecting to ")); Serial.println(host2); Serial.println(F("Sending data.")); WiFiClient client; const int httpPort = 80; if (!client.connect(host2, httpPort)) { Serial.println(F("connection failed")); // startwifi(); return; } String url = "/weatherstation/updateweatherstation.php?ID="; url += ID; url += "&PASSWORD="; url += PASSWORD; url += "&dateutc=now"; url += "&tempf="; url += tem; url += "&humidity="; url += hum; url += "&dewptf="; url += dewptf; url += "&softwaretype=Arduino-ESP8266&action=updateraw&realtime=1&rtfreq=2.5"; Serial.print(F("Requesting URL: ")); Serial.println(url); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); while (client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); delay(10); } Serial.println(); Serial.println(F("Closing connection")); } int dewpointCalc(int tempf, int humidity) { return (tem - (.36 * (100 - hum))); }