/* Gracias a Rui Santos https://RandomNerdTutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/ */ // entramos las libretrías necesarias para nuestro NodeMcu #include #include #include #include #include //https://github.com/bblanchon/ArduinoJson (ver: 6.13) #include AsyncWebServer server(80); // Replace with your network credentials const char* ssid = "TP-LINK_E46A"; const char* password = "Playadonana*44Playadonana*44"; //const char* PARAM_HORA = "hora"; // valor 1 const char* PARAM_STRING = "inputString"; // valor 1 const char* PARAM_STRING2 = "inputString2"; // valor 1 const char* PARAM_STRING3 = "inputString3"; // valor 1 const char* PARAM_STRING4 = "inputString4"; // valor 1 //String la_hora; //const char* PARAM_HORA_VALLE77 = "hora_valle"; // valor 1 String estado; // Define NTP Client to get time WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org"); //Week Days String weekDays[7]={"Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sábado"}; //Month names String months[12]={"Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"}; const char index_html[] PROGMEM = R"rawliteral( ESP Input Form

Ejemplo de Network Time Protocol (NTP)

Además se muestra estado de salida en base a variables

Hoy es %inputString%

Hora %inputString2%

Sistema: %inputString4%

Sistema: %inputString3%

)rawliteral"; void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } String processor5(const String& var){ //Serial.println(var); if(var == "inputString"){ String CUCU= weekDays[timeClient.getDay()]; //Get a time structure unsigned long epochTime = timeClient.getEpochTime(); struct tm *ptm = gmtime ((time_t *)&epochTime); int monthDay = ptm->tm_mday; int currentYear = ptm->tm_year+1900; int currentMonth = ptm->tm_mon+1; String currentMonthName = months[currentMonth-1]; Serial.print("Month name: "); CUCU = CUCU + " " + monthDay + " de " + currentMonthName + " de " + currentYear ; return CUCU; } // añadimos parte del codigo de script para importar hora valle else if(var == "inputString4"){ String hora = estado; return hora; } else if(var == "inputString2"){ String hora = timeClient.getFormattedTime(); return hora; } unsigned long epochTime = timeClient.getEpochTime(); struct tm *ptm = gmtime ((time_t *)&epochTime); int currentYear = ptm->tm_year+1900; if (var == "Encendido" and currentYear==2021){ String fuego = ""; return fuego; } return String(); } void setup() { pinMode(LED_BUILTIN, OUTPUT); // Initialize Serial Monitor Serial.begin(9600); // Conectamos a la Wi-Fi Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); delay(1000); // Send web page with input fields to client server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor5); }); server.onNotFound(notFound); server.begin(); // Initialize a NTPClient to get time timeClient.begin(); // Set offset time in seconds to adjust for your timezone, for example: // GMT +1 = 3600 // GMT +8 = 28800 // GMT -1 = -3600 // GMT 0 = 0 timeClient.setTimeOffset(7200); // para el json StaticJsonBuffer<200> jsonBuffer; } void loop() { timeClient.update(); unsigned long epochTime = timeClient.getEpochTime(); Serial.print("Epoch Time: "); Serial.println(epochTime); String formattedTime = timeClient.getFormattedTime(); Serial.print("Formatted Time: "); Serial.println(formattedTime); int currentHour = timeClient.getHours(); Serial.print("Hour: "); Serial.println(currentHour); int currentMinute = timeClient.getMinutes(); Serial.print("Minutes: "); Serial.println(currentMinute); int currentSecond = timeClient.getSeconds(); Serial.print("Seconds: "); Serial.println(currentSecond); String weekDay = weekDays[timeClient.getDay()]; String CUCU= weekDays[timeClient.getDay()]; Serial.print("Week Day: "); Serial.println(weekDay); //Get a time structure struct tm *ptm = gmtime ((time_t *)&epochTime); int monthDay = ptm->tm_mday; Serial.print("Month day: "); Serial.println(monthDay); int currentMonth = ptm->tm_mon+1; Serial.print("Month: "); Serial.println(currentMonth); String currentMonthName = months[currentMonth-1]; Serial.print("Month name: "); Serial.println(currentMonthName); int currentYear = ptm->tm_year+1900; Serial.print("Year: "); Serial.println(currentYear); //Print complete date: String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay); Serial.print("Current date: "); Serial.println(currentDate); Serial.println(""); //delay(2000); // parte de consulta al archivo json de tecnologia-informatica.es //if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status HTTPClient http; //Declare an object of class HTTPClient http.begin("http://www.tecnologia-informatica.es/esios/json.json"); //Specify request destination int httpCode = http.GET(); //Send the request if (httpCode > 0) { //Check the returning code String payload = http.getString(); //Get the request response payload Serial.println(payload); //Print the response payload // añadimos StaticJsonBuffer<200> jsonBuffer; JsonObject& root2 = jsonBuffer.parseObject(payload); const char* precio = root2["Precio_valle"]; //const char* hora_valle = root2["hora_valle"]; //const char* hora_actual = root2["hora_actual"]; const int hora_valle = root2["hora_valle"]; const int hora_actual = root2["hora_actual"]; //const char* PARAM_STRING4 = root2["hora_valle"]; //const char* la_hora = root2["hora_valle"]; //const int inputString4 = root2["hora_valle"]; // pasamos variable para que se vea en la pagina Serial.print(" Tenemos un precio valle de: "); Serial.println(precio); Serial.print(" La hora valle es: "); Serial.println(hora_valle); Serial.print(" La hora actual es: "); Serial.println(hora_actual); if ( currentHour == hora_valle ) { digitalWrite(LED_BUILTIN, LOW); //digitalWrite(LED_BUILTIN, HIGH); estado = " Encendido"; Serial.print(" Activamos el sistema "); } else { digitalWrite(LED_BUILTIN, HIGH); // digitalWrite(LED_BUILTIN, LOW); estado = " Apagado"; Serial.print(" No es el momento"); } http.end(); //Close connection } delay(30000); //Send a request every 30 seconds }