/* Rui Santos Complete project details at https://RandomNerdTutorials.com/epoch-unix-time-esp32-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ //-------------------------------------------------------------------Wifi Date/time stuff //include wifi and time to get real date/time data #include #include "time.h" //-------------------------------------------------------------------Wifi Date/time stuff #include #include #include "DHT.h" #define DHTPIN 20 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 DHT dht(DHTPIN, DHTTYPE); int readingNumber = 1; String temp = ""; //-------------------------------------------------------------------Wifi Date/time stuff // Replace with your network credentials //-------------------------Wifi Date/time stuff const char* ssid = "NetworkName"; //-------------------------Wifi Date/time stuff const char* password = "Password"; //---------------------------Wifi Date/time stuff //-------------------------------------------------------------------Wifi Date/time stuff // NTP server to request epoch time const char* ntpServer = "pool.ntp.org"; //------------------------Wifi Date/time stuff // Variable to save current epoch time unsigned long epochTime; //---------------------------------------Wifi Date/time stuff // Function that gets current epoch time unsigned long getTime() {//---------------------------------------Wifi Date/time stuff time_t now;//---------------------------------------Wifi Date/time stuff struct tm timeinfo; if (!getLocalTime(&timeinfo)) { //Serial.println("Failed to obtain time"); return(0); } time(&now); return now; } // Initialize WiFi void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); } //-------------------------------------------------------------------Wifi Date/time stuff void setup(void) { //-------------------------------------------------------------------Wifi Date/time stuff Serial.begin(115200); initWiFi(); configTime(0, 0, ntpServer); //-------------------------------------------------------------------Wifi Date/time stuff dht.begin(); } void loop(void) { //-------------------------------------------------------------------Wifi Date/time stuff epochTime = getTime(); //Serial.print("Epoch Time: "); //Serial.println(epochTime); delay(1000); //-------------------------------------------------------------------Wifi Date/time stuff //For this example I only want 6 readings to be output to json while(readingNumber < 7){ if(readingNumber == 1){ //need open bracket before putting in json data Serial.println("["); } float t = dht.readTemperature(); // deley between each temperature reading delay(1000); //get data in json format StaticJsonDocument<1024> doc; String json; doc["date"] = epochTime; doc["temperature"] = t; serializeJson(doc, json); serializeJsonPretty(doc, Serial); //need a comma "," between each reading if(readingNumber < 6){ Serial.println(","); } //need closing bracket after last json data if(readingNumber == 6){ Serial.println(); Serial.println("]"); } readingNumber++; } }