#define BLYNK_PRINT Serial #include #include #include #include #include #include #include #include #include #include #include /* Program code related to Real Time Clock (RTC). */ #include /* Communication code with Blynk Real Time Clock Widget */ #define DHTPIN D0 #define DHTTYPE DHT22 char auth[] = "6yOomrVt8L0bD6VPc3nYunvLaLIEYXqf"; LiquidCrystal_I2C lcd(0x27,20,4); SoftwareSerial mySerial(D4, A0);//(RX,TX) TinyGPS gps; void gpsdump(TinyGPS &gps); void printFloat(double f, int digits = 2); void GPSdata(); const int RecordTime = 1; //Define Measuring Time (Seconds) const int SensorPin = D3; //Define Interrupt Pin (2 or 3 @ Arduino Uno) const int chipSelect = D8; volatile int InterruptCounter; float WindSpeed; float WS; char ssid[] = "Complex Hydrologic Unit"; char pass[] = "SIP6230021"; //----------------------------------------Host & httpsPort const char* host = "script.google.com"; const int httpsPort = 443; // Initialize DHT sensor. DHT dht(DHTPIN,DHTTYPE,12); BlynkTimer timer; WidgetRTC rtc; unsigned long startMillis; /* start counting time for display refresh*/ unsigned long currentMillis; /* current counting time for display refresh */ const unsigned long period = 1000; // refresh every X seconds (in seconds) Default 60000 = 1 minute WiFiClientSecure client; //--> Create a WiFiClientSecure object. String GAS_ID = "AKfycby-ldoR7WMjFK-A7VBhCWXxUVQ_lnTw3mP9Z3fEPC3hf99okGAvHOjKOFM4jJi4vQr-Ag"; //--> spreadsheet script ID int count=0; BLYNK_CONNECTED() /* When Blynk server is connected, initiate Real Time Clock function */ { rtc.begin(); } void Display() { float flat, flon ; gps.f_get_position(&flat, &flon); String Strflat = String(flat, 6); String Strflon = String(flon, 6); meassure(); if(currentMillis - startMillis > period) /* For every 1 second, run the set of code*/ { String currentTime = String(hour()) + ":" + minute() + ":" + second(); /* Define "currentTime" by combining hour, minute and second */ String currentDate = String(day()) + " " + month() + " " + year(); /* Define "currentDate" by combining day, month, and year */ Blynk.virtualWrite(V1, currentTime); /* Send Time parameters to Virtual Pin V1 on Blynk App */ Blynk.virtualWrite(V2, currentDate); /* Send Date parameters to Virtual Pin V2 on Blynk App */ int getSecond = second(); /* Assign "getHour" as the hour now */ if (getSecond > 30) { digitalWrite(2,HIGH);} /* Turn OFF the LED if seconds count is more than 30 */ if (getSecond < 30) { digitalWrite(2,LOW); } /* Turn ON the LED if the seconds count is less than 30 */ startMillis = millis(); /* Reset time for the next counting cycle */ } Blynk.virtualWrite(V4, WS); Blynk.virtualWrite(V6, dht.readTemperature()); Blynk.virtualWrite(V5, dht.readHumidity()); Blynk.virtualWrite(V8, Strflat); Blynk.virtualWrite(V9, Strflon); Blynk.virtualWrite(V10, gps.f_altitude()); lcd.setCursor(0, 0); lcd.print("T:"); lcd.print(dht.readTemperature()); lcd.print(" "); lcd.print("H:"); lcd.print(dht.readHumidity()); lcd.print("%"); lcd.setCursor(0, 1); lcd.print("WS:"); lcd.print(WS); lcd.setCursor(10, 1); lcd.print(" m/s"); } void sendSensor() { float h = dht.readHumidity(); float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit float flat, flon ; if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } // You can send any value at any time. // Please don't send more that 10 values per second. gps.f_get_position(&flat, &flon); Serial.print("Temp. = "); Serial.print(t); Serial.print(" C "); Serial.print("; Humidity = "); Serial.print(h); Serial.println(" %"); Serial.print("Wind Speed: "); Serial.print(WS); //Speed in km/h Serial.print(" km/h - "); Serial.print(WS); //Speed in m/s Serial.println(" m/s"); GPSdata(); String Strflat = String(flat, 6); String Strflon = String(flon, 6); sendData(t,h,Strflat,Strflon,gps.f_altitude(),WS); } void sendData(float string_temp,float string_humi,String string_Lat,String string_Long, float string_mMSL ,float string_Windspeed) { Serial.println("=========="); Serial.print("connecting to "); Serial.println(host); //----------------------------------------Connect to Google host if (!client.connect(host, httpsPort)) { Serial.println("connection failed"); return; } String url = "/macros/s/" + GAS_ID + "/exec?temp=" + string_temp + "&humi=" + string_humi + "&Lat=" + string_Lat + "&Long=" + string_Long + "&mMSL=" + string_mMSL + "&Windspeed=" + string_Windspeed ; 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"); Serial.println("request sent"); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { Serial.println("headers received"); break; } } String line = client.readStringUntil('\n'); if (line.startsWith("{\"state\":\"success\"")) { Serial.println("esp8266/Arduino CI successfull!"); } else { Serial.println("esp8266/Arduino CI has failed"); } Serial.print("reply was : "); Serial.println(line); Serial.println("closing connection"); Serial.println("=========="); Serial.println(); } void GPSdata() { bool newdata = false; unsigned long start = millis(); // Every 5 seconds we print an update while (millis() - start < 5000) { if (mySerial.available()) { char c = mySerial.read(); //Serial.print(c); // uncomment to see raw GPS data if (gps.encode(c)) { newdata = true; break; // uncomment to print new data immediately! } } } if (newdata) { Serial.println("Acquired Data"); Serial.println("-------------"); gpsdump(gps); Serial.println("-------------"); Serial.println(); } } void setup() { Serial.begin(115200); //delay(500); mySerial.begin(9600); //delay(1000); Serial.println("uBlox Neo 6M"); pinMode(SensorPin,INPUT); Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version()); Serial.println(); Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS)); Serial.println(); dht.begin(); Blynk.begin(auth, ssid, pass, "pkdb10.thddns.net", 8334); Wire.begin(); lcd.init(); lcd.backlight(); // WiFi.begin(ssid, password); //--> Connect to your WiFi router Serial.println(""); //----------------------------------------Wait for connection Serial.print("Connecting"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); } //---------------------------------------- //----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor Serial.println(""); Serial.print("Successfully connected to : "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.println(); //---------------------------------------- startMillis = millis(); client.setInsecure(); timer.setInterval(500L, Display); timer.setInterval(60000L, sendSensor); timer.setInterval(60000L, sendSD); } //============================================================================== void loop void loop() { Blynk.run(); timer.run(); } void sendSD() { while (!Serial) { } if (!SD.begin(chipSelect)) { Serial.println("Initialization failed!"); return ; } String dataString = ""; float h = dht.readHumidity(); float t = dht.readTemperature(); byte month, day, hour, minute, second, hundredths; int year; float flat, flon; gps.f_get_position(&flat, &flon); gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths); String Strflat = String(flat, 6); String strflon = String(flon, 6); dataString += static_cast(day); dataString += "/"; dataString += static_cast(month); dataString += "/"; dataString += (year); dataString += " , "; dataString += static_cast(hour + 7); dataString += ":"; dataString += static_cast(minute); dataString += ":"; dataString += static_cast(second); dataString += "."; dataString += static_cast(hundredths); dataString += " , "; dataString += String(t); dataString += " , "; dataString += String(h); dataString += " , "; dataString += (WS); dataString += " , "; dataString += String(flat, 6); dataString += " , "; dataString += String(flon, 6); File dataFile = SD.open("test.txt", FILE_WRITE); if (dataFile) { dataFile.println(dataString); dataFile.close(); Serial.println(dataString); Serial.println("=========="); Serial.println(); } else { Serial.println("error opening datalog.txt"); Serial.println("=========="); Serial.println(); } } void gpsdump(TinyGPS &gps) { float flat, flon; unsigned long age, date, time, chars; int year; byte month, day, hour, minute, second, hundredths; unsigned short sentences, failed; gps.f_get_position(&flat, &flon, &age); Serial.print("Lat/Long(float): "); printFloat(flat, 6); Serial.print(", "); printFloat(flon, 6); Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); Serial.print("Date: "); Serial.print(static_cast(month)); Serial.print("/"); Serial.print(static_cast(day)); Serial.print("/"); Serial.print(year); Serial.print(" Time: "); Serial.print(static_cast(hour + 7)); Serial.print(":"); //Serial.print("UTC +08:00 Malaysia"); Serial.print(static_cast(minute)); Serial.print(":"); Serial.print(static_cast(second)); Serial.print("."); Serial.print(static_cast(hundredths)); Serial.print(" UTC +07:00 Bangkok"); Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms."); Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Alt(m): "); printFloat(gps.f_altitude()); Serial.println(); Serial.print("Speed (mps): "); printFloat(gps.f_speed_mps()); Serial.println(); gps.stats(&chars, &sentences, &failed); Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed); } void printFloat(double number, int digits) { // Handle negative numbers if (number < 0.0) { Serial.print('-'); number = -number; } // Round correctly so that print(1.999, 2) prints as "2.00" double rounding = 0.5; for (uint8_t i = 0; i < digits; ++i) rounding /= 10.0; number += rounding; // Extract the integer part of the number and print it unsigned long int_part = (unsigned long)number; double remainder = number - (double)int_part; Serial.print(int_part); // Print the decimal point, but only if there are digits beyond if (digits > 0) Serial.print("."); // Extract digits from the remainder one at a time while (digits-- > 0) { remainder *= 10.0; int toPrint = int(remainder); Serial.print(toPrint); remainder -= toPrint; } } void meassure() { InterruptCounter = 0; attachInterrupt(digitalPinToInterrupt(SensorPin), countup, RISING); delay(1000 * RecordTime); detachInterrupt(digitalPinToInterrupt(SensorPin)); WindSpeed = (float)InterruptCounter / (float)RecordTime * 2.4; //Windspeed (km/h) WS = (WindSpeed/3.6)*0.141881278113128; // Windspeed (m/s) * adj.factor } ICACHE_RAM_ATTR void countup() { InterruptCounter++; }