Home › Forum › Schede esp32 › ntp in void setup
Taggato: N
- Questo topic ha 1 risposta, 1 partecipante ed è stato aggiornato l'ultima volta 1 anno, 4 mesi fa da
Renzo Mischianti.
-
AutorePost
-
-
15 Gennaio 2024 alle 19:14 #29273
Ciao, come da tua richiesta ti scrivo per il problema relativo al server ntp.
Ho uno sketch, ancora in fase di bozza ma funzionante, che fa determinate cose e poi mette l’esp 32 in sleep mode per 10 minuti per poi riprendere il ciclo di on-off.
Fino qui tutto bene, vorrei inserire la parte del tuo sketch per recuperare la data e l’ora con la regolazione dell’ora legale, ho inserito tutto nel mio sketch solo che la parte compresa nel loop (serial.prinln…)l’ho messa nel setup ma facendo così non recupera la data e ora.
ti allego lo sketch (perdonami per com’è scritto ma è una bozza e sono agli inizi).
Ho provato tutto il tuo codice e funziona benissimo.#include "Arduino.h" #include <WiFi.h> #include <PubSubClient.h> #include <NTPClient.h> #include <WiFiUdp.h> #define mS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ #define TIME_TO_SLEEP 600 /* Time ESP32 will go to sleep (in seconds) */ #define ADCPIN1 34 /////batteria 3 volt //#define ADCPIN2 34///// batteria 3.6 #define ADCPIN3 35 //// batteria 9 volt #define LED 2 #include <WiFiUdp.h> #include <TimeLib.h> #include <time.h> #include <Timezone.h> int adcValue1; float voltValue1; int adcValue2; float voltValue2; int adcValue3; float voltValue3; int data; RTC_DATA_ATTR int bootCount = 0; const char* ssid = "xxxxxx"; const char* passw = "xxxx"; IPAddress local_IP(192, 168, 178, 216); IPAddress gateway(192, 168, 178, 1); IPAddress subnet(255, 255, 255, 0); IPAddress dns(192, 168, 178, 1); //IPAddress dns(8, 8, 8, 8); //optional //IPAddress secondaryDNS(8, 8, 4, 4); //optional WiFiClient wlanclient; PubSubClient mqttClient(wlanclient); static tm getDateTimeByParams(long time){ struct tm *newtime; const time_t tim = time; newtime = localtime(&tim); return *newtime; } /** * Input tm time format and return String with format pattern * by Renzo Mischianti <www.mischianti.org> */ static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)"%d/%m/%Y %H:%M:%S"){ char buffer[30]; strftime(buffer, 30, pattern, newtime); return buffer; } /** * Input time in epoch format format and return String with format pattern * by Renzo Mischianti <www.mischianti.org> */ static String getEpochStringByParams(long time, char* pattern = (char *)"%d/%m/%Y %H:%M:%S"){ // struct tm *newtime; tm newtime; newtime = getDateTimeByParams(time); return getDateTimeStringByParams(&newtime, pattern); } WiFiUDP ntpUDP; // By default 'pool.ntp.org' is used with 60 seconds update interval and // no offset //NTPClient timeClient(ntpUDP); // You can specify the time server pool and the offset, (in seconds) // additionaly you can specify the update interval (in milliseconds). int GTMOffset = 0; // SET TO UTC TIME NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", GTMOffset*60*60, 60*60*1000); // Central European Time (Frankfurt, Paris) TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time Timezone CE(CEST, CET); void setup() { Serial.begin(115200); if (!WiFi.config(local_IP, gateway, subnet, dns)) { Serial.println("STA Failed to configure"); } Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, passw); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } delay(5000); Serial.println(""); Serial.println("WiFi connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.print("ESP Mac Address: "); Serial.println(WiFi.macAddress()); Serial.print("Subnet Mask: "); Serial.println(WiFi.subnetMask()); Serial.print("Gateway IP: "); Serial.println(WiFi.gatewayIP()); Serial.print("DNS: "); Serial.println(WiFi.dnsIP()); mqttClient.setServer ("192.168.178.150", 1885); // mqttClient.setCallback(mqttCallback); if (mqttClient.connect ("ESP-Client_2", "xxx", "xxx")) { Serial.print ("Connected to MQTT Broker"); pinMode(LED, OUTPUT); digitalWrite(LED, LOW); delay(1000); } else { //if (!client.connected()) Serial.print("MQTT Broker connection failed"); Serial.print (mqttClient.state()); delay(1000); //reconnect(); } timeClient.begin(); delay ( 1000 ); if (timeClient.update()){ Serial.print ( "Adjust local clock" ); unsigned long epoch = timeClient.getEpochTime(); setTime(epoch); }else{ Serial.print ( "NTP Update not WORK!!" ); } //client.loop(); //pinMode(02, OUTPUT); //digitalWrite(02, LOW); //delay(500); //Increment boot number and print it every reboot //timeClient.update(); ++bootCount; Serial.println("Boot number: " + String(bootCount)); esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * mS_TO_S_FACTOR); Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds"); char voltValue[8]; //char voltValueB[8]; char voltValueC[8]; char adcValueC[8]; adcValue1 = analogRead(ADCPIN1); //adcValue2 = analogRead(ADCPIN2); adcValue3 = analogRead(ADCPIN3); voltValue1 = (((adcValue1 * 3.0) / 4095) * 1.6); //voltValue2 = ((adcValue2 * 3.6) / 4095); voltValue3 = (((adcValue3 * 9.0) / 4095) * 1.25); //voltValue3 = ((adcValue_raw * 1.25) *1); dtostrf(voltValue1, 1, 2, voltValue); //dtostrf(voltValue2, 1, 2, voltValueB); dtostrf(voltValue3, 1, 2, voltValueC); dtostrf(adcValue3, 1, 2, adcValueC); Serial.print("ADC1 Value = "); Serial.print(adcValue1); //delay(1000); Serial.print(" "); Serial.print("Voltage batteria 3 volt = "); Serial.print(voltValue1); Serial.println(" V"); Serial.println("data ora"); Serial.println(timeClient.getFormattedTime()); mqttClient.publish("esp32/sensore_test/batteria/3_volt",voltValue); delay(1000); //Serial.print(adcValue2); //delay(1000); //Serial.print(" "); //Serial.print("Voltage batteria 3.6 volt = "); //Serial.print(voltValue2); ////Serial.println(" V"); //mqttClient.publish("esp32/cameretta/batteria/3_6_volt",voltValueB); // delay(1000); //Serial.print(adcValue3); //delay(1000); Serial.print(" "); Serial.print("Voltage batteria 9 volt = "); Serial.print(voltValue3); Serial.println(" V"); mqttClient.publish("esp32/sensore_test/batteria/9_volt",voltValueC); mqttClient.publish("esp32/sensore_test/batteria/Valore_adc_9_volt",adcValueC); Serial.println("Going to sleep now"); delay(6000); Serial.flush(); esp_deep_sleep_start(); Serial.println("This will never be printed"); } void loop(){ }
-
18 Gennaio 2024 alle 15:21 #29296
Ciao Marco,
apparentemente non c’è nulla di strano sul tuo codice, prova ad aggiungere dei delay nelle varie fasi per scrupolo, ma pare ok.
Ciao RM
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.