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(){
}
Mantenere un repository, un sito o un forum è come prendersi cura di un giardino - richiede costante attenzione e cura per farlo prosperare. Se sei un abile giardiniere (o programmatore!) e vuoi aiutarci a far fiorire il nostro sito, saremmo felici di averti nel nostro team! Cerchiamo anche scrittori talentuosi e moderatori per il nostro forum per aiutarci a far crescere la nostra comunità. Se sei interessato a unirti a noi, non esitare a contattarci per farci sapere in che modo puoi contribuire!
Sei un appassionato di elettronica o programmazione? Condividi la tua conoscenza con gli altri, scrivi un semplice tutorial o come fare un grande progetto Contattami. share_your_ideas@mischianti.org
The content displayed on this website is protected under a CC BY-NC-ND license. Visitors are prohibited from using, redistributing, or altering any content from this website for commercial purposes, including generating revenue through advertising. Any unauthorized use is a violation of the license terms and legal action may be taken against individuals or entities found to be in violation.
You must also provide the link to the source.
Per offrire le migliori esperienze, utilizziamo tecnologie come i cookie per memorizzare e/o accedere alle informazioni del dispositivo. Acconsentire a queste tecnologie ci permetterà di elaborare dati come il comportamento di navigazione o gli ID unici su questo sito. Non acconsentire o ritirare il consenso può influire negativamente su determinate funzionalità e funzioni.
Functional
Sempre attivo
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Per offrire le migliori esperienze, utilizziamo tecnologie come i cookie per memorizzare e/o accedere alle informazioni del dispositivo. Acconsentire a queste tecnologie ci permetterà di elaborare dati come il comportamento di navigazione o gli ID unici su questo sito. Non acconsentire o ritirare il consenso può influire negativamente su determinate funzionalità e funzioni.
Functional
Sempre attivo
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.